- Home /
Rigidbody move wrong pixel when framerate drop
Hi all, I move 2 sprite ( or more ) by change the velocity, all sprites follow the next one ( like snake with segment ). Each sprites has implement this code:
void FixedUpdate() {
GetComponent<Rigidbody2D>().velocity = transform.up * speed * Time.deltaTime;
}
And when the head turn right ( or left ), i will send the prev segment a message to turn in next time ( depend on velocity )
public void OnTurnRight()
{
// Rotate the current segment
transform.Rotate(Vector3.forward, -90);
// If this segment have prev segment, calculate the time and tell it.
if (prevSegment)
{
prevSegment.nextPos = transform.position;
Vector2 deltaPos = transform.position - prevSegment.transform.position;
Debug.Log(Time.fixedDeltaTime);
float time = deltaPos.magnitude/ speed / Time.fixedDeltaTime;
Invoke("TurnRightPreviousSegment", time);
}
}
public void TurnRightPreviousSegment()
{
if (prevSegment)
{
// before turn, make sure it have same position with the next segment
prevSegment.transform.position = prevSegment.nextPos;
prevSegment.OnTurnRight();
}
}
Everthing seems to be successful in framerate 60 or highter, but when the framerate drop low, 30 or above. Some sprites turn in wrong time and make sprites follow wrong position, that why i call it move wrong pixel.
Someone please help me solve this problem, sorry about my English. Thank you!
Answer by Mich_9 · Jan 10, 2016 at 05:55 AM
In your FixedUpdate function replace Time.deltaTime with Time.fixedDeltaTime
Thanks for your answer. I did it but still not fix this issues :(
Just a note regarding this from the documentation on deltaTime:
"When called from inside $$anonymous$$onoBehaviour's FixedUpdate, returns the fixed framerate delta time."
Although it can be considered proper form to use fixedDeltaTime in FixedUpdate(), it's not actually a requirement. In this case, there is no difference between deltaTime and fixedDeltaTime.