- Home /
Can't set position of Rigidbody precisely
I'm trying to capture and restore the state of a Rigidbody with a couple of colliders for the purpose of doing rollback in a multiplayer game. Unfortunately, this seems impossible.
Even something as simple as this won't work:
//
var position = rigidbody.position;
rigidbody.position = position;
Debug.Log(rigidbody.position.Equals(position)); // Will mostly log "False"!
//
In other words: Asking the Rigidbody for its position and then setting exactly that position will result in a (slightly) different position. That makes rolling back impossible :-(
It seems like it only happens when there are multiple colliders attached to the Rigidbody.
Any ideas what might cause this or how it can be fixed?
You should avoid using .Equals
, use ==
ins$$anonymous$$d per the documentation:
https://docs.unity3d.com/ScriptReference/Vector3.Equals.html
@TreyH No, Equals is correct here because I want to make sure that the values are exactly the same, not just within some tolerance. Even slight differences quickly lead to a completely different simulation result.
Floating point equality comparison isn't guaranteed, that's just part of life.
If you need control over that tolerance and don't trust Unity's, then implement it yourself, but you can't get around machine epsilon or floating point inequality.
@TreyH I am well aware of floating point intricacies, but I don't think this is the problem here. When you assign a value to itself you can expect to get that value back. Seems like when you set a Rigidbody's position, there's a lot going on behind the scenes ...
I don't know much about Unity's networking, is there some syncing issue maybe? In an offline editor, even .Equals
is only true for me when doing self-assignment.
No networking is needed to reproduce the problem. All I want to do is to fully restore a Rigidbody's state including position, rotation, velocity and angular velocity. I was assu$$anonymous$$g that if I remember these properties, I can restore them later on to rewind the simulation. If it was just a simple assignment, that would work. But Unity does something behind the scenes, some calculations, and then precision is lost ... Then playing back the simulation will lead to a different result.
Your answer
Follow this Question
Related Questions
Rotate child object under RigidBody parent 0 Answers
Rigidbody climbing stairs 2 Answers
Move rigidbody to position and still have forces act on it 0 Answers
VR Sword Physics, how to add force or velocity to the sword for impact when it hits an object 1 Answer
Rigidbody NavMeshAgent Causing Crazy Collisions with Player. 0 Answers