Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

door opens on event

Discussion in 'Scripting' started by Eldunar, Jun 5, 2015.

  1. Eldunar

    Eldunar

    Joined:
    Jun 3, 2015
    Posts:
    8
    Hello,
    i tried to wrtie script which will open door when the player will be in trigger. But when i try to open door function OnTriggerStay is calling itself 4 times. How can i fix this issue?

    Code (CSharp):
    1.  
    2.     public bool trigered=false;
    3.     public bool Opened=true;
    4.  
    5.     private Animation anim;
    6.     private GameObject player;
    7.  
    8.  
    9.     void Start () {
    10.         anim = GetComponent<Animation> ();
    11.         player = GameObject.FindGameObjectWithTag ("Player");
    12.     }
    13.     void OnTriggerStay(Collider other) {
    14.         if (other == player)
    15.         {
    16.             trigered = !trigered;
    17.             Opened=!Opened;
    18.             if (Input.GetKeyDown (KeyCode.E))
    19.             {
    20.                     if (Opened==true)
    21.                     anim.Play ("CloseDoor");
    22.                     else
    23.                     anim.Play ("OpenDoor");
    24.                    
    25.                 }
    26.             }
    27.         }
    28.     }
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    519
    OnTriggerStay is called every frame the colliders are touching. Is that what you want? Have you considered OnTriggerEnter which is only called the first time the colliders touch
     
  3. Eldunar

    Eldunar

    Joined:
    Jun 3, 2015
    Posts:
    8
    But inOnTrigerEnter the moment when it is possible to open door is very short. Thats why i put there triggered as a bool to check how long i will be able to OpenDoor:) I want to be able to open door whenever i want. Thats why i gave there OnTrigerStay. Have u different way to do that?
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The main issue you have is your boolean values. While the player is in the trigger, both trigered and Opened are switched in every frame which should definitely not be the case.
    First, you can forget about trigered, because it is not used at all and it has a typo ;) . If you want to use it, you should set it to true in there and have the trigger exit event as well which then turns it off.
    Besides that, Opened should only be changed in when you start to play the animation. As you start the close animation, set it to false, when you start the open animation, set it to true. If you do that, don't forget the curly braces!
     
  5. Eldunar

    Eldunar

    Joined:
    Jun 3, 2015
    Posts:
    8
    Yeah, you are correct! Now it works fine! Thank you so much!