Hello. I want to spawn some pawn and make it move. For spawning I use
MyControllerAI = GetWorld()->SpawnActor<AAIController>(AAIController::StaticClass());
MyPawn = GetWorld()->SpawnActor<AMyPawn>(AMyPawn::StaticClass());
MyControllerAI->SetPawn(MyPawn);
Here, AMyPawn is empty class that extends APawn.
Next, on user input I’m trying:
NavSys->SimpleMoveToLocation(MyControllerAI , DestLocation);
or
MyControllerAI->MoveToLocation(DestLocation);
None of this works. I’m logging MyPawn position - it always 0.
P.S.: I have navmesh - it works for main character.
It should be at location 0,0,0 which is exactly where you told it to spawn.SpawnActor is defined in World.h:
AActor* SpawnActor( UClass* Class, FVector const* Location=NULL, FRotator const* Rotation=NULL, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() );
Try giving it the location where you want it to spawn. You should checkout the ShooterGame example. Normally, in the gamemode, you want to find a playerstart for the Player. NPCs can be spawned at spawn points you might place around the map.
Hi. You didn’t understand a question. My problem is that navigation doesn’t work - pawn is always in the location where it spawned. After calling of NavSys->SimpleMoveToLocation or MyControllerAI->MoveToLocation.
This isn’t working on my end either, same problem. Did you ever find a solution? I’ve tried adding a navmesh to my level, but that did no good.
EDIT: LMAO. It’s working, problem was I had a breakpoint on my MoveToLocatoin() call and I was staring at Visual Studio while my pawn was moving.
Solution incoming
You need to have a Navigation Mesh covering your Pawn and the destination of which it is moving. MoveToLocatoin() and SimpleMoveToLocatoin() will not work otherwise. I’ve posted a couple screenshots as well as links to related resources.
This video was very helpful in getting the basics on how to lay down a Navigation Mesh. All you need in this case (moving using code) is the Navigation Mesh Volume. The destination points are optoinal, you can use them or you can feed the MoveToLocation() functions a 3d Vector.
[Unreal Engine 4 Tutorial - Basic AI Navigation - YouTube][1]
Here is what my call to MoveToLocatoin() looks like. It should be self-explanatory.
MoveToLocation( FVector( GetPawn()->GetActorLocation().X + 5550.f, GetPawn()->GetActorLocation().Y + 5550.f, GetPawn()->GetActorLocation().Z) );
Here is a screenshot of my scene/editor setup. I have the Nav Mesh Bounds Volume you need to drop in your level isolated in the Modes menu, I turned on Nav Mesh Visuals, and selected my Nav Mesh Bounds Volume so you can see it’s location on my map.