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 bluezumbrellas · Dec 21, 2013 at 04:33 PM · rotationcoroutineslerp

Issue With Rotation on Key Press

Hi There,

I'm trying to make a 3D cube rotate to another face based on the currently visible face (in 2D view). I can go from red -> neutral -> green and in reverse, and red -> green, however trying to go from green - > red does not value. On entering the green face, it's X rotation becomes -5.008956e-06 and it's Y and Z become 180. Interestingly the co-routine still runs, changing the current face at end.

 using UnityEngine;
     using System.Collections;
      
     public class RotationScript : MonoBehaviour {
      
         public enum gameColors{Red,Green,Neutral};
         
         public gameColors charColor;
         
         public float redRotation = 0.0f;
         public float greenRotation = 180.0f;
         public float neutralRotation = 90.0f;
         
         // Use this for initialization
         void Start () {
         
         }
         
         IEnumerator RotateCube(Quaternion rotationTarget, float rotateTime, gameColors targetColor)
         {
             float elapsedTime = 0.0f;
             
             Quaternion startingRotation = transform.rotation;
             
             while (elapsedTime < rotateTime)
             {
                 elapsedTime += Time.deltaTime;
                 
                 transform.rotation = Quaternion.Slerp(startingRotation,rotationTarget, (elapsedTime / rotateTime));
                 
                 yield return new WaitForEndOfFrame();
             }
             
             charColor = targetColor;
         }
         
         // Update is called once per frame
         void Update () {
         
             if (Input.GetKeyDown(KeyCode.W))
             {
                 if (charColor == gameColors.Red)
                 {
                     Quaternion tarRotate = Quaternion.Euler(new Vector3(neutralRotation,0.0f,0.0f));
                     StartCoroutine(RotateCube(tarRotate,1.0f,gameColors.Neutral));
                 }
                 if (charColor == gameColors.Neutral)
                 {
                     Quaternion tarRotate = Quaternion.Euler(new Vector3(greenRotation,0.0f,0.0f));
                     StartCoroutine(RotateCube(tarRotate,1.0f,gameColors.Green));    
                 }
             }
             
             if (Input.GetKeyDown(KeyCode.S))
             {
                 if (charColor == gameColors.Neutral)
                 {
                     Quaternion tarRotate = Quaternion.Euler(new Vector3(redRotation, 0.0f,0.0f));
                     StartCoroutine(RotateCube(tarRotate,1.0f,gameColors.Red));
                 }
                 if (charColor == gameColors.Green)
                 {
                     Quaternion tarRotate = Quaternion.Euler(new Vector3(neutralRotation,0.0f,0.0f));
                     StartCoroutine(RotateCube(tarRotate,1.0f,gameColors.Neutral));
                 }
             }
             
             if (Input.GetKeyDown(KeyCode.X))
             {
                 if (charColor == gameColors.Red)
                 {
                     Quaternion tarRoate = Quaternion.Euler(new Vector3(greenRotation,0.0f,0.0f));
                     StartCoroutine(RotateCube(tarRoate, 1.0f,gameColors.Green));
                 }
                 
                 if (charColor == gameColors.Green)
                 {
                     Quaternion tarRotate = Quaternion.Euler(new Vector3(greenRotation,0.0f,0.0f));
                     StartCoroutine(RotateCube(tarRotate, 1.0f,gameColors.Red));
                     Debug.Log("hello");
                 }
             }
         }
     }

I'm getting the same values pre-baking the rotations with animations, and using iTween. Why is the cube not rotating for the green -> red transition, and why is it reporting such an unusual value? I assume the green position would be 180 degrees Any help would be appreciated!

Comment
Add comment · Show 1
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 bluezumbrellas · Dec 21, 2013 at 04:49 PM 0
Share

Edited the question to clarify what specifically I'm trying figure out

1 Reply

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

Answer by robertbu · Dec 21, 2013 at 05:10 PM

On line 78, 'greenRotation' should be 'redRotation' (assuming your code is following the pattern of the rest of your script). That's why your cube is not changing from green to red.

Euler angle reported in the Inspector are derived from the Transform.rotation which is a Quaternion. There are multiple euler angle representations for any given physical rotation, so (180,0,0) is the same physical rotation as (0,180,180).

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

18 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

Related Questions

Slerp Finishes Rotation When Progress is 0.5f 1 Answer

Issue with Slerping 1 Answer

Activating 3 rotations based on multiple presses on one key 2 Answers

Slerp doesnt go to the rotation its supposed to go to 1 Answer

Use Lerp to rotate object 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