- Home /
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!
Edited the question to clarify what specifically I'm trying figure out
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).
Your answer
Follow this Question
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