- Home /
Question by
Kobus · Jul 26, 2016 at 07:34 AM ·
scripting problemrotation axis
Rotation via script behaves unexpectedly
Hi. I created a house in Blender. In Unity I would like the player to be able to open and close the windows of the house when clicking on a window. I have set the pivot points for the windows to be at the top of the windows (the hinges are at the top and the windows open out and up. I have hidden all the other objects in order not to clutter the pictures.
When I rotate the window object in the Inspector I see that the rotation about the Z axis is what I am looking for. (See Picture 1)
However when I try and write a script to rotate the window the rotation goes funny (See Picture 2).
My script is as follows:
public class WindowScript : MonoBehaviour {
public bool isOpen = false;
public float windowOpenAngle = 250.0f;
public float windowCloseAngle = 180.0f;
public float movementSpeed = 2.0f;
private Quaternion targetRotation;
void Update()
{
if (isOpen)
{
//Window is open. Now close it.
targetRotation = Quaternion.Euler(0f, 0f, windowCloseAngle);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, movementSpeed * Time.deltaTime);
}
else
{
//Window is closed. Now open it.
targetRotation = Quaternion.Euler(0f, 0f, windowOpenAngle);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, movementSpeed * Time.deltaTime);
}
}
public void ChangeState()
{
isOpen = !isOpen;
}
}
What am I doing wrong here? I always get caught out when I deal when rotation. Any help will be appreciated.
picture-1-rotation-in-editor.jpg
(45.4 kB)
picture-2-rotation-in-script.jpg
(51.1 kB)
Comment