- Home /
Crashing after trying to rotate a GameObject
Hello, I have this piece of code :
void Start () {
maxLeftRotation = gameObject.transform.localEulerAngles = new Vector3 (0,0,25);
maxRightRotation = gameObject.transform.localEulerAngles = new Vector3 (0,0,-25);
defaultRotation = gameObject.transform.eulerAngles = new Vector3 (0,0,0);
}
void Update () {
if (Input.GetKeyDown (KeyCode.A)) {
for (float j=gameObject.transform.localRotation.z;j <= 25; j+= rotationSpeed){
gameObject.transform.Rotate(new Vector3 (0,0,j));;
}
} else if (Input.GetKeyDown (KeyCode.D)) {
for(float i = gameObject.transform.localRotation.z; i >= -25; i -= rotationSpeed){
gameObject.transform.localEulerAngles = new Vector3 (0,0,i);
}
} else{
gameObject.transform.localEulerAngles = new Vector3 (0, 0, 0);
if(gameObject.transform.localRotation.z < 0){
for(float i = gameObject.transform.localRotation.z; i != 0; i += rotationSpeed){
gameObject.transform.localEulerAngles = new Vector3 (0,0,i);
}
}
else{
for(float i = gameObject.transform.localRotation.z; i != 0; i -= rotationSpeed){
gameObject.transform.localEulerAngles = new Vector3 (0,0,i);
}
}
}
}
and every time I run the game and press a or d it crashes. I believe it's the code in the for statement wich makes the engine crash. The for loops are okay because everything works when I comment the gameObject.transform.localEulerAngles = new Vector3 (0,0,i); part. Thanks in advance
Answer by saschandroid · Sep 28, 2015 at 07:01 AM
I don't know if this causes the crash, but ... You are setting
i = gameObject.transform.localRotation.z
and use it as an euler angle, but localRotation is a Quaternion (with four components x, y, z and w).
It should be
i = gameObject.transform.localRotation.eulerAngles.z;
Your answer
Follow this Question
Related Questions
Problems caused by non-unique Euler angle solutions 1 Answer
How to get Angle float of specific axis.(Turret clamping related). 2 Answers
Set a relative (custom) speed to a rotation 1 Answer
Accurate placed object's transform messes up on game start 1 Answer
c# modify only one axis of a quaternion 2 Answers