AI following a spline

Hi,
I would like to make my AI follow a spline while still using the navmesh and detour avoidance.

I came up with a solution that I didn’t implement yet since I’m not sure if it’s correct or if there’s already a better one, so just for the sake of it I’m asking first before potentially throwing my work away :slight_smile:
Namely the solution consists of creating an invisible Actor that follows the spline and then setting that actor as a move target for my AI.

My main concern is if detour will still work correctly when the target is not far enough from the AI or what would happen if the target suddenly finds itself inside a dynamic collision while the real target (end of spline) is still reachable and unobstructed.

Thanks in advance.

I am not 100% sure what you are trying to accomplish but maybe this will help:

    APawn* MyBot = MyController->GetPawn();
    AMyCharacter* FollowTarget = MyController->GetTarget();
    if (Enemy && MyBot)
    {
        const float SearchRadius = 200.0f;
        const FVector SearchOrigin = FollowTarget->GetActorLocation() + 600.0f * (MyBot->GetActorLocation() - FollowTarget->GetActorLocation()).SafeNormal();
        const FVector Loc = UNavigationSystem::GetRandomPointInRadius(MyController, SearchOrigin, SearchRadius);
        if (Loc != FVector::ZeroVector)
        {
            MyComp->GetBlackboardComponent()->SetValueAsVector(BlackboardKey.GetSelectedKeyID(), Loc);
            return EBTNodeResult::Succeeded;
        }
    }
 
    return EBTNodeResult::Failed;
}

This will have the AI basically look for whatever target you want him to follow. It will then pick a random point near that target and save that location into a BlackBoard Component. Then you can tell the pawn to simply move to that location. This is dynamic path finding. This is how enemy bots find and move to the player in the ShooterGame example. I tweaked the code to suite my needs, perhaps you could do the same.

Hi, thanks for your answer but that’s not what I ment. What i want to achieve is an NPC following a spline (curve) that I place on the level as a SplineComponent. I need this to make them patrol more naturally without making hard turns after reaching certain point.

Hi ,

There’s currently no out-of-the-box way to do what you describe. In general the solution you described would work, with caveats:

  • having AI follow an actor that constantly changes location will result in repathing every “threshold” distance covered by followed actor
  • every repath can result in totally different path, which in turn might defeat your purpose and result in sharp turns
  • you’d need to influence virtual-goal-actor’s movement to stay as more or less same distance of the AI following it, to guarantee “stable” result

But give your solution a try none the less, you’re bound to learn something! :smiley:

The proper solution would be to implement custom PathFollowingComponent (that tells CharacterMovementComponent where it should go) or custom CharacterMovementComponent (to adjust what PathFollowingComponent is telling it), and just handle sudden move direction changing, smoothing it out.

There are some far plans to do that on the AIModule level, but not anytime soon. Sorry!

Cheers,

–mieszko