Math Question: Get EnemyObject to NavMeshDestination x Units from Player

Math is not my strong suit.

I have an enemy and a player, and I want the enemy (an archer), to set the NavMesh Destination to be x units from the player, in the same angle between the player and enemy. So if the enemy is further than X units, they will move forward. If the enemy is closer than X units, they will move back.

For the life of me I can’t seem to figure this out. probably because math is hard :slight_smile:

Anyone know the solution?

var positionFromPlayer : Vector3;
var distanceToMove = (distanceToPlayer - shootRange);
if (distanceToMove > 0)
{
positionFromPlayer = (transform.position + transform.forward);
enemyNav.SetDestination(positionFromPlayer);
}
else
{
positionFromPlayer = (transform.position - transform.forward);
enemyNav.SetDestination(positionFromPlayer);
}

This works. Ensure that the navMesh stopping distance is set to zero, of course. Or zed, whichever you prefer. It’s not as elegant as my initial choice, but it works.