Triggering an animation from within another animation

Hi firstly I’m an artist with very little to no code knowledge. Im wanting to trigger an animation from within another animation much like you can trigger an animation from an on click of a ui button. How do i go about doing this and do I require an additional script or something?

I know you can set events in the animation timelime but I can’t find out how to get that event to trigger another animation?

Any help really appreciated :slight_smile:

Im not too sure what you mean, weather you want it triggered through collision or just at a specific time of another animation. Both however are possible. If you have explored with parameters, you can do a true or false parameter that is triggered with a true or false statement. This would require multiple scripts and would take time but is very possible and if you know what your doing it is very easy. After adding a true or false parameter, you go to the arrow that starts the default animation and only allow it to trigger when the section is declared true. Now that you have this general Knowledge I can continue to the code. The code takes advantage of these parameters. If your looking for a on collision animation then you would need to do function OnTriggerEnter () { if your doing something that detects when your other animation is at a certain point you would use function Update () { But before all of this we need to look at the beginning variables. The beginning variables would be as follows…

var Shooting = false; //or whatever the name of your paramater is.
var animator : Animator; //stores the animator component
 
function Start () {
 
animator = GetComponent(Animator); //assigns Animator component when we start the game
 
}

Now depending on what you want would vary
for the trigger, you would do…

function OnTriggerEnter (){
     shooting = true;
}

This script is attached to the object that relies on the first objects animation. If you want alignment of animations, there are two ways to do this. One you could use 2 scripts and have one activate when the first animation says so or you could put all of this into an empty game object and have it animate all together. But ill show you the scripting version.
First off your going to need two more scripts, one for the second animation and one for the first. The first animated object is very simple.

public var Shooting = false;

function Start () {
    Shooting = true;
}

You want to make sure you put this on to your first object and leave it unchecked. For your second script, itll look alot like what we had before.

var Working = false; //or whatever the name of your paramater is.
var animator : Animator; //stores the animator component
  
 function Start () {
  
 animator = GetComponent(Animator); //assigns Animator component when we start the game
  
 }

function Update (){
 if (Shooter.Shooting == true){ //instead of "Shooter", do the name of you gave the first script
 Working = true;
}
}

I believe I have covered everything, If this was helpful a thumbs up would benefit me greatly along with marking the question as correct, if your looking for something else id love to see if I can help. Again, I really hope this helps! :slight_smile:

That was an amazing response thankyou.
I was looking for a way to utilise the event trigger within an animation to trigger a second reactive animation (not through collision).
I’ll try your above approach and see if I can get it working. Thanks again :slight_smile:

I have a one script answer following Sethhalocat’s lead. Thank you for this it helped me @Sethhalocat.

public class Defender : MonoBehaviour 
{

    public int firingNow = 0; // parameter manager - can be a bool like Sethhalocat says
    Animator myAnim; 
    void Start()
    {
        myAnim = GetComponent<Animator>();
    }

    public void FireNow() // call this function start the next animation through the animation event
    {
        firingNow = 1;  // make sure to set up the animation event where you want it

    }
    void Update()
    {
        if (firingNow == 1)
        {
            myAnim.SetInteger("Fire", firingNow); // string refs to the parameter and next anim state
            myAnim.Play("Cactus Attacking");   // this plays and resets the animation if you need to
            firingNow = 0;
        }
    }

    
}