Download

Custom movement component and AI

I’m working on some basic AI training, and I need to create a basic movement component with steering behaviors. I have basic steering, but I need it to work with pathfinding. While the “Move To” nodes partially work with the new component, it obviously does not take pathfinding into account. I’ve tried looking through the “Character Movement Component” how it uses pathfinding, but I came up short. I can’t progress with the steering behaviors until my barebones pawn is using NavMesh pathfinding.

So what’s the absolute minimum code required to have a barebones Pawn and Movement Component (inherited from Pawn Movement Component) use pathfinding? I can’t use Character because it’s way too complex and bloated for what I need to do. I couldn’t find any reference how a Movement Component takes pathfinding into account.

Have a look at the PathFollowingComponent class, that’s what controls path following. When you call the Move To node it goes over the AI controller and ultimately requests the move on the path following component. In the tick function it then calls UNavMovementComponent::RequestPathMove(const FVector& MoveInput), which you can override on your custom pawn movement component.

Thank you. Using the “MoveInput” parameter I’ve managed to pass it to the Velocity of the Component to get it to move along the path. Of course, this is just the beginning, now I need to use the MoveInput variable to control the steering. Is there a way to “track” the path within the Movement Component so that I can see how far it’s along the path? Or is there another way to control acceleration and deceleration along the path and so that I can control how it stops at the destination?

You can override UNavMovementComponent::GetPathFollowingBrakingDistance(float MaxSpeed), which will automatically adjust the MoveInput to to interpolate to 0 approaching the target. Have a look at UPathFollowingComponent::FollowPathSegment in PathFollowingComponent.cpp that gives you an idea of how it is implemented and how you can influence it.

I can’t seem to get it to work. What I can see is that GetPathFollowingBrakingDistance merely returns the distance based on how you calculate it (CharacterMovement calculates based on MaxSpeed and rate of deceleration). The FollowPathSegment method simply gets the braking distance and then calculates when the Pawn needs to start braking and gradually interpolates MoveInput to 0. Movement works fine, but whatever I do it never starts braking and stops at the destination.

Is there a way to get the **current **distance of the Pawn to the destination on the path? So I can say “if (distanceOnPath < brakingDistance)” and reduce the velocity.

EDIT: Everything appears to be working, it’s just that the Pawn never seems to brake. The overridden GetPathFollowingBrakingDistance method is never called (I’ve put a log in there) and the MoveInput size remains 1 and never “slows down”. bUseAccelerationValues is set to true, and it does call RequestPathMove, but the FollowPathSegment method never seems to “slow down” the MoveInput variable when calling RequestPathMove so it keeps moving at full speed, regardless of any braking distance.