Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Help with animated doors?

Discussion in 'Animation' started by DxDen1004, Nov 27, 2014.

  1. DxDen1004

    DxDen1004

    Joined:
    Nov 27, 2014
    Posts:
    8
    Hello everybody, I started using Unity 3D 2 months ago but only two days ago I decided to create a small game, an now i have a big problem.
    I want to create several rooms and for each room there must be a door, i've tried this script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var isOpen : boolean = false;
    4. private var guiShow : boolean = false;
    5. var Door : GameObject;
    6. var rayLength = 10;
    7.  
    8. function Update()
    9. {
    10.     var hit : RaycastHit;
    11.     var fwd = transform.TransformDirection (Vector3.forward);
    12.     if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    13.     {
    14.         if(hit.collider.gameObject.tag == "Door")
    15.         {
    16.             guiShow = true;
    17.             if(Input.GetKeyDown("e") && isOpen == false)
    18.             {
    19.                 Door.animation.Play("DoorOpen");
    20.                 isOpen = true;
    21.                 guiShow = false;
    22.             }
    23.            
    24.             else if(Input.GetKeyDown("e") && isOpen == true)
    25.             {
    26.                 Door.animation.Play("DoorClose");
    27.                 isOpen = false;
    28.                 guiShow = false;
    29.             }
    30.         }
    31.     }
    32.    
    33.     else
    34.     {
    35.         guiShow = true;
    36.     }
    37. }
    38.  
    39. function onGUI()
    40. {
    41.     if(guiShow == true && isOpen == false)
    42.     {
    43.         GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Interagisci");
    44.     }
    45. }
    and it works fine, but i need more than one door..i need a lot of doors in my game and for every door i have to create a new animation and a new version of the script, matching the animations. So the question is: there's a quickest way to put more than one animated door, without modifying the script and create new animations every time? Thanks in advance and sorry for my english but i'm italian =)