I’m trying to create a logic that makes an npc move to the back of the player over half a circle, but have some troubles calculating this NavMesh path/destination.
I’m trying to cut up this half circle in a few points (this case 8 points), which will make up the path and which is set in the agent.
public void CalculatePath()
{
int amountOfPoints = 8;
NavMeshAgent agent = sm.gameObject.GetComponent<NavMeshAgent>(); //sm is the Statemachine attached to the NPC gameobject.
Vector3 playerPos = player.transform.position;
Vector3 npcPos = sm.transform.position;
float radius = Vector3.Distance(playerPos, npcPos);
Vector3[] points;
for (int i = 0; i < amountOfPoints; i++)
{
//logic to calculate the specific points.
}
//Form a path from the points.
//Set the NavMesh agent to follow the path.
}
Hopefully someone can help me out with this code or maybe there is a more efficient way to do this. Thanks already!
There is a nice tutorial in our docs on creating patrolling behavior for NavMeshAgent. You could use the same concept to make your agent follow points on the circle.
You cannot create and modify NavMeshPath directly. To move the agent around like you’ve described it involves some manual steering because SetDestination will always try choose the shortest direct path to the endpoint.
To do this you have at least 2 options:
Manually steer the NavMeshAgent around the player by choosing a set of points like you described above. Using the Unit Circle & Sine/Cosine you can pick the X/Z coordinates around the player to move. Based on your question this is probably what you’re looking for. This tutorial may be helpful
You could calculate the path to the endpoint to get the NavMeshPath and based on the corners, curve the path with a relatively high curve factor and manually steer the agent around it. This tutorial may also be helpful.
hi, not sure if this will help, but since you need to go behind your player, you can multiply by 2 the distance from your NPC to the player (radius line in your drawing) and you’ll get your end point position. Which you could then (I think) use Vector3.Slerp (and a coroutine(?)) to move your NPC around the player to the final position. Maybe I’m wrong but writting this just in case