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.

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.

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.