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 can i make animation while object hit trigger ?

Discussion in 'Animation' started by karammaks, Apr 25, 2014.

  1. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    I want a help in animation , i have a game which objects fall and player should catch them by box trigger, so when spawner fell a Bomb in Box trigger , i want to make a BOOM animation , but i dont know where to put the script and what will the script going to be and how ? please help me :cry::confused:
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Attach a script with OnTriggerEnter() to your box. Below is an example, but you will need to modify it to fit your needs. Also, I just typed it into this reply, so please forgive any typos.
    Code (csharp):
    1.  
    2. public class Box : MonoBehaviour {
    3.  
    4.     void OnTriggerEnter(Collider other) {
    5.         if (string.Equals(other.tag, "Bomb")) { // <-- Tag your bombs as "Bomb".
    6.             PlayBoomAnimation();
    7.             Destroy(other.gameObject);
    8.         }
    9.     }
    10.  
    11.     private void PlayBoomAnimation() { // <-- Use this version for Legacy.
    12.         animation.Play("boom"); // <-- Your box must have an animation named "boom".
    13.     }
    14.  
    15.     private void PlayBoomAnimation() { // <-- Use this version for Mecanim.
    16.         Animator animator = GetComponent<Animator>();
    17.         if (animator != null) {
    18.             animator.SetTrigger("boom"); // <-- Your animator controller must have a trigger named "boom".
    19.         }
    20.     }
    21. }
    22.  
     
  3. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    i already made onTriggerEnter2D function , but im not sure if i should make animation for the Bomb or for the Box , or separate one , and if you can please tell me the steps how to link this animation with code , i am beginner with unity and need your help :)
    thanks a lot
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
  5. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    still doesnt work :( i already put the first code to the box trigger, but still dont know where to put the second and the third code , and why you called private void PlayBoomAnimation() twice ?! , i saw videos for animation and 2D , and i made animation for the Bomb fuze to flash , but i still dont know how to make animation while the bomb hit the box trigger , please if any one can help me more about this issue :(