Search Unity

New Need Help with Open Door Script

Discussion in 'Scripting' started by ctk111, Feb 24, 2010.

  1. ctk111

    ctk111

    Joined:
    Feb 24, 2010
    Posts:
    1
    I've been over this several times and still can't figure out what I'm doing wrong. :oops: I have a simple model and am trying to execute the open/close door script by Will Goldstone.

    I imported an animated door with 3 keyframes from max. I checked split animations and defined the idle, opendoor and closedoor start and end frames. I then attached the script to the First Person Controller. When I walk up to the door it is already slightly open and the animation doesn't play when I'm next to it. This is the script I'm using. The only thing I changed was the tag names. Any help is appreciated. I'm not very good at coding, but I usually can follow along with tutorials. Not sure where this is going wrong. Thanks.

    Code (csharp):
    1. var doorOpened : boolean = false;
    2. var doorAudio : AudioClip;
    3. var doorShut : AudioClip;
    4. var timer : float = 0.0;
    5.  
    6. function OnControllerColliderHit(hit:ControllerColliderHit){
    7.    
    8.     if((hit.gameObject.tag == "housedoor")  (doorOpened == false)){
    9.         openDoor();
    10.     }  
    11.    
    12. }
    13.  
    14.  
    15. function Update(){
    16.    
    17.     if(doorOpened){
    18.         timer += Time.deltaTime;
    19.     }
    20.    
    21.     if(timer >= 5){
    22.         shutDoor();
    23.     }
    24. }
    25.  
    26. function shutDoor(){
    27.  
    28.     var theHouse = gameObject.FindWithTag("house");
    29.     theHouse.animation.Play("doorclose");
    30.     doorOpened = false;
    31.     audio.PlayOneShot(doorShut);   
    32.     timer = 0;
    33. }
    34.  
    35.  
    36. function openDoor(){
    37.    
    38.         doorOpened = true;
    39.         var theHouse = gameObject.FindWithTag("house");
    40.         theHouse.animation.Play("dooropen");
    41.         audio.PlayOneShot(doorAudio);  
    42.        
    43. }