Search Unity

Calling animations through triggers?

Discussion in 'Scripting' started by GamesOnAcid, Feb 8, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Code (JavaScript):
    1.     void OnTriggerEnter(Collider other){
    2.         if(other.attachedRigidbody){
    3.             GetComponent<Animator>().SetBool("Running", true);
    4.             GetComponent<Animator> ().SetBool ("Walking", false);
    5.         }
    6.     }
    7.  
    8.     void OnTriggerExit(Collider other){
    9.         if(other.attachedRigidbody){
    10.             GetComponent<Animator>().SetBool("Running", false);
    11.             GetComponent<Animator> ().SetBool ("Walking", true);
    12.         }
    13.     }
    14.  
    So, basically what I want to do it set it up so that the enemy runs when I enter a trigger, and walks when I exit it. Problem is, he doesn't do that. My animator controller is using a state called Idle, which is never used by the game, which can transition to walking and running. I also have 2 parameters called running/walking that are bools, but I don't think they are referencing the animations. Can anyone tell me how to make this work? Currently, all he does is switch between running and waking constantly every second. Any help appreciated, thanks!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    have you set the bools to be used in the transitions between animation states?
     
  3. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Yes, I have. They are both set so if Walking is true and Running is false, they play. That doesn't work however.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    surely one of them should be the other way around...
    if walking and runningBool true : run,
    if running and walkingBool true: walk

    From what you've described it also sounds like the timecompletion condition is in the transition too, so that it only changes when an animation cycle has completed.