Find out from which side the agent entered NavLink

Hello everyone.
Is there any way in C++ or Blueprint to find out from which side the agent used a NavLink?

Yes of course! You can get the character’s direction by using the “Get Actor Rotation” node when it enters the nav link range.

Or if the character’s capsule component can face to different directions than it’s movement direction, you can calculate the direction it entered the nav link on the x-y plane by getting it’s velocity, normalizing it and comparing it to the (x = 1, y = 0, z = 0) unit vector. To compare, you can perform an addition between the character’s location and the normalized vector. Also add the other unit vector to the character’s location as well, and then plug the both vector type results into a “Find Look at Rotation” node. That would give you the angle it’s deviated from the +x world direction. There are others ways to compare those vectors too, but this time I chose to recommend this one.

Hope this helps! :blush:

1 Like

I use this, it’s a bit hacky, but couldn’t find any better.

/**
 * Callable from ReceiveSmartLinkReached in NavLinkProxy subclasses, 
 * Helps figuring out if agent is traversing Left-to-right or Right-to-left.
 */
UFUNCTION(BlueprintPure, Meta = (AdvancedDisplay = 2))
static bool IsTraversingLinkLTR(const UNavLinkCustomComponent* SmartLink, const FVector& Dest, const float ZTolerance = 50)
{
    if (SmartLink)
    {
        // Dest and Link point do not match exactly due to navmesh Z offset
        const FVector& RightPoint = SmartLink->GetEndPoint();
        return Dest.X == RightPoint.X && Dest.Y == RightPoint.Y && FMath::Abs(Dest.Z - RightPoint.Z) < ZTolerance;
    }
    return true;
}

1 Like