Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Getting consistent distance moved per frame

Discussion in 'Scripting' started by CoughE, Mar 22, 2021.

  1. CoughE

    CoughE

    Joined:
    Oct 26, 2015
    Posts:
    39
    Hello there,

    I have a world map screen where the player can move between points, and the game will tell the amount of time and distance between each point. There is an icon for the player which moves via a navmesh agent.These values are fudged, but I am having trouble getting them to be consistent.
    For example, under the same exact conditions, it might say the distance travelled was, 6.89 or 3.2. The distance each checked each frame, as I want to show the actual distance the player has moved as their icon moves across the map.

    Currently I am calculating this by doing this in Update(): Note the * 60f is just to give it more readable values.
    Code (CSharp):
    1.  
    2. [INDENT]var distTravelledThisFrame = (transform.position - lastPos).magnitude * (Time.deltaTime * 60f);[/INDENT]
    3.         distanceTravelled += distTravelledThisFrame;
    4.         minutesTravelled += distTravelledThisFrame * (hoursPerDistanceUnit * 60f) ;
    5.  
    Removing Time.delta time still produces inconsitent results. Has anyone ever had trouble with getting a distance moved per frame? Does it have something to do with the NavMeshAgent? Thanks for your time!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    transform.position - lastPos already accounts for frame time, since I'm assuming lastPos was the position last frame. So don't multiply it by Time.deltaTime. I'd remove that 60f from line 2 as well, while you are troubleshooting this. In fact, I don't see what it is there, since you said you want to show the actual distance, but with it you will show 60X the distance.
     
  3. CoughE

    CoughE

    Joined:
    Oct 26, 2015
    Posts:
    39
    Yes sorry, lastPos is simply the position after. Here is the whole LateUpdate method, without using Time.deltaTime.

    Code (CSharp):
    1.  
    2. private void LateUpdate()
    3.     {
    4.  
    5.         var distTravelledThisFrame = (transform.position - lastPos).magnitude;
    6.         distanceTravelled += distTravelledThisFrame;
    7.         minutesTravelled += distTravelledThisFrame * (hoursPerDistanceUnit * 60f) * speedMultiplier;
    8.  
    9.    
    10.         lastPos = transform.position;
    11.     }
    12.  
    hoursPerDistanceUnit and speedMultiplier are float values that do not change for this example. Might as well think of them as 1f.
    I initially thought I wouldn't need to use Time.deltaTime as you said, since its just getting a distance. But, since i'm trying to get the distance moved per frame as the player moves along it I need to get the change in distance per frame, which is is producing very different results for some reason.
     
  4. CoughE

    CoughE

    Joined:
    Oct 26, 2015
    Posts:
    39
    In case anyone in the future comes looking, I have solved this by taking a different approach. I simply get the percent of the journey and calculate from there.
    1. On the start of moving to another point, store the initial distance to the point (startingDistance)

    Code (CSharp):
    1.     public float GetDistToDest() => Vector3.Distance(transform.position, dest);
    2.     float PercentJourneyCompleted => 1 - (GetDistToDest() / startingDistance);
    3. private void LateUpdate()
    4.     {
    5.  
    6.         float percentNow = PercentJourneyCompleted;
    7.  
    8.         var distTravelledThisFrame = startingDistance * (percentNow - percentCompleteLastFrame);
    9.         distanceTravelled += distTravelledThisFrame;
    10.         minutesTravelled += distTravelledThisFrame * (hoursPerDistanceUnit * 60f) * speedMultiplier;
    11.  
    12.  
    13.         percentCompleteLastFrame = PercentJourneyCompleted;
    14.     }
     
    Joe-Censored likes this.