- Home /
Prevent Rigidbody From Rotating
Hi all, I am creating a 2D top-down rescue game. Where the player has to go around rescuing civilians using arrow keys.
Both the player and the civilians share the same script. Both have rigidbodies attached to them. Basically I check for the 'player' tag when using script on the player.
Anw, currently I'm doing this to move the rigidbodies:
inputMovement = objPlayer.transform.position - transform.position;
rigidbody.AddForce(inputMovement.normalized*moveSpeed*Time.deltaTime); transform.position = new Vector3(transform.position.x, 0, transform.position.z);
But if the civilian hits anything, it'll spin.
I tried using Is Kinematic or rigidBody.MovePosition()
, but both player and civilian won't respond to anything.
Thanks in advance for your help.
I am also open to any other workarounds.
Answer by Statement · Dec 17, 2010 at 02:34 AM
I take it you don't want any rotations at all?
You can check the checkbox "Freeze Rotation" on your rigid bodies through the editor. This will stop rotation all together. Alternatively you can set it through script:
// Freeze the rotation
rigidbody.freezeRotation = true;
- Note that you still can rotate the object. This only disables rotation from physics simulation responses as described in the scripting reference for Rigidbody.freezeRotation.
Answer by dissidently · Dec 17, 2010 at 03:39 PM
This is going to sound strange, but it works. Add a configurable Joint to the civilian, and don't connect the joint to anything, but do add the following settings to the joint to stop ALL rotation:
civilian.GetComponent(ConfigurableJoint).angularXMotion = ConfigurableJointMotion.Locked; civilian.GetComponent(ConfigurableJoint).angularYMotion = ConfigurableJointMotion.Locked; civilian.GetComponent(ConfigurableJoint).angularZMotion = ConfigurableJointMotion.Locked;
If you're top down, you may want to free up one axis for rotation so you can have the civilian rotate to face the direction he's heading, just set the desired X,Y or Z to .Free; depending on which way your cameras making the desired axis.
This is strange, I think, because configurable joint is useful when not connected to anything.
You can also use the configurable joint to lock out Z-movement from your camera view of the object if you're making a 2D game and this becomes a problem when they get smacked by physics events. Assuming Z is depth from camera view: GetComponent(ConfigurableJoint).zMotion = ConfigurableJointMotion.Locked;
isKinematic stops all responses to physics events, allowing you only control via direct changes to the objects transform, however other objects when hitting your isKinematic object respond to it physically.
No need to find it strange. That's the intended behavior when not connecting it to any particular object. It's said to be "connected to the world". See http://unity3d.com/support/documentation/ScriptReference/Joint-connectedBody.html for more informaion
WOW. Statement is the coolest nickname. And you've dug up a brilliant example of the deficiencies in the Unity references. On your link is a sentence that should be on EVERY page that talks about joints. "If not set, the joint connects the object to the world." I cannot believe it's hidden away on a page that nobody's ever gonna goto. That is f#$king imperative information about the handling of joints. Here's one perfect page to put it on, in brackets, after the description of connectedBody. http://unity3d.com/support/documentation/ScriptReference/Joint.html
and just because I can be more anal... where does it connect the object to in the world? In exactly the current position of the objects transform? To world (0,0,0) regardless of objects current position? What happens if the object moves? Does the joint move with it, or stay where the original transform was? Is this "virtual" connection point of the joint contactable through script? Editable through script?