Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by AlucardJay · Mar 22, 2012 at 09:22 AM · rotationrigidbodyquaternioneuler

am I using degrees, radians, rotation or quaternion ?

I'm trying to rotate a rigidbody on the Y-axis using MoveRotation , Input "Horizontal" and time, but cannot see where this is breaking.

Steps :

make a target rotation , make it equal to this.transform.[current rotation in degrees]

increase or decrease target rotation's Y (in degrees) by Input "Horizontal" and deltaTime

'snap to target rotation - euler angles degrees' with rigidbody.MoveRotation(target rotation);

Script :

 var targetRotAngle : Quaternion = Quaternion.Euler (0, 0, 0);
 var turnSpeed : float = 10.0;
 
 function FixedUpdate () {    
     targetRotAngle = Quaternion.Euler (0, transform.eulerAngles.y, 0);
     targetRotAngle.y += Input.GetAxis("Horizontal") * Time.deltaTime * turnSpeed;    
     rigidbody.MoveRotation(targetRotAngle);    
 }
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

2 Replies

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

Answer by aldonaletto · Mar 22, 2012 at 02:24 PM

As @syclamoth said, the quaternion components x,y and z have nothing to do with the familiar Euler angles - don't mess with them directly!
I would use a trick similar to that used in Orient vehicle to ground normal: keep the desired angle about Y in a variable, and assign Quaternion.Euler(0, angle, 0) to transform.rotate - or pass it to rigidbody.MoveRotation():

var targetRotAngle: float = 0.0; var turnSpeed : float = 10.0;

function Update () {
targetRotAngle += Input.GetAxis("Horizontal") Time.deltaTime turnSpeed;
transform.rotation = Quaternion.Euler(0,targetRotAngle%360,0);
} I moved the code to Update and used transform.rotation because Input is synced to the update cycle - reading it in FixedUpdate may cause double or triple rotation increments. If you really need to use rigidbody.MoveRotation, split the Input reading and MoveRotation:

var targetRotAngle: float = 0.0; var turnSpeed : float = 10.0;

function Update () { // read the input in Update and apply to targetRotAngle: targetRotAngle += Input.GetAxis("Horizontal") Time.deltaTime turnSpeed;
}

function FixedUpdate(){ // rotate the rigidbody in FixedUpdate: rigidbody.MoveRotation(Quaternion.Euler(0,targetRotAngle%360,0));
} NOTE: Pass the angle in modulo 360, since angles out of range may produce weird results.

Comment
Add comment · 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 AlucardJay · Mar 22, 2012 at 03:19 PM 0
Share

Thanks for posting back. And reading over to this question . And showing a method of storing my target angle in a var after being separately calculated, then applying it.

All my program$$anonymous$$g is self-taught from forums, I appreciate all the replies. With some previous very basic use of Flash (AS2), I am blown away by what I have made with Unity in 3 months.

O$$anonymous$$, all of my rotation questions are co$$anonymous$$g from one standard I picked up : transform is used on gameObjects ; force and torque is used on rigidbody's .

$$anonymous$$y basic understanding was : to get real physics effect (colliding with other objects) I had to use rigidbody's , and manipulate them with force and torque in a FixedUpdate loop. Also using addForce And transform was wrong, and could lead to strange consequences with the physics engine.

SO the root question I have finally distilled to is : how to I correctly manipulate a rigidbody's angle of an axis to a specific angle over input and time ?

Saying that, I guess I should be applying torque, and when target angle is reached apply opposite torque. But on another project (pinball), it was suggested to me that Slerp was better than torque , with all the additional issues of dampening or slowing to target angle.

After 2 days of thinking about this problem, I have also discovered another command : Quaternion.FromToRotation .

Or should I never have gone down this thought path and just Slerp my rigidbody's ? (or would that re-create my gimble lock problem...) Sorry guys, but I feel like I'm stuck in a perpetual loop with one silly little problem over 3 posted questions . Again, all the time given by yourselves and others on this forum have greatly helped me, so the replies are much appreciated.

avatar image aldonaletto · Mar 22, 2012 at 03:42 PM 1
Share

Rigidbodies are hard to position precisely: they are good for things like obstacles, projectiles and other things that will be thrown, hit, launched or fall to the ground. I would use a CharacterController ins$$anonymous$$d: you can move and rotate it more precisely with $$anonymous$$ove (see example in http://unity3d.com/support/documentation/ScriptReference/CharacterController.$$anonymous$$ove.html). The question I've mentioned about following the terrain normal used a CharacterController to control a vehicle's behaviour.

avatar image
1

Answer by syclamoth · Mar 22, 2012 at 02:01 PM

Pretty much, 'targetRotAngle' is a Quaternion, not a 3-axis rotation. Quaternions are to complex numbers what complex numbers are to real numbers, but don't let that worry you- all you really need to know is that anything you can't do with the inbuilt Quaternion library, you should ask a mathematician about. Modifying the values directly- right out. Don't do it unless you really understand what quaternions are.

Just because it's created with the '.Euler' factory, doesn't mean it's a euler angle on the inside.

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 AlucardJay · Mar 22, 2012 at 03:18 PM 0
Share

Thanks for posting back. All my program$$anonymous$$g is self-taught from forums, I appreciate all the replies. With some previous very basic use of Flash (AS2), I am blown away by what I have made with Unity in 3 months.

O$$anonymous$$, all of my rotation questions are co$$anonymous$$g from one standard I picked up : transform is used on gameObjects ; force and torque is used on rigidbody's .

$$anonymous$$y basic understanding was : to get real physics effect (colliding with other objects) I had to use rigidbody's , and manipulate them with force and torque in a FixedUpdate loop. Also using addForce And transform was wrong, and could lead to strange consequences with the physics engine.

SO the root question I have finally distilled to is : how to I correctly manipulate a rigidbody's angle of an axis to a specific angle over input and time ?

Saying that, I guess I should be applying torque, and when target angle is reached apply opposite torque. But on another project (pinball), it was suggested to me that Slerp was better than torque , with all the additional issues of dampening or slowing to target angle.

After 2 days of thinking about this problem, I have also discovered another command : Quaternion.FromToRotation .

Or should I never have gone down this thought path and just Slerp my rigidbody's ? (or would that re-create my gimble lock problem...) Sorry guys, but I feel like I'm stuck in a perpetual loop with one silly little problem over 3 posted questions . Again, all the time given by yourselves and others on this forum have greatly helped me, so the replies are much appreciated.

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Calculating Torque but cant add Quaternion 0 Answers

Quaternion.Slerp and Quaternion.Euler problem 1 Answer

Quaternion.euler gives me glitch. Why? 1 Answer

Quaternion.Slerp problem... 1 Answer

How to get rigidbody local z axis rotation in Euler angles? 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