• 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
2
Question by James · Jan 05, 2010 at 10:35 PM · physicsrigidbody

How can I lock a rigidbody's angular velocity to one axis?

I have a 3D environment with 2D gameplay. The player controls a rocket with rotate and thrust within the XY plane. The physics-based gameplay necessitates the rocket being a rigidbody. I want it to roll off collision models within the environment, which it does perfectly. But when I hit something at a high velocity, the rocket will be rotated on its local X or Y axis, setting it on an oblong plane rather than the XY plane of the environment.

I thought this code would keep the rotation locked to the local Z axis, but after some testing, I can still disengage the rocket from the XY plane.

rigidbody.angularVelocity = Vector3.Scale(rigidbody.angularVelocity, Vector3(0, 0, 1));

What is the answer?

Here is the current script for the rocket:

var thrust : float = 1; var turnSpeed : float = 1;

private var rotation : float;

function Update () { //"rotation" is > or < 0 based on left and right arrows, multiplied by "turnSpeed" rotation = Input.GetAxis ("Horizontal") turnSpeed Time.deltaTime; }

function FixedUpdate () { //"vert" is > 0 when the up arrow or W is pushed var vert : float = Input.GetAxis ("Vertical");

 //Adds a force in the direction of the rocket's local Y+ coord when "vert" &gt; 0,    multiplied by "thrust"
 if (vert &gt; 0)
 {
     rigidbody.AddRelativeForce (Vector3(0, 1, 0) * thrust);
 }

 //Rotates rocket in local Z coord based on "rotation"
 transform.Rotate (0, 0, rotation);

}

Comment
Add comment · Show 1
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 Ehren · Jan 05, 2010 at 11:19 PM 1
Share

One suggestion: I'm not sure why you're getting the rotation input in Update and then applying it in FixedUpdate. I think that will cause you to overwrite the rotation a few times unnecessarily before you actually apply it in FixedUpdate. Ins$$anonymous$$d, simply calculate and apply the Horizontal input in FixedUpdate.

1 Reply

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

Answer by Ehren · Jan 05, 2010 at 11:06 PM

Use a Configurable Joint (see also the class documentation). If you attach it to your rigidbody, you can use it to limit the rotation around any of the axes. Simply set the "Angular X/Y/Z Motion" property to Locked for the axis you want to restrict.

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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

ConfigurableJoint Anchors and nested RigidBodies 2 Answers

Creating a SIMPLE car. 1 Answer

Why is my object tilting when moving? 2 Answers

Character Controller collision speed 0 Answers

swapping out an object on collision 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges