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

Adding events to an animation

Discussion in 'Animation' started by Ruckrova, Nov 5, 2020.

  1. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    I am trying to add an event to one of the whales animations to trigger an audio and a particle system to play so that the whale breaths when it surfaces but get lost trying to add the function of the event .

    only trying to get the audio to work first .

    can an event function do both ?

    I have watched many of the tutorials about it but cant follow them in my case .

    "no function selected" is the only thing to come up in the list .
     
  2. dlips

    dlips

    Joined:
    Nov 8, 2019
    Posts:
    4
    I first had the same problem. It turns out that you can only call function from the gameObject which has the Animator component. In my case the Animator component was on a child of the actual gameObject that had the function I wanted to call.
     
  3. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    thanks dips

    So what did you do ?

    I think the function in on the gameobject but how can you tell ?

    I not sure what the function is really , do they mean the script , im not a programmer and dont understand a lot of the terms they use .

    this is the error message

    'humpback_whale_model_baked_animation' AnimationEvent has no function name specified!
     
  4. dlips

    dlips

    Joined:
    Nov 8, 2019
    Posts:
    4
    I just moved the component with the function I wanted to call to the same object that had the Animator component. Then it worked.
     
  5. Ed_Muel

    Ed_Muel

    Joined:
    Mar 16, 2017
    Posts:
    51
    If you created a script, lets call it "Animation Events",
    in there create a function called Void PlayWhaleSong
    Add the script as a component to the whale gameobject.
    Add the whale Gameobject to a scene (you can delete it later)
    click on the whale, and open the "Animation" tab
    Now you should see all the animations available on the whale,
    select the one you want, add the event, and then in the inspector window it will list all the available functions you can trigger on the event. Select "PlayWhaleSong" and you're done.

    That actually amends the animation file itself and not the whale in the scene, so you can delete that if you want.

    Hope that helps.
     
  6. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    Thanks so much for your responses so far but I am not a programmer so I have a very hard time working on scripts.

    Can you please show me what your script looks like .

    Heres what i worked out so I can get the breath to happen with a button press . The script is a component of the whale, can I add to this script to get the breath to be an event in the animation for the same whale ?

    did i mention im using 2018.4.2f1 personal


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class whaleBreath : MonoBehaviour

    {


    public GameObject Breath;


    // Update is called once per frame
    void Update ()

    {
    if(Input.GetKey(KeyCode.Tab))
    {
    Breath.SetActive(true);


    }
    else Breath.SetActive(false);
    }


    }
     
  7. Ed_Muel

    Ed_Muel

    Joined:
    Mar 16, 2017
    Posts:
    51
    Hi,

    Yeah, you just need to add a new function to the script, so outside of void update () which is triggered every frame you can create a new custom function that you can trigger from the animation event.

    Private void activateWhaleBreath()
    {
    Breath.setactive(true)
    }
     
  8. Ed_Muel

    Ed_Muel

    Joined:
    Mar 16, 2017
    Posts:
    51
    If that script is added to the whale, and the whale is active in the scene, then each custom function in it should appear as an option when you create an animation event.
     
  9. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    thanks I now see the function .

    Heres what the script looks like after the software suggested this .


    Code (CSharp):
    1. public class whaleBreath : MonoBehaviour
    2.  
    3. {
    4.  
    5.  
    6.         public GameObject Breath;
    7.      
    8.      // Update is called once per frame
    9.      void Update ()
    10.  
    11.       {
    12.             if(Input.GetKey(KeyCode.Tab))
    13.             {
    14.              Breath.SetActive(true);
    15.  
    16.  
    17.             }
    18.             else Breath.SetActive(false);
    19.     }
    20.  
    21.     private void
    22.        activateWhaleBreath() => Breath.SetActive(true);
    23. }

    but the event does not change the Active of the whaleBreath game object when the animation plays in preview or in game .

    I still dont have it working .

    whalebreath object has an audio source as a component and the mist particle system as a child .

    should they both be in the whale object and not the whalebreath object in order for the animation event can trigger them ?
     
  10. Ed_Muel

    Ed_Muel

    Joined:
    Mar 16, 2017
    Posts:
    51
    Hhmm... Tricky to say without seeing it, but if

    Breath.SetActive(true)

    Works when you press the tab button, then it should also work when triggered by the animation event. Which means, either the event isn't firing, it it can't find the right objects when it does.

    Turn it in to a full void (in my example) and add a few debug.log messages in there.
    Debug.log("firing breath event")

    Check the console after to see if that wrote correct,
    If it wrote then the event fired okay.

    If that works then stick a debug line on your code and check out what it's actually seeing at runtime.
     
  11. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    I have made some progress
    the animation event does trigger the whalebreath object to active but the "else" statement turns it back off instantly , so i removed it and made a second script that is triggered later in the animation that sets the state to inactive and that works .


    thanks for your help on this .
     
    Last edited: Nov 26, 2020
    Ed_Muel likes this.