• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
12
Question by lisor15 · Dec 17, 2010 at 02:26 AM · rotationmovementphysicsrigidbodyaddforce

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
38
Best Answer

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.
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lisor15 · Dec 19, 2010 at 04:45 AM 0
Share

ok thx i'll try tt

avatar image
2

Answer by Reivaj28 · Aug 06, 2017 at 04:53 PM

alt text


screenshot-61.png (344.5 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Dec 17, 2010 at 04:08 PM 0
Share

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

avatar image dissidently · Dec 19, 2010 at 10:01 AM 0
Share

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

avatar image dissidently · Dec 19, 2010 at 10:06 AM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

making perpetual movement 1 Answer

Can you add a force to a rigidbody at an angle? 2 Answers

Add Force to the right of the rigidbody, not right of the screen 1 Answer

What is the best way to move the player with physics without using MovePosition? 0 Answers

Update speed and physics makes my rigidbody jiggle 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges