Hi all…
I’m trying to experiment with the Nav system. I have a VERY basic level.
- A ship
- A terrain
- Water
I’m tying to use the Nav system to make a ship sail through the water. To create my navigation points, I created an empty game object with one gizmo for visual reference. I then created a prefab out of that naming it NavPoint.
I then placed 7 NavPoint prefabs around my screen making sure that all Y axis were at 0. I named each prefab NavPoint1 through NavPoint7. See
On my Ship, I have a NavMeshAgent and a script called ShipNavigation.
My ShipNavigation script is based on the Unity reference
https://docs.unity3d.com/Manual/nav-AgentPatrol.html
As you can see, I have added each NavPoint to the scripts collection.
Here is the ShipNavigation script. It’s not bullet proof yet, just proof of concept code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ShipNavigation : MonoBehaviour
{
public GameObject[] _navPoints;
private NavMeshAgent _navAgent;
private int _nextPoint = 0;
void Start( )
{
_navAgent = GetComponent<NavMeshAgent>();
_navAgent.autoBraking = false;
DisplayPointPosition( );
GoToNextPoint();
}
void GoToNextPoint( )
{
if( _navPoints.Length == 0 )
return;
Debug.Log( "Nav To " + _navPoints[ _nextPoint ].name );
transform.LookAt( _navPoints[ _nextPoint ].transform.position );
_navAgent.destination = _navPoints[ _nextPoint ].transform.position;
_nextPoint = NextNavPoint( _nextPoint );
}
void Update( )
{
if( _navAgent.remainingDistance < 0.5f )
GoToNextPoint( );
}
// Get the next spot in the array. Circle back to the beginning if needed
private int NextNavPoint( int curNavPoint )
{
if( curNavPoint == _navPoints.Length - 1)
curNavPoint = 0;
else
curNavPoint++;
return curNavPoint;
}
// Utility function to just display the NavPoint transform positions.
private void DisplayPointPosition( )
{
Transform t;
foreach( GameObject o in _navPoints )
{
t = o.transform;
Debug.Log( o.name + " --- X = " + t.position.x + ", Y = " + t.position.y + ", Z = " + t.position.z);
}
}
}
When the code runs, I’m spitting out this debug info. I wanted to see what each NavPoint prefab has as it’s transform location. When I look at this, I can see that at run time, my prefabs do NOT have the same values as they have in Scene view/Inspector.
Here is what debug shows.

Yet, here is what each value is in the inspector.
NavPoint1 X=73, Y=0, Z=-155
NavPoint2 X=-32, Y=0, Z=-37
NavPoint3 X=-14, Y=0, Z=121
NavPoint4 X=79, Y=0, Z=145
NavPoint5 X=130, Y=0, Z=32
NavPoint6 X=248, Y=0, Z=-120
NavPoint7 X=173, Y=0, Z=-238
After logging these values out to the console and I log where the ship is heading to, which would be NavPoint1. This can be seen in the debug log in the console. GoToNextPoint() gets called, I log where the ship is heading which can be seen in Nav To NavPoint1 in the console. I then set the nav agents destination and the ship begins to move. Despite the transform x,y,z values being different between design time and run time, the ship starts to move towards NavPoint1. I then get the next index in the array for the next time we need to tell the ship where to go.
Update().
The function is working as I want. Initially…
While the ship is still heading towards NavPoint1, everything works fine. But, once this if condition passes
if( _navAgent.remainingDistance < 0.5f )
GoToNextPoint() gets called repeatedly.
It’s as if if( _navAgent.remainingDistance < 0.5f ) does not pick up on the fact that there is a new NavPoint to head to and it thinks it’s still close to it’s destination.
Then, the ship will just navigate between NavPoint1 NavPoint6, NavPoint7 and circle around.
Like I said, I know the code (mine) is not bullet prooft, and it’s also based on the Unity example
https://docs.unity3d.com/Manual/nav-AgentPatrol.html
Thoughts on why I’m getting odd behavior like this?

