Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JustinTheSwift · Nov 14, 2017 at 10:49 PM · rotationquaternioneuler

Rotate an object around z axis on user input

I'm trying to rotate a 2D object around its z-axis when the user presses left or right. For some reason, my object only turns partway before it sticks and goes no further (the angle it sticks at depends on the speed).

It also turns back to zero if I let go of the key, which I don't want.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class player : MonoBehaviour {

 public int speed;

 void Start () {
     speed = 100;
 }
 
 void Update () {
     //Check to see if there's input
     if (Mathf.Abs(Input.GetAxis ("Horizontal")) > 0) {        

                     //Set a rate at which we should turn
             float turnSpeed = speed * Time.deltaTime;       

                     //Connect turning rate to horizonal motion for smooth transition
                     float rotate = Input.GetAxis ("Horizontal") * turnSpeed;        

                     //Get current rotation
         float currentRotation = gameObject.transform.rotation.z;

                     //Add current rotation to rotation rate to get new rotation
         Quaternion rotation = Quaternion.Euler (0, 0, currentRotation + rotate);

                     //Move object to new rotation
         gameObject.transform.rotation = rotation;
     }
 }

}

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

1 Reply

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

Answer by FabDynamic · Nov 15, 2017 at 03:21 AM

Update: Hey I figured out an even smaller modification to your code! I suspect you knew that your code was close and boy were you right! 12 characters away from perfect to be precise!

Change this line from:

 float currentRotation = gameObject.transform.rotation.z;
 

To:

 float currentRotation = gameObject.transform.rotation.eulerAngles.z;    


Rationale: Apparently there is a z value of the rotation but this is not the z you are looking for ;). Its apparently the Z value of the quaternion directly and those are way above my pay grade (so to speak) so I'm not even going to try to explain what that means but I know that modifying those directly lets us easily shoot ourselves in the foot.

That should do the trick (it did for me!)

Original Answer: Try this small modification. I just commented out a few lines and added one new line which uses the built in function Transform.Rotate() which I think is just what you are looking for. It also uses the pre-defined Vector3.Forward which in this context represents the Z axis for us.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ARARotatingAroundZ : MonoBehaviour {
     public int speed = 100;
 
     void Update () {
         //Check to see if there's input
         if (Mathf.Abs (Input.GetAxis ("Horizontal")) > 0) {        
             //Set a rate at which we should turn
             float turnSpeed = speed * Time.deltaTime;       
             //Connect turning rate to horizonal motion for smooth transition
             float rotate = Input.GetAxis ("Horizontal") * turnSpeed;  
 //            //Get current rotation
 //            float currentRotation = gameObject.transform.rotation.z;
 //            //Add current rotation to rotation rate to get new rotation
 //            Quaternion rotation = Quaternion.Euler (0, 0, currentRotation + rotate);
 //            //Move object to new rotation
 //            gameObject.transform.rotation = rotation;
             gameObject.transform.Rotate (Vector3.forward * rotate);
         }
     }
 

The final line rotates around the z axis by the magnitude that your code already correctly calculated.

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

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

Quaternion LookRotation issue 0 Answers

Quaternion.euler gives me glitch. Why? 1 Answer

Gun not rotating with the camera? 1 Answer

Limiting the camera's rotation using quaternions, camera sticking to top/bottom limit 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