• 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 Blkhwks19 · Dec 21, 2014 at 10:01 AM · rotationquaternion

Unity noob needs help rotating with Quaternions

I could use a bit of help getting start rotating with Quaternions.

Basically I'm building a 2d pinball game and I want to rotate the flipper when a key is pressed. The starting rotation is 0 on the Z axis and I would want that to increase as long as a key is held down, up to a max of 45 on the Z axis. When the key is released it would go back down from 45 to 0 on the Z axis.

I've looked into several of the Quaternion functions, but I guess I'm just not quite understanding it well enough to put the pieces together in this case. Can someone show me the proper function to use to achieve the results I'm looking for? That would really help me understand not only how it works but best practices as far as what the preferred method would be to accomplish something like this.

Thanks so much in advance!

Comment
Add comment · Show 7
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 Blkhwks19 · Dec 20, 2014 at 11:55 PM 0
Share

I tried that initially but I couldnt get it to work. Here's my code:

 public GameObject flipperLeftTop;
     
 private float flipper$$anonymous$$ax;
     
     
 // Use this for initialization
 void Start () {
     flipper$$anonymous$$ax = 45f;
 }
         
 // Update is called once per frame
 void Update () {
             
     if (Input.GetButtonDown("Left Flipper"))
     {
         if (flipperLeftTop.transform.rotation.z < flipper$$anonymous$$ax)
         {
             flipperLeftTop.transform.Rotate(Vector3.forward * Time.deltaTime);
         }
     }
 }


Nothing happens when I press the Left Flipper button (left shift key). At least with Quaternions I was seeing some results (weird angles and stuff, but at least it was something). So am I not using transform.Rotate() correctly then?

avatar image meat5000 ♦ · Dec 21, 2014 at 03:29 AM 0
Share

http://docs.unity3d.com/ScriptReference/Transform.Rotate.html

avatar image bobin115 · Dec 21, 2014 at 11:04 AM 0
Share

have you tries using animation it is alot easier as you don't have to worry about rotate transforms and time.

avatar image Blkhwks19 · Dec 21, 2014 at 04:07 PM 0
Share

Not yet but I might. I just figured rotating on one axis a bit is about the simplest kind of rotation you can do, so it shouldnt be this hard in code. Unfortunately, transform.Rotate() is not working for me. I guess I'll have to keep experimenting.

avatar image meat5000 ♦ · Dec 21, 2014 at 04:13 PM 0
Share

There are actually quite alot of Rotation methods. You find that if you require collisions, you'll end up using the rotations supplied by the rigidbody's functions.

Show more comments

1 Reply

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

Answer by Alec-Slayden · Dec 21, 2014 at 10:10 PM

It sounds like you're going to want to use euler angles instead of quaternions in this case.

The script example you posted in the comments probably isn't working because you're checking GetButtonDown() instead of GetButton(), which means it's just going to check the first frame a button was pressed down. Changing it to GetButton() will call the rotate method constantly, as desired.

However, while Transform.Rotate() is an acceptable solution, I think you would do better to have direct control of the rotation at every point, and it wouldn't take much more coding.

By keeping a variable to hold the transform's local euler angles, we can always quickly check or adjust them to suit a desire. With Rotate, we're telling it to perform an action, and storing no information about the result.

Here's one way to use a stored rotation value to control your flipper:

 using UnityEngine;
 using System.Collections;
 
 public class FlipperScript : MonoBehaviour {
 
     public Transform flipperLeftTop;

     public float FlipSpeed; // controls how fast the flipper moves when button is pressed
     public float ResetSpeed;// controls how fast it returns to rest position
     
     private float maxZ;
     private Vector3 flipperRotation = Vector3.zero; 

     // Use this for initialization
     void Start () {
         maxZ = 45f;
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetButton("Left Flipper") )
         {
         
             // update the stored z rotation value, limited to the max z, at an adjustable speed
             flipperRotation.z += Time.deltaTime * FlipSpeed;
             if (flipperRotation.z > maxZ) flipperRotation.z = maxZ;
         }
         else
         { 
                     // if the button isn't pressed, rotate it toward the rest position
             flipperRotation.z -= Time.deltaTime * ResetSpeed;
             if (flipperRotation.z < 0f) flipperRotation.z = 0f;
         }
         
         flipperLeftTop.localEulerAngles = flipperRotation; // set the transform to match our stored rotation.
     }
 }


I changed flipperLeftTop from GameObject to Transform, because you were only using the transform of it and not the game object, in the sample provided. This saves keystrokes and improves performance (since you're not fetching the transform every frame, it's already fetched) slightly.

The idea is to keep a local representation of the local euler angles of the object. The euler angles are the more familiar terms of rotation you'd be looking for. Setting local euler's z value to 20f, for example, will equate to 20 degrees of rotation around the z axis. Setting a quaternion value will not have the same effect.

By keeping the flipperRotation a local variable, you have complete control over it, and can assign it to localEulerAngles when ready. Before testing, make sure to set your FlipSpeed and ResetSpeed variables in the inspector, as leaving them at 0f will prevent the flipper from moving.

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 Blkhwks19 · Dec 22, 2014 at 10:23 PM 0
Share

Thanks so much, this was exactly the kind of info I needed to make sure I'm handling this situation properly. When I first approached this scenario I tried something similar, but I wasn't entirely sure which properties/methods to be using. So obviously, I just needed this nudge to say "hey, you were on the right track, just had things not quite set up properly". So glad to hear I was close, and definitely appreciate your optimization advice, as well as clarifying a few things. Those tips may not seem like much but they will go a long way towards helping me think in the Unity $$anonymous$$dset. Thanks again!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Change the roll of the rotation 0 Answers

How to ROTATE an object without slowing the ends (lerp) 3 Answers

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

How do I rotate a quaternion with euler angles? 0 Answers

Clamped Turret Doesn't Want to Lerp the Other Way 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