Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Nova-1504 · Dec 03, 2016 at 02:25 PM · animationanimatornot workingindexinvalid

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nova-1504 · Dec 04, 2016 at 12:48 AM 0
Share

Need an answer as SOON AS POSSIBLE!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nova-1504 · Dec 04, 2016 at 06:15 PM 0
Share

says moveSpeed doesn't exist.

avatar image Nova-1504 · Dec 04, 2016 at 06:17 PM 0
Share

Also says moveSpeed is an invalid argument.

avatar image Nova-1504 · Dec 04, 2016 at 06:21 PM 0
Share

I'm using the Ethan prefab

avatar image Nova-1504 · Dec 04, 2016 at 06:31 PM 0
Share

See my original questions:
Q1

Q2

avatar image wesleywh Nova-1504 · Dec 04, 2016 at 06:40 PM 0
Share

I'll take a look at these.

avatar image wesleywh · Dec 04, 2016 at 06:35 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animator problem 0 Answers

How to link a follow script to the existing ThirdPersonCharacter animator 1 Answer

The animations not run on the Copied/duplicated enemies (Characters) 0 Answers

Problem with game animation for gameobject movement 0 Answers

Why is my animation not working? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges