- Home /
inconsistent results calculating the distance moved by an object
the results are inaccurate the the first print function always prints 6 but the second one in the WaitAndPrint method outputs inconsistent values once it is 13.54 once it's 13.4 . 13.38 and so on it never give a fixed value. And it is so essential to get the accurate distance the object moves in one second.
private float speed;
void Start(){
speed = 7.25f;
init();
}
public void init(){
print ("before"+transform.position.z);
StartCoroutine (WaitAndPrint ());
}
IEnumerator WaitAndPrint() {
yield return new WaitForSeconds(1f);
print ("after"+transform.position.z );
}
void Update() {
transform.Translate (Vector3.forward * Time.deltaTime * speed);
}
Answer by tanoshimi · Sep 21, 2016 at 06:46 PM
This is to be expected. Your game does not run at a fixed frame rate. WaitForSeconds means "wait for this amount of time and then continue execution in the next frame", so you're also including the variable frame execution time in your timing.
But there's an architectural problem here - I can't think of many games where a distance error of 0.15% should matter - and, if it does for you, you might have to rethink your approach.
can i fix the frame rate? i tried Application.targetFrameRate = 60; but i was getting the same results Or can i know the true distance the transform.translate method will move the object without using code. Yes it does as this i want the value to be 13.5 if it's below it will be impossible to pass the level if higher the level will be extremely easy.
No, you can set a fixed target frame rate. But that is just a target for the application to aim for. If you try to do calculations in Update() that take 20ms to execute, for example, it's simply impossible to maintain 60fps.
And you can't know the "true" distance moved by translate, since that varies each frame too.
thanks for your help :D , is there any way i could get around this as i said earlier the distance moved is the main core deter$$anonymous$$ing the difficulty so i cant just proceed in the fashion.
It's not clear to me what you're trying to do.
If you want the distance moved since the last frame, you can cache the position each frame and work it out.
If you want the distance moved in the next frame, you need to fix the distance rather than the speed, since you can't know how long the next frame's going to take.
Or you could just fix the distance you move the thing each frame (ie don't multiply by time.deltaTime which varies out of your control). Then you'd know the distance moved each frame because you'd be setting it directly using variables you have total control over, but you'd lose the smoothness of the motion. You can't have both - smooth motion means dealing with varying and unpredictable frame rate.
i want to get the distance moved since the last frame.when i cache the positions i dont get an accurate value as well. my main problem is that the main object which i want to move has a child object which jumps in a time of 0.88 seconds so i want to know in the 0.88 seconds how far my main object moved.
Your answer
Follow this Question
Related Questions
Please help. I can't stop this light from moving!!! 1 Answer
using coroutines for move objects - performance. 1 Answer
Flash image while ammo is low - coroutines? 3 Answers
Coroutine in Editor? 7 Answers