Search Unity

How to make a door open ;D

Discussion in 'Scripting' started by Xcellian, Mar 3, 2010.

  1. Xcellian

    Xcellian

    Joined:
    Mar 3, 2010
    Posts:
    17
    If someone could teach me how t make a door open when you press e on the keyboard that would be great :D
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Firstly, you should position the door model's anchor point so that one of the axes (usually the Y axis) is in line with the hinges. If it isn't convenient for you to alter the model itself, you can place the door inside an empty parent object and position the child so that the hinge lines up with the parent's Y axis. With the model set up like this, rotations around the Y axis will make the door rotate around the hinge.

    From there, the easiest way to go is to use the animation editor to add a new animation to the door object. This will involve changing the Y rotation smoothly over a period of time. If you save that animation as "DoorOpen", say, you can then activate it from a script with something like:-
    Code (csharp):
    1. function Update() {
    2.     if (!animation.isPlaying  Input.GetKeyDown("e")) {
    3.         animation.Play("DoorOpen");
    4.     }
    5. }