I made one 2 wheeled car with one caster wheel in front in Unity3d. After moving the car for some task, I want to bring it back to the initial position after pressing "STOP" button through Unity3d. I did the following for doing so.
createdRobot.rigidbody.angularVelocity = Vector3.zero;
createdRobot.rigidbody.velocity = Vector3.zero;
createdRobotL.rigidbody.angularVelocity = Vector3.zero;
createdRobotL.rigidbody.velocity = Vector3.zero;
createdRobotR.rigidbody.angularVelocity = Vector3.zero;
createdRobotR.rigidbody.velocity = Vector3.zero;
createdRobot.transform.rotation = Task1ResetPosition.rotation;
createdRobot.transform.position = Task1ResetPosition.position;
createdRobotL.transform.rotation = Task1ResetPosition.rotation;
createdRobotL.transform.position = Task1ResetPosition.position;
createdRobotR.transform.rotation = Task1ResetPosition.rotation;
createdRobotR.transform.position = Task1ResetPosition.position;
where createdRobot is by car base gameObject, createdRobotL and createdRobotR are my left and right wheel gameobject respectively. Both the wheels are connected with base through configurable joint.
Now, whenever I am doing the above operation in FixedUpdate my car is coming to initial position but it is in flipped in front side up form. I googled alot but didn't found the proper solution.
My createdRobot, createdRobotL, createdRobotR are the children of another gameobject called Robot which is not a rigid body.
Now, instead of doing above mentioned way to reset if I destroy the robot and create again using instantiate from prefab, my car is resetting properly as expected, but this method resets everything in the robot that I do not want.
GameObject.Destroy(GameObject.FindGameObjectWithTag("Robot_Tag"));
Instantiate(Robot,Task1ResetPosition.position,Task1ResetPosition.rotation);
Doing above thing, is making my car coming properly to the initial position. But if anyone tells me how to bring back the car to initial position without Instantiating it again then it would be appreciable.
Thanks.
Forcing rigidbodies to do stuff is always tricky as they're supposed to be driven by physics. I would try something like this (untested), for each of your rig parts:
part.rigidbody.Sleep();
part.rigidbody.velocity = Vector3.zero;
part.rigidbody.angularVelocity = Vector3.zero;
part.rigidbody.position = Vector3.zero;
part.rigidbody.rotation = Quaternion.identity;
part.rigidbody.WakeUp(); // might have to delay one frame there...
First, access the RB's position/rotation instead of the transform.
Then if that's not enough, force it to Sleep then WakeUp.
Also, you could try to set it to kinematic while you teleport it: part.rigidbody.isKinematic = true/false;