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
0
Question by Kawalyn · Jul 15, 2018 at 09:48 AM · rotationscripting problemcoroutinerotation axis

Rotating cube steadily around world axis not working properly

Hey,

im kind of stuck in a project im working on. Goal is to rotate a cube by 90° over a set amount of time when a directional key is pressed. I want to rotate it around the world axis as my camera is always facing in the forward z direction, directly facing the cube.

Im using a Coroutine to smoothly turn the cube which is almost working as intended - initally turning works perfectly but as soon as i for example rotate the cube upwards the next rightwards rotation is around the local axis of the cube in question.

Struggling to figure out where the problem is as to me it seems like it should rotate around the world axis from the code in the script >.<

Code blocks in question are below, thanks in advance!


The place where the coroutines are called:

 if(Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") > 0) 
 {
     if (rotationOnGoing == false)
     {
         // turn cube rightwards
         StartCoroutine(RotateCube(objHit, -90, Vector3.up, cubeTurnTime));
     }   
 }

 else if(Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") < 0)
 {
     if(rotationOnGoing == false)
     {
         // turn cube leftwards
         StartCoroutine(RotateCube(objHit, 90, Vector3.up, cubeTurnTime));
     }
 }

 else if (Input.GetButtonDown("Vertical") && Input.GetAxisRaw("Vertical") > 0)
 {
     if(rotationOnGoing == false)
     {
         // turn cube upwards
         StartCoroutine(RotateCube(objHit, 90, Vector3.right, cubeTurnTime));
     }
 }

 else if (Input.GetButtonDown("Vertical") && Input.GetAxisRaw("Vertical") < 0)
 {
     if (rotationOnGoing == false)
     {
         // turn cube downwards
         StartCoroutine(RotateCube(objHit, -90, Vector3.right, cubeTurnTime));
     }
 }

The coroutine handling the cube rotation over time:

     IEnumerator RotateCube(Transform objToRotate, float angle, Vector3 axis, float duration)
     {
         float rotationSpeed = angle / duration;
         float deltaAngle = 0;
 
         Quaternion startRotation = objToRotate.rotation;
         Debug.LogWarning(objToRotate.rotation);
 
         rotationOnGoing = true;
 
         if (angle > 0)
         {
             while (deltaAngle < angle)
             {
                 deltaAngle += rotationSpeed * Time.deltaTime;
                 deltaAngle = Mathf.Min(deltaAngle, angle);
                 objToRotate.rotation = startRotation * Quaternion.AngleAxis(deltaAngle, axis);
                 yield return null;
             }
         }
         else if (angle < 0)
         {
             while (deltaAngle > angle)
             {
                 deltaAngle += rotationSpeed * Time.deltaTime;
                 deltaAngle = Mathf.Max(deltaAngle, angle);
                 objToRotate.rotation = startRotation * Quaternion.AngleAxis(deltaAngle, axis);
                 yield return null;
             }
         }
         Debug.Log("Coroutine finished.");
         rotationOnGoing = false;
         yield break;
     }
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

0 Replies

· Add your reply
  • Sort: 

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

180 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 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 to turn a character frame independently? 1 Answer

i need to rotate cube in z axis or x axis one direction at time 0 Answers

Coroutine not stopping with rotation (stuck on infinite loop). 1 Answer

Bullet rifling simulation 1 Answer

Rotate object of certain degree on button press 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