• 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 unity_cfUW-mhVK73Mcg · Apr 27 at 01:34 PM · rotate objectrotation axisquaternions

Object doesn't rotate properly

So I have a cube that can be rolled to the right or forward. In order to avoid overshooting, I'm using Quaternion.RotateTowards to roll the cube. Here's the code:

 using UnityEngine;
 using System.Collections;
 public class CubeMovement : MonoBehaviour
 {
     public GameObject colliderPlane;
     Quaternion planeDefaultRotation;
     public AnimationCurve yValueCurve;
     float animationTime;
     float animCurveTime;
     void Start()
     {
         planeDefaultRotation = colliderPlane.transform.rotation;
         Keyframe lastKeyframe = yValueCurve.keys[yValueCurve.length - 1];
         animCurveTime = lastKeyframe.time + 1;
         animationTime = animCurveTime / 60;
     }
     public IEnumerator RollForward()
     {
         Vector3 finalPosition = transform.position + new Vector3(0, 0, 10);
         Quaternion finalRotation = transform.rotation * Quaternion.AngleAxis(90, Vector3.right);
         for (float currentTime = 0; currentTime < animationTime; currentTime += Time.deltaTime)
         {
             float currentFrameTime = (currentTime * animCurveTime) / animationTime;
             transform.position = new Vector3(transform.position.x, yValueCurve.Evaluate(currentFrameTime), transform.position.z + (10 * Time.deltaTime / animationTime));
             transform.rotation = Quaternion.RotateTowards(transform.rotation, finalRotation, 90 * Time.deltaTime / animationTime);
             yield return new WaitForEndOfFrame();
         }
         transform.position = finalPosition;
         colliderPlane.transform.position = transform.position;
         colliderPlane.transform.rotation = planeDefaultRotation;
         yield return null;
     }
     public IEnumerator RollToRight()
     {
         Vector3 finalPosition = transform.position + new Vector3(10, 0, 0);
         Quaternion finalRotation = transform.rotation * Quaternion.AngleAxis(90, Vector3.back);
         for (float currentTime = 0; currentTime < animationTime; currentTime += Time.deltaTime)
         {
             float currentFrameTime = (currentTime * animCurveTime) / animationTime;
             transform.position = new Vector3(transform.position.x + (10 * Time.deltaTime / animationTime), yValueCurve.Evaluate(currentFrameTime), transform.position.z);
             transform.rotation = Quaternion.RotateTowards(transform.rotation, finalRotation, 90 * Time.deltaTime / animationTime);
             yield return new WaitForEndOfFrame();
         }
         transform.position = finalPosition;
         colliderPlane.transform.position = transform.position;
         colliderPlane.transform.rotation = planeDefaultRotation;
         yield return null;
     }



If the cube only moves in one direction, the cube rotates properly. However if it changes to any other direction the cube doesn't rotate like it should. For example, if the cube rolls forward and in no other direction it rolls in the forward direction. However, if the player moves it forward and the moves it to the right , the cube rotates sideways. It seems these happens because the rotation axis change their position too

I've tried several methods (transform methods, Quaternion.FromToRotation, Quaternion.AngleAxis, animation curves...) but the problem still persists. Any ideas?

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
0

Answer by unity_cfUW-mhVK73Mcg · May 17 at 04:37 PM

I've fixed the issue, the thing is that each rotation changes the orientation of each rotation axis but I assumed that wasn't the case. To fix this, I now rotate an empty object that is the cube's parent. Once a roll is finished, I detach the cube from the parent, I reset the parent's orientation and reattach the cube. It works every roll.

In case someone has to deal with rotations, here's the code for rolling forward:

     IEnumerator RollForward()
     {
         Vector3 finalPosition = transform.position + new Vector3(0, 0, 10);
         Quaternion finalRotation = transform.rotation * Quaternion.AngleAxis(90, transform.right);
         for (float currentTime = 0; currentTime < animationTime; currentTime += Time.deltaTime)
         {
             float currentFrameTime = (currentTime * positionCurveTime) / animationTime;
             Vector3 nextPosition = new Vector3(transform.position.x, positionCurve.Evaluate(currentFrameTime), transform.position.z + (24 * Time.deltaTime));
             Quaternion nextRotation = Quaternion.RotateTowards(transform.rotation, finalRotation, 90 * Time.deltaTime / animationTime);
             transform.SetPositionAndRotation(nextPosition, nextRotation);
             yield return new WaitForEndOfFrame();
         }
         transform.position = finalPosition;
         cubeObject.transform.parent = null;
         transform.rotation = Quaternion.identity;
         cubeObject.transform.SetParent(transform);
         Vector3 selectionPlaneCenter = new Vector3(transform.position.x, transform.position.y + 5, transform.position.z);
         selectionPlane = new Plane(Vector3.up, selectionPlaneCenter);
         yield return null;
     }

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

136 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

Related Questions

How to steer a vehicle 1 Answer

Prevent child rotation but still control it yourself 1 Answer

How can I make a character look in a direction that's halfway between its direction of 2D movement and facing the camera? 1 Answer

Strange rotation pattern. 0 Answers

i need to rotate cube in z axis or x axis one direction at time 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