- Home /
Time.deltaTime messing with object speed increase
Hey, I had a problem were when I exported my game, the object would move crazy fast, and I fixed that. Now it will not speed up, what SHOULD happen is once it gets to a point it turns around and goes back at 1.1 times the last speed, but now it only stays at 1.
transform.Translate(Vector3.right * (speed * Time.deltaTime));
///
if(transform.position.x >= 9.5 || transform.position.x <= -9.5)
{
transform.position = Vector3(transform.position.x,drop,transform.position.z);
speed = speed * -1.1;
}
Answer by Mike 3 · Mar 04, 2011 at 12:41 PM
Make sure that speed is a float, not an int, that'd stop it from increasing unless speed starts out at 10+
Since he says it stays at 1, I guess he has an integer set to 1.
yeah, all I had to do was change var speed = 1; at the top to var speed = 1.0; :D thanks all!
Answer by Statement · Mar 04, 2011 at 12:41 PM
To me it seems it should work. Do you have any other code that assign speed
elsewhere? Also check that your speed variable isn't an integer.
Answer by Bunny83 · Mar 04, 2011 at 02:02 PM
I see another problem beside what Mike and Statement already suggested.
If you reverse the speed when your object moves out of your limit (+- 9.5), it could happen that in the next frame the object is still outside your limits and it would reverse again.
To be on the save side check the speed value as well. I would check the two cases separately:
transform.Translate(Vector3.right * (speed * Time.deltaTime));
if(transform.position.x >= 9.5 && speed > 0 ) { speed = speed * -1.1; }
if(transform.position.x <= -9.5 && speed < 0 ) { speed = speed * -1.1; }
But you can put them in one condition:
if((transform.position.x >= 9.5 && speed > 0 ) || (transform.position.x <= -9.5 && speed < 0 ))
Your answer
Follow this Question
Related Questions
how to stop every thing and enter into the OnTriggerEnter function? 0 Answers
Time.deltaTime not consistent over time 1 Answer
Time.deltatime is returning 0? 1 Answer
Accumulating deltaTime oddity 1 Answer
Help With Quickest TimeScore 0 Answers