Animation not working, invalid layer index "-1"
With this script, i get infinite invalid layer index errors and the animation doesn't play:
using UnityEngine;
using System.Collections;
public class Stalker : MonoBehaviour
{
public Animator anim;
public float followSpeed = 0.1f;
public Transform followTarget;
void Awake ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
this.Follow (this.followTarget, fSpeed: this.followSpeed);
}
void Follow (Transform target, float fSpeed = 1)
{
Vector3 newPosition = Vector3.MoveTowards(this.transform.position, target.position, fSpeed * Time.deltaTime);
anim.Play ("HumanoidRun");
this.transform.position = newPosition;
this.transform.LookAt (target.position, this.transform.up);
}
}
Why? What is the proper way to do this? I already watched the tutorial on animator scripting.
Please help!
Answer by wesleywh · Dec 04, 2016 at 04:54 AM
Hum. Do you have only 1 movement animation? By the look of this script I would guess so. Also it looks like your using the new Mecanim system is that right? So your using the following
anim.Play ("HumanoidRun");
Which is the following
public void Play(string stateName, int layer = -1, float normalizedTime = float.NegativeInfinity);
Doc found here: https://docs.unity3d.com/ScriptReference/Animator.Play.html
However it would be a lot easier to go from your idle animation to a running animation using a bool or speed.
Play animation based on speed
private Vector3 lastPos = Vector3.zero; //for speed calculation
private float moveSpeed = 0.0f;//declare variable
//how fast player going, place in Update()?
moveSpeed = Vector3.Distance (lastPos, this.transform.position) / Time.deltaTime;
lastPos = this.transform.position;
anim.SetFloat("speed",moveSpeed);
So open your animator and add the "speed" float and play it when it is greater than 0.1 for example. For a blend tree you would want to have a direction parameter as well as a speed parameter.
So with that all said change
void Update ()
{
this.Follow (this.followTarget, fSpeed: this.followSpeed);
}
To
void Update() {
//how fast player going, place in Update()?
moveSpeed = Vector3.Distance (lastPos, this.transform.position) / Time.deltaTime;
lastPos = this.transform.position;
anim.SetFloat("speed",moveSpeed);
this.Follow (this.followTarget, fSpeed: this.followSpeed);
}
and add the following to the top of the script
private Vector3 lastPos = Vector3.zero; //for speed calculation
private float moveSpeed = 0.0f;//declare variable
You must have just tried to copy and paste this code and click play. The code above was missing declarations, I have added it. That was my mistake. That's why you say it said "moveSpeed doesn't exist" and "moveSpeed is an invalid argument". This is because "moveSpeed" wasn't declared. Then it was trying to use the undeclared value in the function. It could also can be declared within the Update function as well but declaring it outside the update function actually saves on memory, kinda a $$anonymous$$uscule amount but still it can add up with many other scripts.