How to use "MovetoLocation" to move character

please excuse my stupidness if i am doing something simple wrong but i am new to developing with unreal engine. What i am trying to do is move my character from start of maze to end of maze and have accomplished it using blueprints but i want to implement it in c++.

I Have assigned my characters AI Controller Class to my AIController.

in my AiController class i have

void AMyAIController::MoveEnemyToTarget(){


	AMyEnemyCharacter* const Enemy = Cast<AMyEnemyCharacter>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));

	if (Enemy != NULL){
		FVector location = FVector(1962, 1270, 260);
		AMyAIController::MoveToLocation(location, 10.0f, true, true, true);
	}

}

and then in my Characters BeginPlay() i have

Super::BeginPlay();

	AMyAIController* ai = Cast<AMyAIController>(AMyAIController::StaticClass());
	
	FVector location = FVector(1962, 1270, 260);

	if (ai != NULL){
		ai->Possess(this);
		ai->MoveEnemyToTarget();
	}

When i go to play it just dose nothing… any help greatly appreciated as it has been annoying and hold me up all day

Hi Dobby,

The AI Controller should automatically possess the character when it spawns so you should be able to remove that bit. To get a reference to the controller you should call GetController instead of StaticClass.

Try replacing your BeginPlay function with this

void AMyCharacter::BeginPlay()
{
	Super::BeginPlay();

	if( GetWorld() && !GetWorld()->bStartup && GetController() == nullptr )
	{
		SpawnDefaultController();
	}

	AMyAIController* ai = Cast<AMyAIController>(GetController());

	FVector location = FVector(1962, 1270, 260);

	if( ai != NULL ) {
		ai->MoveEnemyToTarget();
	}
}

In your character constructor, make sure to set the default controller class (you do use StaticClass here):

AIControllerClass = AMyAIController::StaticClass();

Still wont work for me :frowning: would the fact that i have the player controller class to for controlling my camera and other game objects make a difference??

I don’t think that would cause any problems.

Try setting a breakpoint in BeginPlay on the line where you cast to your AI Controller class. See if the variable ai is null or if MoveToLocation is failing.

it keeps coming back from the cast as null

Well, if it doesn’t cast to AIController then it’s either null or a PlayerController. This basically means whole your setup and/or assumptions are wrong and I suggest grabbing any on UE4 samples that have AI in it and see how it’s done there.

There’s a lot of places where stuff could have gone bad for you, so it’s a wast of time trying to solve it via an answerhub thread. Just look at samples.

Cheers,

–mieszko

Thanks Alderbit. this worked for me eventually when i created a new AI controller class and new character class… not sure what the initial problem was

Take a look at the UE4 sample project TopDown in C++. In that project the character moves where you click. This is done using the function MoveToLocation(…). It is very simple and well documented.

Hope this helps!