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. Dismiss Notice

How to change animation when hitting collider?

Discussion in 'Scripting' started by FalconJ, Mar 13, 2015.

  1. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I have a sprite which include an edge collider with isTrigger enabled. I want to make my player object change its animation when he got hit by this collider.
    Any solutions?
    thank you
     
  2. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Suppose your have an animator component on your gameObject, and in the animator controller you have a trigger varible called hit.

    Code (CSharp):
    1.  
    2. Animator anim;
    3.  
    4. Awake()
    5. {
    6.   anim = gameObject.GetComponent<Animator>();
    7. }
    8.  
    9. OnTriggerEnter(Collider other)
    10. {
    11.   anim.SetTrigger("hit");
    12. }
    13.  
     
  3. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    Sorry I'm confused what you mean there. Right now I have 2 parameters called alive and dead (both of them are boolean) in my animator.

    And I gave this function to the character :
    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D collider)
    2.     {
    3.         animator.SetBool("dead", true);
    4.     }
    But the character is switching back and forth the alive and dead animation endlessly.
    What should be corrected?
     
  4. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    bool is almost right, delet it and create a dead trigger instead

    if the boolean dead is switching to fast the animator cant capture the state.
    if you use a trigger the animator gets locked in the state.

    in the ontrigger function do what roderyk said:

    Code (CSharp):
    1.  OnTriggerEnter(Collider other)
    2. {
    3.   anim.SetTrigger("dead");
    4. }
     

    Attached Files:

  5. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I have created the trigger now and applied the code but the character isn't change its animation.
    Do I need to make transition from "Any State" to "Die" in Animator? Sorry I'm new to Unity so this all is part of the learning process.
     
  6. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    show us your animator, you can make a screenshot and past it here.
     
  7. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    Here's the screenshot
     

    Attached Files:

    • ask1.png
      ask1.png
      File size:
      95.2 KB
      Views:
      1,063
  8. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    so your Character can only die and fall?

    add an idle animation state. take a look at my character animator i posted previously (ignor Entry state this is new in Unity 5 and it looks like you are using <5)

    Edit:
    this Tutorial includes the Animator State Machine that suits your problem perfectly:
    http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers @ jump to 20 min and 45 seconds
     
    Last edited: Mar 14, 2015
  9. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    Yes it only has 2 animations, the default is fall(because it's a falling game) and if the character hit the wall, it dies.
     
  10. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    simple game idea. i'm interessted, keep me updated :)

    first: falling is your default state right click on it -> set as default layer state (now state should be orange) - or am I wrong again and your game idea is dieing most of the time and sometimes falling ;)

    second: delete the transition from "any state" -> falling

    third: add a transition from dead -> falling, transistion has exit time = 1 (one complete die animation will be shown before translation to state falling)
     
  11. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    :) I have followed your steps. So right now, in the animator, there are "Any State" (not connected to anything), "Fall" <-- "Die". And there are 2 parameters, "Fall"(boolean) and "Dead"(trigger).
    Do I need to change anything again? Because the character is still not moving.

    PS : The character has a rigidbody 2D also.
     
  12. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    any state -> dead transition with condition trigger dead

    please upload a screenshot

    and your current script
     
  13. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    This is my current script (it's attached to the player) :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseController : MonoBehaviour {
    5.  
    6.     Animator animator;
    7.  
    8.     Transform myTrans;
    9.     Rigidbody2D myBody;
    10.  
    11.     float speed = 3.0f;
    12.  
    13.     void Awake(){
    14.         animator = gameObject.GetComponent<Animator> ();
    15.     }
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         myBody = this.rigidbody2D;
    20.         myTrans = this.transform;
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.         transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
    26.     }
    27.  
    28.     void OnTriggerEnter2D(Collider2D other)
    29.     {
    30.         animator.SetTrigger("die");
    31.     }
    32. }
    33.  
    and I also attached the animator screenshot below
     

    Attached Files:

  14. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    you move your character with transform.Translate().... with this method you can't get Collisions/Triggers to run. You should work with

    GetComponent<Rigidbody2D>().MovePosition()
    GetComponent<Rigidbody2D>().AddForce()
    GetComponent<Rigidbody2D>().velocity

    although this is not perfect try this one:

    and check your console if the OnTriggerEnter fires some messages

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class MouseController : MonoBehaviour {
    4.     Animator animator;
    5.     bool isDead = false;
    6.     Transform myTrans;
    7.     Rigidbody2D myBody;
    8.     float speed = 3.0f;
    9.     void Awake(){
    10.         animator = gameObject.GetComponent<Animator> ();
    11.     }
    12.     // Use this for initialization
    13.     void Start () {
    14.         myBody = this.rigidbody2D;
    15.         myTrans = this.transform;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update () {
    20.     if(anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
    21.         {
    22.             isDead = false;
    23.         {
    24.  
    25.     if(!isDead)
    26.         {
    27.             animator.SetBool("fall", true);
    28.             transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
    29.         }
    30.     }
    31.     void FixedUpdate () {
    32.     myBody.MovePosition(this.transform.position);
    33.     }
    34.  
    35.  
    36.     void OnTriggerEnter2D(Collider2D other)
    37.     {
    38.     Debug.Log("I'm hitting " + other.name " + ", die!");
    39.        Die();
    40.    }
    41.  
    42.    void Die()
    43.    {
    44.        isDead = true;
    45.        animator.SetTrigger("die");
    46.    }
    47. }
     
    Last edited: Mar 15, 2015
  15. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I'm still using my code and noticed that the trigger is working, maybe because I just realized I accidentally removed the rigidbody2D component. The only problem is now the animation part. I'll try playing with the animator a bit and hope that helps.
    Anyway, thank you tobad and roderyk :)