Search Unity

Making different doors toggle open and closed.

Discussion in 'Scripting' started by rogersc, Apr 2, 2007.

  1. rogersc

    rogersc

    Joined:
    Apr 2, 2007
    Posts:
    20
    Hi All,

    I have a project and can only get one door to work. Here is the code that works:

    private var clipLength = 0.00;
    private var moving = false;
    private var open = false;
    private var originalPosition : Vector3;
    private var originalRotation : Quaternion;

    function Start()
    {
    animation.Stop();
    animation["open"].wrapMode = WrapMode.PingPong;
    clipLength = animation["open"].clip.length;
    originalPosition = transform.localPosition;
    originalRotation = transform.localRotation;
    }

    function OnMouseDown()
    {
    if(!moving)
    {
    if(open)
    {
    Close();
    }
    else
    {
    Open();
    }
    }
    }

    function Open()
    {
    moving = true;
    animation["open"].enabled = true;
    yield WaitForSeconds(clipLength);
    animation["open"].enabled = false;
    moving = false;
    open = true;
    }

    function Close ()
    {
    moving = true;
    animation["open"].enabled = true;
    yield WaitForSeconds(clipLength - Time.deltaTime);
    animation["open"].normalizedTime = 0;
    animation["open"].enabled = false;
    moving = false;
    transform.localPosition = originalPosition;
    transform.localRotation = originalRotation;
    open = false;
    }

    "open" is the name of the animation I am using. When I try to reuse the code and change the name "open" to say "doorflyout" it still only works for my original door. Is there a better way to toggle an animation? I built the animation in Unity.

    Thanks!
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    if your animation component has only one animation, just replace all of the placed where it says animation["open"] with animation[animation.clip.name]
     
  3. rogersc

    rogersc

    Joined:
    Apr 2, 2007
    Posts:
    20
    Thanks Yoggy. To anyone who may read this needing help with a door:

    The door was part of my object when I imported it as an asset into Unity.

    I animated the rotation and the position of the door in Unity. All the door was animated to do was open.

    This script basically allows the animation to play forward or in reverse, thus giving the effect of a closing door.