I am having trouble getting my AI actor to move. I cannot figure out what i’m doing wrong. I have researched this and viewed similar questions but nothing helps.
Controller Class:
#include "Wolf_AIController.h"
#include "Wolf_Character.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"
//Constructor
AWolf_AIController::AWolf_AIController()
{
PrimaryActorTick.bCanEverTick = true;
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));
SightConfig->SightRadius = AISightRadius;
SightConfig->LoseSightRadius = AILoseSightRadius;
SightConfig->PeripheralVisionAngleDegrees = AIFieldOfView;
SightConfig->SetMaxAge(AISightAge);
//we can detect, by affliliation, friendles, enemies, and neutrals.
SightConfig->DetectionByAffiliation.bDetectEnemies = false;
SightConfig->DetectionByAffiliation.bDetectFriendlies = false;
SightConfig->DetectionByAffiliation.bDetectNeutrals = false;
//Set the dominant sense as sight
GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation());
GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this, &AWolf_AIController::OnPawnDetected);
GetPerceptionComponent()->ConfigureSense(*SightConfig);
}
void AWolf_AIController::BeginPlay()
{
Super::BeginPlay();
if (GetAIPerceptionComponent() != nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("All Systems Set"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Error setting perception component"));
}
}
void AWolf_AIController::Possess(APawn * Pawn)
{
Super::Possess(Pawn);
}
void AWolf_AIController::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
FVector targetLocation;
AWolf_Character* character = Cast<AWolf_Character>(GetPawn());
if (target != NULL)
{
targetLocation = GetTargetLocation(target);
}
else
{
emenyDetected = false;
}
if (emenyDetected)
{
//Get the direction this controller is facing
FVector focalPoint = GetFocalPoint();
//Subtract that direction from the targets location
FVector direction = focalPoint - targetLocation;
//Save this direction in a rotation variable
FRotator newRotation = direction.Rotation();
//clamp the angle so it is a valid angle
newRotation.Yaw = FRotator::ClampAxis(newRotation.Yaw);
SetControlRotation(newRotation);
character->FaceRotation(newRotation, DeltaSeconds);
}
}
FRotator AWolf_AIController::GetControlRotation() const
{
if(GetPawn()==nullptr)
{
return FRotator(0.0f,0.0f,0.0f);
}
//Yaw is rotation around the up axis (around Z axis), Running in circles 0=East, +North, -South.
return FRotator(0.0f, GetPawn()->GetActorRotation().Yaw, 0.0f);
}
void AWolf_AIController::OnPawnDetected(TArray<AActor*> Detectedpawns)
{
if (Detectedpawns[0]!=nullptr)
{
emenyDetected = true;
target = Detectedpawns[0];
}
else
{
emenyDetected = false;
}
}
Character Class:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Wolf_Character.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
AWolf_Character::AWolf_Character()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
bUseControllerRotationPitch = true;
bUseControllerRotationRoll = true;
bUseControllerRotationYaw = true;
//face the direction you are a moving
//GetCharacterMovement()->bOrientRotationToMovement = true;
//turn at this given rate along the Y axis
//GetCharacterMovement()->RotationRate = FRotator(0.0f, 600.0f, 0.0f);
}
// Called when the game starts or when spawned
void AWolf_Character::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AWolf_Character::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AWolf_Character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}