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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to get trigger that opens door to work properly.

Discussion in 'Scripting' started by Ninamori, Nov 2, 2015.

  1. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    I have a feeling it's a problem in the animator, but we'll see.

    Here is a video demonstrating the problem. The expected behavior is when the ball hits the cube it opens the door, and when it exits the door closes. Problem the first time the ball enters nothing happens, and on exit the door opens then closes (just watch the video).

    I followed a tutorial made this script with some modifications to only allow the ball.

    My code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SetAnimationBoolOnTrigger : MonoBehaviour {
    6.  
    7.     public string animationBoolName;
    8.     public Animator target;
    9.  
    10.     void OnTriggerEnter(Collider coll)
    11.     {
    12.         if (coll.gameObject.tag == "Ball")
    13.             Debug.Log("Ball Entered! Door Opening!");
    14.             target.SetBool(animationBoolName, true);
    15.     }
    16.  
    17.     void OnTriggerExit(Collider coll)
    18.     {
    19.         if (coll.gameObject.tag == "Ball")
    20.             target.SetBool(animationBoolName, false);
    21.     }
    22. }
    23.  
    The videos shows the issue, and shows the animator set up. Mine deviates a bit from the one shown in the the tutorial I linked, because I had the opposite issue he had.

    Anyway how do I fix this so when the ball hits the cube it opens the door, and when it exits the door closes? It's just weird to me that it doesn't work on the first entry.
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you make sure that the initial value of animationBoolName is false?
     
  3. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    Ya it is set to false.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You seem to be starting in the door open state. In the animation graph, "Initial" points to "DoorOpen", but the door is in fact not open. The graph and your visuals in the game don't match.
     
  5. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    I swapped the initial to got to door close. Same thing happens. Doesn't work the first time, and upon the ball exiting the first time the door opens then closes. After that it works perfectly fine.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Check the state machine and the variable at runtime. Before you enter the cube, what is the state and what's the value of the boolean?
     
  7. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    Not sure how to check that exactly, but the boolean should be false until the ball enters the cube.
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Make sure you have the Animator window open at runtime that shows the controller that is responsible for the door.
     
  9. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    I detached it, and nothing happens. Only thing that I can see is the transition from idle is white.
     
  10. bayview

    bayview

    Joined:
    Apr 3, 2014
    Posts:
    7
    replace your code below,my english not good,so just try the code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SetAnimationBoolOnTrigger : MonoBehaviour {
    5.  
    6.    public string animationBoolName;
    7.    public Animator target;
    8.  
    9.    void OnTriggerEnter(Collider coll)
    10.    {
    11.        if (coll.gameObject.tag == "Ball")
    12. {
    13.       Debug.Log("Ball Entered! Door Opening!");
    14.        target.SetBool(animationBoolName, true);
    15. }
    16.    }
    17.  
    18.    void OnTriggerExit(Collider coll)
    19.    {
    20.        if (coll.gameObject.tag == "Ball")
    21.            target.SetBool(animationBoolName, false);
    22.    }
    23. }
     
    Dantus and Ninamori like this.
  11. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    Thank you so much! Those 2 brackets really made the difference!
     
  12. bayview

    bayview

    Joined:
    Apr 3, 2014
    Posts:
    7
    you are welcome! :),it's my plesure
     
    Ninamori likes this.