- Home /
TRouble with quaternion
Hello im currently working on a project where i need a cube to rotate on command in specific directions for example i need the cube to rotate along its x axis to make the face to the left of the cube on the top but im having an issue where when i rotate the cube on its x and follow up rotating on its z it does a flip on the corner and ends up facing a completely unexpected way. I have a feeling it is to do with my little understanding of the quaternion values and functions
here is the code attach it to any cube and try flipping it vertically and follow up with a horizontal flip to see what i mean it doesn't happen all the time but if anyone has a fix i would appreciate it
using UnityEngine;
using System.Collections;
public class CubeRotate : MonoBehaviour {
public float speed = 55;
private float rotation;
Quaternion qTo;
void Update ()
{
if (Input.GetKeyDown ("w")) {
rotation -= 90;
qTo = Quaternion.Euler (rotation,0,0);
}
if (Input.GetKeyDown ("s")) {
rotation += 90;
qTo = Quaternion.Euler (rotation,0,0);
}
if (Input.GetKeyDown ("d")) {
rotation += 90;
qTo = Quaternion.Euler (0, 0, rotation);
}
if (Input.GetKeyDown ("a")) {
rotation -= 90;
qTo = Quaternion.Euler (0, 0, rotation);
}
transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, speed * Time.deltaTime);
}
}
Answer by Kiwasi · Jun 03, 2014 at 12:58 AM
I believe the problem is using += on rotation. This will have the effect of increasing the value of rotation by 90 every frame. However 360=0 so the target rotation will be going all over the place.
To confirm this problem you could use Debug.Log(rotation).
Solution is to change the += sign to an = sign.
If you need relative rotation you could use
rotation = transform.rotation.eulerAngles.x + 90;
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Transform a Vector by a Quaternion 1 Answer
Have an object continuously rotate towards another 1 Answer
Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer
Move and Rotate object with "Parent" without Parenting 1 Answer