1
\$\begingroup\$

I am working on a road fighter kind of game. The game has its tracks moving to give the effect of car movement. The car accelerates and decelerates as expected with certain keys pressed. But How do I give the notion of distance travelled by the car and how to calculate it?

For some reference, here's how the movement of tracks is sped to simulate acceleration and deceleration:

void Update ()
        {
        if (Input.GetKey(KeyCode.F))
        {
            if(speed<=35f)
            {
            speed = speed + 3f* Time.smoothDeltaTime; 

            }

        }
        if (Input.GetKeyUp(KeyCode.F))
        {

                speed = speed - 7f * Time.smoothDeltaTime;

        }

        if(Input.GetKeyDown(KeyCode.S))
          {
            speed = speed - speed/2;
        }
        this.transform.Translate (Vector3.right * Time.smoothDeltaTime * speed);

        }
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Simple: just keep track of the distance traveled by keeping track of the changed time and current speed. Make sure that if your speed is in kilometers per hour that you multiply by time in hours.

If we assume Time.smoothDeltaTime is in seconds:

timeInHours = (Time.smoothDeltaTime / 60) / 60;
totalDistance += speed * timeInHours;
\$\endgroup\$
1
  • \$\begingroup\$ Ahh, silly me :D I was not adding the distance :) Thanks Roy :) \$\endgroup\$
    – systemdebt
    Commented Sep 6, 2014 at 22:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .