Search Unity

animation won't play on collision

Discussion in 'Scripting' started by scr33ner, Aug 1, 2015.

  1. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    188
    Hi all,

    I've attached collision detection to the character controller script that plays a "gothit" animation clip (using legacy animations).

    Here are the code snippets, the first is for the collision:
    Code (CSharp):
    1. void OnTriggerEnter (Collider other)
    2. {  
    3.      if(other.gameObject.CompareTag ("dmgEnvironment")||other.gameObject.CompareTag ("dmgPlayerHit"))
    4.     {
    5.         GetComponent<Animation>().Play("gothit", PlayMode.StopAll);
    6.         GetComponent<Animation>().wrapMode = WrapMode.Once;
    7.         print("hit damage");
    8.     }
    9. }
    The attack animation call:
    Code (CSharp):
    1. public void AnimatePunch()
    2. {
    3.     DidAttack ();
    4.     //play animation
    5.     if (canHit && lastAttackTime < lastAttackButtonTime + attackTimeout) {
    6.         SendMessage("DidAttack", SendMessageOptions.DontRequireReceiver);
    7.     }
    8.     GetComponent<Animation>().Play("punch", PlayMode.StopAll);
    9.     GetComponent<Animation>().wrapMode = WrapMode.Once;
    10. }
    The problem I'm running into is that the "gothit" sequence won't play on collision (even though it's detected) but will do so when wired to the AnimatePunch().

    I'm not sure why this is happening. Any help is greatly appreciated.

    Edit: attached is the 3rd person controller script.
     

    Attached Files: