Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to set up Animator when playing Animations from string?

Discussion in 'Animation' started by RuneShiStorm, Jan 10, 2023.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi!

    I'm trying to play animations based on what I write in the Inspetor/String.
    Code (csharp):
    1.  
    2.     public class Activity
    3.     {
    4.         public string animation; // The name of the animation to play
    5.         public Transform location; // Location at which the activity should be performed
    6.     }
    7.  
    8.  
    9.  
    10.  
    11.      if (Vector3.Distance(transform.position, activity.location.position) < tolerance)
    12.                 {
    13.                       // Play the animation for the activity
    14.                         animator.SetTrigger(activity.animation);
    15.                         Debug.Log("PLAYING MY ANIMATION OF CHOICE");
    16.                 }
    17.                 // If the NPC is not at the location of the activity, move to the location
    18.                 else
    19.                 {
    20.                      animator.Play("Walk");
    21.                 }
    22.  
    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!
     

    Attached Files:

  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,533
    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.
     
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    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);

    ?
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,533
    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.
     
    RuneShiStorm likes this.