Question by
ItzChris92 · Jul 25, 2020 at 10:07 PM ·
c#distancetime.deltatime
Calculating distance travelled. Not sure what i'm missing....
private void Update()
{
float distanceTravelled = movingSpeed.Value * Time.deltaTime;
testDistanceText.text = Mathf.RoundToInt(distanceTravelled).ToString() + "m";
}
Any reason why this shouldn't work? My movingSpeed.Value is 10. My debug.log shows 0. If I take out the RoundToInt the float is bouncing around at a value of 0.04xxxx - 0.06xxxx
Comment
Answer by Maniacbob · Jul 25, 2020 at 11:16 PM
Every frame your distanceTravelled variable is being reset. Try changing it to
float distanceTravelled += movingSpeed.Value * Time.deltaTime;
This will always add the new result value to your existing variable. Now when you print it, the number will continue to tick upward.