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!