Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  3. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Transform points at run time are not the same as design time.

Discussion in 'Navigation' started by rickblacker, Apr 18, 2017.

  1. rickblacker


    May 1, 2014
    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


    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    6. public class ShipNavigation : MonoBehaviour
    7. {
    9.     public GameObject[] _navPoints;
    10.     private NavMeshAgent _navAgent;
    11.     private int _nextPoint = 0;
    14.     void Start( )
    15.     {
    16.         _navAgent = GetComponent<NavMeshAgent>();
    17.         _navAgent.autoBraking = false;
    19.         DisplayPointPosition( );
    20.         GoToNextPoint();
    21.     }
    24.     void GoToNextPoint( )
    25.     {
    26.         if( _navPoints.Length == 0 )
    27.             return;
    29.         Debug.Log( "Nav To " + _navPoints[ _nextPoint ].name );
    30.         transform.LookAt( _navPoints[ _nextPoint ].transform.position );
    32.         _navAgent.destination = _navPoints[ _nextPoint ].transform.position;
    33.         _nextPoint            = NextNavPoint( _nextPoint );
    34.     }
    37.     void Update( )
    38.     {
    39.         if( _navAgent.remainingDistance < 0.5f )
    40.             GoToNextPoint( );
    41.     }
    44.     // Get the next spot in the array. Circle back to the beginning if needed
    45.     private int NextNavPoint( int curNavPoint )
    46.     {
    47.         if( curNavPoint == _navPoints.Length - 1)
    48.             curNavPoint = 0;
    49.         else
    50.             curNavPoint++;
    51.         return curNavPoint;
    52.     }
    55.     // Utility function to just display the NavPoint transform positions.
    56.     private void DisplayPointPosition( )
    57.     {
    58.         Transform t;
    59.         foreach( GameObject o in _navPoints )
    60.         {
    61.             t = o.transform;
    62.             Debug.Log( + " --- X = " + t.position.x  + ", Y = "  + t.position.y + ", Z = " + t.position.z);
    63.         }
    64.     }
    65. }
    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.

    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
    Code (CSharp):
    1. if( _navAgent.remainingDistance < 0.5f )
    GoToNextPoint() gets called repeatedly.

    It's as if
    Code (CSharp):
    1. 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

    Thoughts on why I'm getting odd behavior like this?
  2. rickblacker


    May 1, 2014
    Guess I should also add that Transform.LookAt is not rotating the ship in the direction of the navigation point. Am I wrong in assuming that this would rotate my ship in the direction of next destination / navpoint?
  3. Jakob_Unity


    Unity Technologies

    Dec 25, 2011
    The path calculation can take several frames, especially if the path passes through a lot of navmesh polygons (see also

    Additionally - the remainingDistance is the remaining distance of the current, already calculated, path.

    So I'm guessing it's likely because you still have the same destination for a number of frames (for some of the legs on the route), plz - try this:

    Code (csharp):
    2. if(! _navAgent.pathPending && _navAgent.remainingDistance < 0.5f)
    3.   GoToNextPoint( );
    while I'll go fix the example in the docs :oops:
    rickblacker likes this.
  4. rickblacker


    May 1, 2014
    Hey Jakob!

    Adding !_navAgent.pathPending to the if statement fixed my issue! I had a feeling it was something to do with what you explained. I just wasn't sure how to fix it.

    Thank you so very much! :)