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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Legacy Animator State Machine - Object Instance - SM does not affect Object Animations

Discussion in 'Scripting' started by eanbowman, May 26, 2017.

  1. eanbowman

    eanbowman

    Joined:
    May 26, 2017
    Posts:
    5


    I have created a little raccoon character prefab for a board game. It contains an animator which points to the FBX's imported animations.

    If I click on the state machine of the generic object, it affects the instance.

    If I click the instance, then the state machine for that instance shows, and the states are updating. From there I can turn on the booleans but they have no effect.

    Why is this? Why can I not have an animator on an instance that works?

    Thanks in advance!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityStandardAssets.Characters.ThirdPerson;
    6.  
    7. public class RaccoonToken : MonoBehaviour {
    8.     private Animator anim;
    9.     private Transform target;
    10.     private NavMeshAgent nav;
    11.  
    12.     // Use this for initialization
    13.     void Awake () {
    14.         SetReferences();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if( this.target != null ) CheckDistanceFromDestination();
    20.  
    21.         bool jump = Input.GetButtonDown("Fire1");
    22.         this.anim.SetBool("Jump", jump);
    23.     }
    24.  
    25.     void SetReferences() {
    26.         this.anim = gameObject.GetComponent<Animator>();
    27.         Debug.Log(this.anim.runtimeAnimatorController);
    28.         this.nav = gameObject.GetComponent<NavMeshAgent>();
    29.     }
    30.  
    31.     public void MoveTo(Transform destination) {
    32.         Debug.Log(this.anim);
    33.         this.target = destination;
    34.         Vector3 position = new Vector3(destination.position.x, destination.position.y + 1, destination.position.z);
    35.         this.nav.SetDestination(position);
    36.         Debug.Log(this.nav);
    37.         this.anim.SetBool("Walking", true);
    38.         Debug.Log("Walking state: " + this.anim.GetBool("Walking"));
    39.     }
    40.  
    41.     void CheckDistanceFromDestination()
    42.     {
    43.         //Debug.Log("destination: " + this.nav.destination + ", pathStatus: " + this.nav.pathStatus);
    44.         float dist = Vector3.Distance(this.target.position, transform.position);
    45.         if (this.anim.GetBool("Walking") == true)
    46.         {
    47.             if (false)//dist < 1.85f)
    48.             {
    49.                 this.nav.enabled = false;
    50.                 Debug.Log("Walking state: " + this.anim.GetBool("Walking"));
    51.                 this.anim.SetBool("Walking", false);
    52.                 Debug.Log("Walking state: " + this.anim.GetBool("Walking"));
    53.             }
    54.         }
    55.     }
    56. }
    57.  
     
  2. eanbowman

    eanbowman

    Joined:
    May 26, 2017
    Posts:
    5
    Obviously what I want here is for the raccoon to run once I set walking to true in the state machine of that instance. I can't for the life of me figure out why the state machine particular to that instance won't animate it but the prefab that is already selected before I click play will.

    You can see the state machine of the instance properly changing walking to true when I click run.
     
  3. eanbowman

    eanbowman

    Joined:
    May 26, 2017
    Posts:
    5
    I figured it out. The instance actually had two animators. One on the container object and one on the model itself. I eliminated the container object as it wasn't really needed. Fixed everything. The state machine now properly affects the instance animations.