0

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.

Niks
  • 57
  • 2
  • 8

1 Answers1

0

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;

benblo
  • 877
  • 8
  • 18
  • I am already setting base rig as kinematic before teleporting it. Without setting it kinematic if I teleport, the entire body stopped at its current place without any movement. As I explored more, what I found is my base part design orientation is "UPSIDE DOWN" so that might be affecting. Do you know any way of rotating rig after fixing it to a certain position. – Niks Jul 05 '14 at 13:55
  • Is there any way to know, how Instantiate is working internally. – Niks Jul 05 '14 at 14:42
  • Well Instantiate just creates new GameObjects with the components and properties of the original prefab, so they know nothing of the previous state of the object you've destroyed, they're created fresh. Using destroy+instantiate is clearly a hack and not a proper solution. – benblo Jul 08 '14 at 07:14