- Home /
How to open and close a door parented to a rotating object?
I am using the following code in update to open and close a door by a specific number of degrees (eg. 45). These rotations are triggered by separate opening and closing booleans.
Quaternion openRotation = Quaternion.AngleAxis(doorOpenAngle, Vector3.up);
vehicleDoor.rotation = Quaternion.Slerp(vehicleDoor.rotation, openRotation, doorOpeningSpeed * Time.deltaTime);
Quaternion closeRotation = Quaternion.AngleAxis(doorCloseAngle, -Vector3.up);
vehicleDoor.rotation = Quaternion.Slerp(vehicleDoor.rotation, closeRotation, doorClosingSpeed * Time.deltaTime);
The doors are parented to an object. When this object is stationary, the above code works fine. However in most cases this object is moving and rotating, at which point it appears that the door rotation is occurring globally since it doesn't take into account the current rotation of the object. I also suspect that using Slerp is not the best way to go and that RotateTowards would be better in this case.
What should I do to my code to make the doors always open and close by a specific amount of degrees, irrespective of the current rotation of the object they are parented to?