How to set up Animator when playing Animations from string?

Hi!

I’m trying to play animations based on what I write in the Inspetor/String.

    public class Activity
    {
        public string animation; // The name of the animation to play
        public Transform location; // Location at which the activity should be performed
    }




     if (Vector3.Distance(transform.position, activity.location.position) < tolerance)
                {
                      // Play the animation for the activity
                        animator.SetTrigger(activity.animation);
                        Debug.Log("PLAYING MY ANIMATION OF CHOICE");
                }
                // If the NPC is not at the location of the activity, move to the location
                else
                {
                     animator.Play("Walk");
                }

The Animator and inspector look like this (Image Attached) - But I have noooo clue how to set it up in the Animator. I’ve learned I should write “” on the names in the Inspector. But nothing happens…
The animator manage to play the “Walk” animation, but thats only cuz its written in the script.
The other activites I want to write myself in the Inspector so I can make sure all NPC are doing different thing etc.
I rather not use triggers between 10-20 animations. So I want the Animation written in the Inspector to play.
Any idea how to do this? Is it possible?

Thankful for help!

animator.Play(“State Name”);
animator.CrossFade(“State Name”, fadeDuration);

If you set the name in the Inspector, don’t include quotes. Quotes are for C# to know that something is a string rather than actual code but an Inspector field already knows it’s a string.

Thanks for the info, but how do I set it up in the Animator? Does it look ok on the Image? Meaning, No need to make transistion arrows and all that? I can just leave it as is?

Also, do you mean i should replace:
animator.SetTrigger(activity.animation);

With:
animator.Play(“State Name”);
animator.CrossFade(“State Name”, fadeDuration);

?

Play if you want an instant transition or CrossFade if you want it to blend over time.

The Animator Controller in your image is correct, Play and CrossFade ignore transitions.

1 Like