Download

Help - Moving something using NavMesh and the SimpleMoveToLocation function

Hi everyone

As said in the title I would like to move an enemy by using a navmesh and the function SimpleMoveToLocation in a multiplayer project.

The function requires an Acontroller, so I created a new class “MyRobot” based on ACharacter and “MyRobotController” based on AController.

In MyGameMode.cpp I added this :


UWorld* const World = GetWorld();
if (World != NULL)
{
	AMyRobotController* RobotController = World->SpawnActor<AMyRobotController>(AMyRobotController::StaticClass(), FVector(100.0f, 300.0f, 0.0f), FRotator(0.0f, 0.0f, 0.0f));
	AMyRobot* Robot = World->SpawnActor<AMyRobot>(RobotClass, FVector(100.0f,300.0f,0.0f), FRotator(0.0f,0.0f,0.0f));
	if (Robot != NULL && RobotController)
	{
		RobotController->SetPawn(Robot);
		UE_LOG(LogGameMode, Log, TEXT("_____ Spawn Completed _____"));
	}			
}

In game I see the robot spawn correctly.

In the PostCreated function of MyRobotController.cpp, I call, with a timer of 3 secondes, this code :


UNavigationSystem* const NavSys = GetWorld()->GetNavigationSystem();

if (NavSys)
{
	IsMoving = true;
	FVector Destination;

	Destination.X = this->GetPawn()->GetActorLocation().X + 1000.0f;
	Destination.Y = this->GetPawn()->GetActorLocation().Y;
	Destination.Z = this->GetPawn()->GetActorLocation().Z;

	NavSys->SimpleMoveToLocation(this, Destination);
}

But nothing happens, MyRobot does not move. I tested this code on MyPlayerController and the player moves, It has some difficulties to move, as carrying heavy stuff, but it moves. Then I guess the code is correct but MyRobot/MyRobotController are, maybe, not a valid target.

I tested with an AAIController in place of AController, still does not work.

Anyone has an idea where my mistake is ?

Thanks in advance for you help, I really need it !

No idea ?

I “checked” the Rebuild at Runtime of the RecastNavMesh in the editor and I used AAIController now MyRobot rotate a bit during few secondes before stopping. Still no movement.

This can help you to help me ?

I’m not sure if setpawn is the right way, I normally use the Controller::Possess(Pawn) method.

Hi Moss,

I changed my code from SetPawn(MyRobot); to Possess(MyRobot);.

Now I does not see anymore MyRobot in game. With log, I know that MyRobotController is actif for 47 Ticks, after that, nothing more in log.

After some tests I realize that MyRobotController is just destroyed after the 47th Tick, probably the garbage collector remove it because it was not well spawned. So I suppose that I miss something somewhere.

Any ideas to fix that ?

Hi every one.

I did lot of testing to find out the problem. I finaly fixed it (MyRobot was detected as static mesh so the NavMesh considered it as an obstacle and exlude it from the NavMesh).

The problem is fixed … to create an other.

If, in the editor, I drop manually MyRobot in the level all work fine. But if I try to spawn it from code, it does not move at all.

This must be easy to fix that, but I does not find how to do that.

Some one can give me the final help ?

Thanks in advance.

Hi people, I still need some help from the UE community.

I still have trouble moving MyRobot with Navmesh when MyRobot is spawned during the game (C++).

The in-game image below show you the solo version. The ground, 4 blue waypoints and the blue MyRobot are all spawned durring the game (C++). In green, we can see that the Navmesh is correctly updated to include this new area.

4dbb70b981333b37ab69a81415f69261ff7228f8.jpeg

But if I call NavigationSystem->SimpleMoveToLocation(this, Destination->GetActorLocation()); inside MyRobotController, nothing happen. MyRobot does not move.

What I forgot to do ? I really need your help, I’m completly stuck there !

Great thank you.