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
1
Question by tigshadorakie · Feb 27, 2014 at 08:41 PM · rotationquaternionunderstanding logic

Really Confused about Scripting Rotations

Hello everyone. I am new to Unity and 3D game development and have been messing around with creating controls for an object(ie Spacecraft in 3D space). I have been making some great progress but I am really stuck on something. Here is what I am confused about.

Basically what I am am trying to do is get the object to rotate a certain number of degrees left or right the same way you would by changing the numbers in the inspector panel. When you change the numbers in the inspector panel the object's rotation changes(tilts). I would like to do the same thing in script when certain keys are pressed. No matter what I have used, it seems like the object always "orbits" a center rather than just changing its orientation like it does in the inspector panel. Can someone point me in the the right direction and tell me which function I need to use to get the same result in script that you do if manually changing the rotation numbers in Inspector panel. Thank you in advance for any help you can offer.

Comment
Add comment · Show 2
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 Jamora · Feb 27, 2014 at 09:50 PM 1
Share

Unity uses quaternions to represent rotation. Have a look at the UnityGems explanation of Quaternions.

avatar image tigshadorakie · Feb 27, 2014 at 10:51 PM 0
Share

Thanks. I will look that over later.

2 Replies

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

Answer by ChrisSch · Feb 27, 2014 at 09:42 PM

Yes rotation can be confusing, especially for beginners. I never really tried anything like it before but here's what I cooked up so fast. If you just want to snap to those rotations as if you're writing it in the inspector, I think what you're looking for is euler angles.

https://docs.unity3d.com/Documentation/ScriptReference/Quaternion-eulerAngles.html

Here's a quick code that will allow you to add for example 30 degrees rotation on Y axis every time you press left or right, so it rotates the object by 30 degrees to the left or right.

 var rotationDegrees : int = 30;
 
 function Update()
 {
     //Change GetKeyDown to GetButtonDown, I just used keys for simplicity's sake.
     //But any controls you want to be changed by the player use buttons not keys.
     if(Input.GetKeyDown("a")) //To the left
     {
         var leftRotation = transform.rotation.eulerAngles;
         transform.rotation.eulerAngles = Vector3(0, -rotationDegrees, 0) + leftRotation;
     }
     else if(Input.GetKeyDown("d")) //To the right
     {
         var rightRotation = transform.rotation.eulerAngles;
         transform.rotation.eulerAngles = Vector3(0, rotationDegrees, 0) + rightRotation;
     }
 }

If you wanna rotate on a different axis just put the "rotationDegrees" into one of the other axis in the "Vector3(x,y,z)" part.

But if you want to just rotate it to 30 or 170 or whatever exactly as if typing in inspector then this code should do the trick.

 var rotationDegrees : int = 30;
 
 function Update()
 {
     if(Input.GetKeyDown("r"))
     {
         transform.rotation.eulerAngles = Vector3(0, rotationDegrees, 0);
     }
 }
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 ChrisSch · Feb 27, 2014 at 10:59 PM 0
Share

@tigshadorakie

I probably confused you with leftRotation and rightRotation, it isn't actually left or right I might as well typed in rotation1 and rotation2, because it just gets your current rotation vector and adds the desired rotation vector in the next line.

Also Jamora gave you a great link! Definitely read that as eulers aren't really suited for more complicated rotations, you'll run into problems. So definitely take a look at that link.

avatar image
1

Answer by DanielRS · Feb 27, 2014 at 10:44 PM

There's a chapter about this in Unity's project tutorials Space Shooter: 6. Moving the player. It explains how to "tilt" the ship to the left or right depending on player input.

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 tigshadorakie · Feb 27, 2014 at 10:51 PM 0
Share

Thanks. Will take a look.

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform a Vector by a Quaternion 1 Answer

Calculating Torque but cant add Quaternion 0 Answers

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers

player rotation to follow the mouse 2 Answers

2D animation : problem with rotation interpolation 1 Answer


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