Search Unity

Counting the distance traveled

Discussion in 'Scripting' started by chimpanzeee, Jul 3, 2022.

  1. chimpanzeee

    chimpanzeee

    Joined:
    May 22, 2017
    Posts:
    7
    Hello,
    I am pretty new to game development and I am trying to achieve this: counting the distance the player has moved/traveled.
    So in my head, I would have to compare old position and new position every frame and then add the difference to a "distanceTraveled" variable. I have been trying but can't find a good way to do it, also taking into account the fact that the player would not move at certain moments etc..

    Can someone help me or at least show me the right direction?
    Thanks!
     
    Last edited: Jul 3, 2022
  2. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    I don't know, but maybe this could help you :

    https://docs.unity3d.com/ScriptReference/Vector3.Distance.html
    After that depends on what you want, the real distance or the fake distance.

    I'm not strong in size units, but I think I read that a cube of 1x1x1 = represents 1 meter, to be confirmed by the masters in the field, so if you want to have real distances you will have to build your games at the scale.

    After you can truncate your values and multiply your result by a multiplier... it's up to you.
    I hope to have helped you with my poor knowledge in the field.
     
  3. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    I just wrote this script quickly I haven't tested it, but I think you have to do something like this :


    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3. using UnityEngine.UI;
    4.  
    5. public class ReplaceText: MonoBehaviour
    6. {
    7.    public GameObject myGametext;
    8. vector3 startPos = new Vector3(x,y,z);
    9.  
    10.    void Update()
    11.    {
    12.  
    13. float dist = Vector3.Distance(startPos.position, transform.position);
    14.      
    15.        myGametext.GetComponent<TMP_Text>().text = "My distance is : " + dist;
    16.    }
    17. }
    You create a text mesh pro,

    you paste my code on your player.

    you replace x,y,z of startPos by the coordinates of your start pos.

    and in the inspector of your player you drop your text in the variable myGametext.

    Then if you just want the distance of a given axis, I don't know, maybe something like this:
    (here is an example on the X axis) "transform.position.x", gives you the position of your player on the X axis, and therefore if your start is on x=0, then you will have your distance ;)
    After that it's up to you to give the right size to your elements so that the distances are real.


    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3. using UnityEngine.UI;
    4.  
    5. public class ReplaceText: MonoBehaviour
    6. {
    7.    public GameObject myGametext;
    8.  
    9.  
    10.    void Update()
    11.    {
    12.  
    13.      
    14.        myGametext.GetComponent<TMP_Text>().text = "My distance is : " + transform.position.x;
    15.    }
    16. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You can use Vector3.Distance() to compare start to end positions and you will get an "as the crow flies" distance. This works if the player is moving in a linear direction, but if they wander, you don't add the wandering.

    Otherwise you can see how far the player moved each frame (in Update()) and tally all the micro-distances up into some accumulator.