- Home /
Revolving door
I'm new to unity, and am trying to figure a way to create a revolving door, so that when my player runs into it, the door will rotate 90 degrees as the player goes forward. I tried creating a hinge joint on the object, but when my player runs into one of the walls on the revolving door it spins on a bunch of axes. I was wonder wether the best way to do this would be using a script with OnCollisionEnter or OnTrigger or something else.
Thanks
Answer by NickP_2 · Feb 05, 2014 at 10:37 PM
The OnTriggerEnter should work great. If you want to make it go open from a distance, you could make an if statement with a [Vector3.Distance][1] and trigger it when the distance is lower than the distance you want.
To rotate, you can simply use [Quaternion.RotateTowards][2]:
For example:
public float speed = 5, distance = 5;
public Vector3 rotateAngle = New Vector3(0, 90, 0);
void Update() {
if(Vector3.Distance(player.transform.position, transform.position) < distance)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, transform.rotation + rotateAngle, speed * Time.DeltaTime);
}
}
Comment if there's an error, i didn't check it :) [1]: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Distance.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.RotateTowards.html
Your answer
Follow this Question
Related Questions
Rotating Door 2 Answers
Always turning left in Unity games 0 Answers
isTrigger door issues 1 Answer
Transparent part of wall with door(2d) 1 Answer
How to find a rotation of a child object 0 Answers