- Home /
How to increment the number by 1's to get distance traveled?
I have a DistanceKeeper that moves to the left, it is a child of a static object. Currently I am calculating the distance between DistanceKeeper and it's parent in order to get the distance using this code:
distanceTraveled = Mathf.Abs(transform.position.x - parent.transform.position.x);
I am then displaying in a DFgui label, but the thing is, the number is not sequential.
For example, instead of going 1 2 3 4 5, it will output 1 3 5 9 13 20.
Is there another way to return the distance traveled in a updated and sequential way?
If the object stays within it's parent, it's easier to use
distanceTraveled = $$anonymous$$athf.Abs(transform.localPosition.x)
Having said that, the other calculation should also work. How are you moving the object?
$$anonymous$$ost likely your GameObject movement is not sequential. Code is fine.
Please display your movement code.
@Ivovd$$anonymous$$arel, the distancekeeper is a child and I have used the new line to calculate the distanceTraveled, but it still skips.
@Bored$$anonymous$$ormon, here is the movement code.
void Update ()
{
distanceTraveled = (int) $$anonymous$$athf.Abs(transform.localPosition.x); //cast the float transform.positon.x as an int
transform.Translate(Vector2.right * (Time.deltaTime * -5));
}
Well, I figured out the problem. For some reason I put the get distanceTraveled into a get input rather than the update itself.
Thanks for the help guys, the localposition really helped.
Your answer
Follow this Question
Related Questions
Resource counter doesn't reset on each iteration 0 Answers
How to Update a score count going up and down 1 Answer
Does Update() count matter? 2 Answers
Ping pong position using lerp 3 Answers