Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How can I play an animation when 3 triggers are being detected on character ??

Discussion in 'Scripting' started by staincorb, Jul 22, 2014.

  1. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    Hi all .

    I need a code to be able to play an animation of an object only when 2 or more specified triggers are colliding with Character.

    The code im using to play animation is this one

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    var ObjectToAnimate : GameObject; //Implement your Faller game object into this variable in the inspector

    function OnTriggerEnter (Other : Collider){

    if(Other.gameObject.tag == "Player"){

    ObjectToAnimate.animation.Play("DoorOpen_IN");

    }

    }

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Basically I need something like If TriggerA, TriggerB, TriggerC, Play Animation, or however its done.

    If someone has done some thing similar and can help that would be awsom if not, I will just take longer :D
     
  2. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    You could set up 3 booleans, one to represent each trigger. When all three booleans are true play the sound
     
  3. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    thx, looking for booleans_ cant deam to find reference on what they are, just code with them, any script documentation in unity page ?
     
  4. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Booleans are a variable type that hold true/false values.

    http://en.wikipedia.org/wiki/Boolean_data_type

    Example.

    Code (CSharp):
    1. public bool animationTrigger = true
    2.  
    3. ....
    4.  
    5. if (animationTrigger == true){
    6. Animation.play(WalkingAnimation);
    7. }
    In your case you just need to create 3 boolean variables and set them to "true" OnTriggerEnter or OnTriggerStay, and set to false on OnTriggerExit. Then check whatever the boolans are true or not.


    something like:
    Code (CSharp):
    1. public bool animationTrigger1 = false;
    2. public bool animationTrigger2 = false;
    3. public bool animationTrigger3 = false;
    4.  
    5.  
    6.  
    7. ...
    8. if (animationTrigger1 == true && animationTrigger2 == true && animationTrigger3 == true){
    9. ObjectToAnimate.animation.Play("DoorOpen_IN");
    10. }
    and on the collider something like

    Code (CSharp):
    1.  
    2. void OnTriggerStay (Other  Collider){
    3.  
    4. if(Other.gameObject.tag == "Player"){
    5.  
    6. //turn trigger value to true
    7. Player.animationTrigger1 = true;
    8.  
    9. }
    10.  
    11. }
    12.  
    13. void OnTriggerExit (Other  Collider){
    14.  
    15. if(Other.gameObject.tag == "Player"){
    16.  
    17. //turn trigger value to false
    18. Player.animationTrigger1 = false;
    19.  
    20. }
    21.  
    22. }
    23.  
    24.  
    This is sample code, you need to create the final script yourself.

    Kind regards.
     
  5. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    Thx thats more like it, I will give it a try. sorry for Noobnes, game is not so noobish :D