Hi I am working on a pretty simple walkthrough game. I am new to unity and I don’t have any great scripting skills yet so I hope you guys can help me out. There is two things that needs to be scripted.
The first one is a revolving door where you can walk through. I have animated the rotation for every 90 degrees until it is a full circle. I know you can trigger the animation but I just don’t know how yet. I also figured it could be done with the physics engine but I don’t know what works best.
The other thing I need is an elevator that works when the user pushes a button. I saw in the 2dplatform tutorial an example of the moving platform but how do I link this to a button?
The easiest solution would be to animate the revolving door using a script:
var speed : float = 5; // Degrees per second
function Update() {
transform.Rotate(0, speed * Time.deltaTime, 0);
}
Add a trigger (= a collider with “Is Trigger” enabled) to your button and then use a script like this one:
var elevator : GameObject = null;
private var elevatorScript : Script = null;
function Start()
{
// So we don't have to do this on every mouse up.
elevatorScript = elevator.GetComponent("ElevatorScript");
}
function OnMouseUp() {
elevatorScript.StartElevator();
}
“ElevatorScript” would be the name of your elevator script, StartElevator() should be the name of the function that sets the elevator in motion.
You’ll need to attach the elevator object you’re controlling to the elevator variable in the inspector.
hth,
Jim.
Thanks will have a crack at it. As for the door the default position has to be idle so I guess it needs a trigger as well?
Yep. If you want to make a revolving door that only revolves when the player is close, you’ll need a trigger. In this case, you should use the OnTriggerEvent() instead of OnMouseUp().
Uhm… just in case you’re not a troll, how about this:
Call me crazy but what does this have to do with this topic?
Or, you could make it a rigidbody object that is restricted to its position, as then it’d rotate when the player pushed into it.
I think if i make it a rigid the door doesn’t close in a nice 90 degree angle. What I basically need is; when the door is clicked, rotate y=90. Getting closer and closer but it’s hard for a visual guy like me. Whis scripting was like the node based Xpresso in C4D…
Try this:
static var kSpeed : float = 5; // Rotation speed in degrees per sec.
private var startRotation : Quaternion;
private var endRotation : Quaternion;
private var rotationT : float = 0.0;
function Start()
{
startRotation = transform.localRotation;
endRotation = startRotation + Quaternion.AngleAxis(Vector3.up, 90.0);
}
function Update()
{
if (rotationT > 0.0)
{
rotationT = Mathf.Max(rotationT - speed * Time.deltaTime, 0.0);
transform.localRotation = Quaternion.Lerp(endRotation, startRotation, rotationT); // Note: swapped to and from, 'cause I'm starting my rotation with rotationT = 1
}
}
function OnMouseUp()
{
if (rotationT <= 0.0) rotationT = 1.0;
}
The above should make the door turn a clean 90 degrees after a mouse click.
Note that using rotationT to count back from 1 to 0 is slightly counter-intuitive, but it saves me from having a separate isRotating boolean. Programmers like that sort of thing! 
I’ve tried your script but it says;
“Can’t add script Can’t add script behaviour Revolving Door. You need to fix all compile errors in all scripts first!”
The console gives me these;
Assets/Revolving Door.js(10,54): BCE0017: The best overload for the method ‘UnityEngine.Quaternion.AngleAxis(float, UnityEngine.Vector3)’ is not compatible with the argument list ‘(UnityEngine.Vector3, float)’.
Assets/Revolving Door.js(10,32): BCE0051: Operator ‘+’ cannot be used with a left hand side of type ‘UnityEngine.Quaternion’ and a right hand side of type ‘UnityEngine.Quaternion’.
Assets/Revolving Door.js(17,41): BCE0005: Unknown identifier: ‘speed’.
Thanks a lot for all your time Jim!
That’s what you get from typing code over coffee! 
Looks like I made a few typos. Here’s a version that should work better:
static var kSpeed : float = 5; // Rotation speed in degrees per sec.
private var startRotation : Quaternion;
private var endRotation : Quaternion;
private var rotationT : float = 0.0;
function Start()
{
startRotation = transform.localRotation;
endRotation = startRotation * Quaternion.AngleAxis(90.0, Vector3.up);
}
function Update()
{
if (rotationT > 0.0)
{
rotationT = Mathf.Max(rotationT - kSpeed * Time.deltaTime, 0.0);
transform.localRotation = Quaternion.Lerp(endRotation, startRotation, rotationT); // Note: swapped to and from, 'cause I'm starting my rotation with rotationT = 1
}
}
function OnMouseUp()
{
if (rotationT <= 0.0) rotationT = 1.0;
}
edit: removed bold tags from code, as that doesn’t seem to work
Thanks a million Jim. If you ever need some graphics or 3d models let me know.
I will. Thanks! (and glad I could help :P)
I noticed some things about the script. Is it possible that the door adds 90˚ everytime it spins? It seems that the door resets to 0˚ after a mouseclick. Another thing is from a different nature it seems that the collision doesn’t work with the rotation of the door but when it is in its default state it collides just fine.
It doesn’t add 90 after every spin (the start and end rotations are initialized in Start(), which is called only once). It does reset to 0 on a mouse click. To make it open and close, add this right after the Quaternion.Lerp() line in Update():
if (rotationT <= 0.0)
{
// Swap start and end rotation when we're done,
// so that on the next click the door returns to
// the original position.
endRotation = startRotation;
startRotation = transform.localRotation;
// note: transform.localRotation is equal to endRotation at this point.
}
I’m not sure why this is. Haven’t done much with moving colliders yet. A few things you could try:
a) Move the animation to FixedUpdate() instead (i.e. rename Update() to FixedUpdate())
b) Use a kinetic rigidbody instead of a collider.
Maybe one of the other blokes here can shed more light on the why and how of this problem.