Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to animate a door

Discussion in 'Scripting' started by gposchman, Jan 26, 2012.

  1. gposchman

    gposchman

    Joined:
    Jun 8, 2011
    Posts:
    31
    I spent a while trying to work out how to open a door using the door knob, and closing it by touching the door or the door knob. I decided to use some basic shapes for the door, wall, hinge, and a remote object for the door opener because you never know when you may want to open a door with remote control. I added some texture and created a wall with a door and a cube as the remote control to open and close the door, but also that if you use touch the door once it has been opened, it will close.


    Figure 1 is a picture of Unity with the project for animating a door.


    This article assumes that you have a basic understanding of how to use Unity and you are familiar with scripting.



    The door itself is broken down into 3 components, the major component being a parent called “TheDoor” the hinge a child of “TheDoor” called “hingeA” which controls the door's action and the door itself which is a child of “hingeA” called “doorandpanels”. The names are irrelevant but the hierarchy structure is not.

    The cube is a remote switch that drives the initial action of opening the door, and can be put anywhere, including attached to the door where the doorknob is located.

    The scripting that controls the action of the door swinging open and closed is attached to “hingeA”.



    Variables to the RotateDoor Script

    var myDegrees = 100;
    var openDoor = 0;
    var closeDoor = false;

    var rotationState = 0; // this variable keeps track of the state of the rotation- whether it is off or to be opened 1, or closed 2


    The script is available in a separate file and I will describe what the various functions do if you wish to play with the script to affect what it does, but this article is about how to use the script. In figure 2 and the variables above are how to exercise the script. MyDegrees represents how far the swing of the door will go. The number does not represent actual degrees within a circle but is a time movement number that will need to be adjusted depending how far you want the swing to go. OpenDoor, closeDoor, and rotationState are variables used by the functions to perform the animation.

    There are three functions to this script. The Update function is a Unity function which updates for every frame. See Unity documentation for more about the update function. The update function is testing the rotationState to determine which direction the door is to swing. If rotationState is 0, then the door does not move. If rotationState is 1, then the door swings clockwise until the myDegrees is counted down and then rotationState is set to 0. OpenDoor is the counter used to show how much the door is opened, and it is also forced set to 0 when the door is closed so the door is always closed in the starting position. CloseDoor is a flag that when set to one indicates that the door is to be closed.

    The setRotate sets the rotationState based upon what it is. If the state is 1, then it changes it to 2, and if it is 2 it changes it to 1. if it is 0 then closeDoor is checked if it is 1, then rotationState is set to 2 to close the door, and if it is 0, then rotationState is set to 1.

    The closeTheDoor function checks closeDoor and if it is true it executes setRotate which will set the rotationState to close the door and the update function will comply.

    The rotateDoor script is controlled entirely by external forces, in this case a mouse down.



    That brings us to the open door script (see figure 3).

    OpenDoor Script

    var sendToObject: GameObject;
    var callTheFunction: String;

    function OnMouseDown () {

    sendToObject.SendMessage(callTheFunction);
    }

    The purpose of openDoor is to actually identify the game object being effected by a function command and to call that command (see figure 3).

    For the object doorandpanels the function is not setRotate but closeTheDoor.



    HingeA is a cube that fits into the hinged side of the door. In this example there are Unity commands that are used, transform.rotate and transform.eulerAngles, in the script I use the Y axis though any axis may be used.

    The following links will provide you with the additional elements to animate a door.

    http://www.renaissanceplayer.com/unity/AnimateDoor.html

    Just click on the white cube to open and close the door, and click on the door to close it. If you do not have the Unity Web player, you will need to install it from Unity. It is safe and contains no nasties.

    http://www.renaissanceplayer.com/unity/AnimateDoor.zip

    This folder contains the entire project, the scripts are mine and may be used anyway you want, just give me credit. The basic images are also free to use. If you have any question please contact me at gposchman@renaissanceplayer.com.

    Gene
     
  2. TheCasual

    TheCasual

    Joined:
    Sep 30, 2010
    Posts:
    1,286
    Really simple , but definately could help alot of people out! Nice stuff, anytime someone contributes to everyone, its appreciated.
     
  3. gposchman

    gposchman

    Joined:
    Jun 8, 2011
    Posts:
    31
    I have made some changes to the script for simpleRotate because I was having a problem with the door not quite closing correctly. Additionally I added code and a sound effect for the door closing. Not sure whether to publish a whole new piece or to just show the differences, or simply to answer questions on the forum here.

    Here is the code that I changed the most it was ---

    if (rotationState == 2) {

    if (openDoor < 20) {
    rotationState = 0;
    closeDoor = false;
    openDoor = 0;
    transform.eulerAngles = Vector3(0, 0, 0);
    // transform.Rotate(0, 0 * Time.deltaTime, 0);
    }
    else {
    transform.Rotate(0, -myDegrees * Time.deltaTime, 0);
    openDoor = openDoor - 10;
    }

    }


    And I changed it to ---

    if (rotationState == 2) {

    if (transform.eulerAngles.y < closeAxis ) {
    transform.Rotate(0, 0, 0);
    rotationState = 0;
    closeDoor = false;
    }
    else {
    transform.Rotate(0, -myDegrees * Time.deltaTime, 0);

    }

    }


    This new code forces the transform.rotate to 0,0,0 which returns the door to its "closed position".
    The previous code didn't always do this.

    I also added code to have a sound effect of a closed door happen, but that is not shown here. If there is interest I will publish the full enhanced version of the closed door.

    Gene
     
  4. Rag2RichesFX

    Rag2RichesFX

    Joined:
    Nov 2, 2012
    Posts:
    18
    thanks i appreciate this :)