• 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
Question by 1eleven · May 05, 2013 at 10:54 PM · rotationjavascriptphysicsrigidbodynoob

can anyone help me stop a rigidbody from rotating around its z axis

I have been working on creating a script that rotates a object using the mouse along it's y and x axis but i want it's z axis to return to zero after turning, does any one have any suggestions.

this is what i have so far.

   #pragma strict
     
     var xspeed = 4.0;
     var yspeed = 4.0;
     //var zspeed = 4.0;
     
     
     
     function FixedUpdate ()
     {
     //Ignore this stuff it was leftovers from my earlier attempts//
     //    var z : float;
     //    if(transform.eulerAngles.z > 0.5f)
     //    {
     //        z = -zspeed;
     //    }
     //    if(transform.eulerAngles.z < -0.5f)
     //    {
     //        z = zspeed;
     //    }
         
         var x = xspeed * Input.GetAxis("Mouse Y");
         var y = yspeed * Input.GetAxis("Mouse X");
         rigidbody.AddTorque(x, y, z);
     //    Debug.Log(z);
         
     }
Comment
GeekyMonkey

People who like this

1 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by gharbill · May 06, 2013 at 01:21 AM

A better option is to use Transform.Rotate(x,y,z) since using torque does not give you instant rotation as you have to wait for angular velocity to increase.

Your code should be like this:

 var x = xspeed * Input.GetAxis("Mouse Y");
 var y = yspeed * Input.GetAxis("Mouse X");
     
 transform.Rotate(xspeed,yspeed,0);

If you use a rigidbody u can constrain any axis rotation using inspector if you dont want any unwanted behaviour

Hope that helps...

Comment

People who like this

0 Show 2 · 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 1eleven · May 06, 2013 at 04:34 AM 0
Share

The problem i was having with this fix before was that when i was testing constant forces it moved wrong, traveling in a straight line even when i turned, Since i am using this in a a flight game i was planning on handling speed with the built in physics. i was wondering if there was a better way of changing velocity?

avatar image gharbill · May 06, 2013 at 03:19 PM 0
Share

then you can use Rigidbody.angularVelocity to change angular velocity using physics. it would be like this :

 Rigidbody.angularVelocity = Vector3(xspeed,yspeed,0);

and remember that the axis that you change in code is not always what you expect to be changed in editor depending on world or local coordination being used. so just play with x,y,z to find out which one should be zero.

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Clamp Bike Rotation 0 Answers

Engine weight Simulation 2 Answers

A way to kill rotation made by rigid body pysics? 1 Answer

Ball getting stuck in surfaces and bouncing for no reason? Help! 0 Answers

How to set a limit for my player rotation ? 0 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