- Home /
What is transform.Rotate doing internally when going beyond 360° or 0°?
Title says it all. The reason why I'm asking this is because I have Vector3 points which should follow the corner points of a cube. It's kinda working but there appear to be "2 sections of 360°". At the start everything works fine but if the rotation goes beyond 360° or below 0° the vectors rotate in the opposite direction of the cube until the rotation goes beyond 360° degrees again. using UnityEngine;
public class Move : MonoBehaviour {
float movespeed;
float rotspeed;
Vector3 pointA = new Vector3();
Vector3 pointB = new Vector3();
Vector3 pointC = new Vector3();
Vector3 pointD = new Vector3();
Vector3 pointE = new Vector3();
Vector3 pointF = new Vector3();
Vector3 pointG = new Vector3();
Vector3 pointH = new Vector3();
void Awake() {
// force 60 fps
Application.targetFrameRate = 60;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
movespeed = Input.GetAxisRaw("Vertical") * Time.deltaTime;
if (movespeed > 0) movespeed *= 4;
transform.Translate(0, 0, movespeed);
rotspeed = Input.GetAxisRaw("Horizontal") * Time.deltaTime * 60;
transform.Rotate(0, rotspeed, 0);
// initialize corner points of bounding box
pointA.Set(-0.5f, 0, -0.5f);
pointB.Set(0.5f, 0, -0.5f);
pointC.Set(0.5f, 0, 0.5f);
pointD.Set(-0.5f, 0, 0.5f);
pointE.Set(-0.5f, 1f, -0.5f);
pointF.Set(0.5f, 1f, -0.5f);
pointG.Set(0.5f, 1f, 0.5f);
pointH.Set(-0.5f, 1f, 0.5f);
// rotate around y-Axis with transform's y-rotation
pointA = Quaternion.AngleAxis(transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointA;
pointB = Quaternion.AngleAxis(transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointB;
pointC = Quaternion.AngleAxis(transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointC;
pointD = Quaternion.AngleAxis(transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointD;
pointE = Quaternion.AngleAxis(transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointE;
pointF = Quaternion.AngleAxis(transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointF;
pointG = Quaternion.AngleAxis(transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointG;
pointH = Quaternion.AngleAxis(transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointH;
// translate points to transform's position
pointA.x += transform.position.x;
pointB.x += transform.position.x;
pointC.x += transform.position.x;
pointD.x += transform.position.x;
pointE.x += transform.position.x;
pointF.x += transform.position.x;
pointG.x += transform.position.x;
pointH.x += transform.position.x;
pointA.y += transform.position.y - 0.5f;
pointB.y += transform.position.y - 0.5f;
pointC.y += transform.position.y - 0.5f;
pointD.y += transform.position.y - 0.5f;
pointE.y += transform.position.y - 0.5f;
pointF.y += transform.position.y - 0.5f;
pointG.y += transform.position.y - 0.5f;
pointH.y += transform.position.y - 0.5f;
pointA.z += transform.position.z;
pointB.z += transform.position.z;
pointC.z += transform.position.z;
pointD.z += transform.position.z;
pointE.z += transform.position.z;
pointF.z += transform.position.z;
pointG.z += transform.position.z;
pointH.z += transform.position.z;
// draw bounding box
Debug.DrawLine(pointA, pointB, Color.red, 0, false);
Debug.DrawLine(pointA, pointD, Color.red, 0, false);
Debug.DrawLine(pointB, pointC, Color.red, 0, false);
Debug.DrawLine(pointC, pointD, Color.red, 0, false);
Debug.DrawLine(pointA, pointE, Color.red, 0, false);
Debug.DrawLine(pointB, pointF, Color.red, 0, false);
Debug.DrawLine(pointC, pointG, Color.red, 0, false);
Debug.DrawLine(pointD, pointH, Color.red, 0, false);
Debug.DrawLine(pointE, pointF, Color.red, 0, false);
Debug.DrawLine(pointE, pointH, Color.red, 0, false);
Debug.DrawLine(pointF, pointG, Color.red, 0, false);
Debug.DrawLine(pointG, pointH, Color.red, 0, false);
Debug.Log(transform.eulerAngles.y);
}
}
I know how to fix this by checking in which "360° section" I am and writing it like this for the other "section":
pointA = Quaternion.AngleAxis(360f - transform.eulerAngles.y, new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z)) * pointA;
Thanks in advance!
Comment