Does not work Ai MoveToLocation

Hello,

I just need to move the bot to the some place use MoveToLocation.

That’s what I’m doing:

  1. create BotAiController inherited from AAIController
  2. in BeginPlay() BotAiController call MoveToLocation()
  3. create BP from Pawn or Character, assign him BotAiController
  4. place NavMesh
  5. place bot on the stage and run
  6. Must move, but does not move…why?

Equivalent code on bp works.

I have a big old project (4.10) in which much is used MoveTo/MoveToLocation/MoveToActor.
It is important for me to understand why it does not work anymore.

Thanks!



#pragma once
 
#include "CoreMinimal.h"
#include "AIController.h"
#include "NextAIController.generated.h"
 
UCLASS()
class TPSPROJECT_API ANextAIController : public AAIController
{
    GENERATED_BODY()
   
    virtual void BeginPlay() override;
};




#include "TPSProject.h"
#include "NextAIController.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
#include "Runtime/Engine/Classes/Engine/World.h"
#include "TPSCharacter.h"
 
void ANextAIController::BeginPlay()
{
    GLog->Log("BeginPlay");
 
    UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
 
    ATPSCharacter* player = (ATPSCharacter*)UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
    FVector vec = player->GetActorLocation();
 
    GLog->Log(vec.ToString());
 
    MoveToLocation(vec);
}


Equivalent code on bp:

set up controller:

Make sure the character is possessed by an AI Controller before you ask the character to move to a location.

Also, you’re not calling Super::BeginPlay() for some reason. Make sure you add that in before your own code. I would suggest adding a breakpoint and stepping through the code to find out why it’s not working.

You’re also not casting correctly. C-style casting isn’t safe, you should use Unreal’s ‘Cast’ function:

this:



ATPSCharacter* player = (ATPSCharacter*)UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);


should be this:



ATPSCharacter* player = Cast<ATPSCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));


After adding Super::BeginPlay() everything worked.
Thank you!