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 02, 2016 at 07:34 PM · animationanimatornot workingfollowrun

How to link a follow script to the existing ThirdPersonCharacter animator

I'm trying to link a character's follow script to the existing ThirdPersonCharacter (Ethan) animator, but the animation doesn't play. I get no errors. Here is the current script, I used part of a tutorial script:

  using UnityEngine;
  using System.Collections;
  
  public class Stalker : MonoBehaviour
  {
        Animator anim;
     int jumpHash = Animator.StringToHash("Jump");
     int runStateHash = Animator.StringToHash("Base Layer.Run");
      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);
  
      AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
     if(stateInfo.nameHash == runStateHash)
     {
             anim.SetTrigger (jumpHash);
         }
      
          this.transform.position = newPosition;
          this.transform.LookAt (target.position, this.transform.up);
      }
  }

I just realized the "Jump" will always be active with the current script, but i'll fix that later. If you are willing, post the script. PLEASE HELP, NEED AN ANSWER AS SOON AS POSSIBLE!

Comment
Add comment · Show 7
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 YellowUromastyx · Dec 02, 2016 at 07:48 PM 1
Share

What have you tried to fix the problem?

avatar image Nova-1504 YellowUromastyx · Dec 02, 2016 at 07:53 PM 0
Share

Before getting this script, I originally tried to use Nav$$anonymous$$esh: link

I tried meddling with the code, deleting and undeleting, doing stuff from the Animator Scripting Unity tutorial.

avatar image YellowUromastyx Nova-1504 · Dec 02, 2016 at 07:58 PM 1
Share

I'll probably be able to help you with your nav mesh, it'll make things a lot easier for you when making an object follow you.

Show more comments
avatar image Nova-1504 · Dec 02, 2016 at 08:05 PM 0
Share

It also says "Speed" doesn't exist.

avatar image Nova-1504 · Dec 03, 2016 at 12:13 AM 0
Share

Ok, I got a little closer:

  using UnityEngine;
  using System.Collections;
  
  public class Stalker : $$anonymous$$onoBehaviour
  {
        Animator anim;
     int runStateHash = Animator.StringToHash("Base Layer.Run");
      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.$$anonymous$$oveTowards(this.transform.position, target.position, fSpeed * Time.deltaTime);
  
     if(fSpeed > 0.001)
     {
     anim.Play ();
     }
 
      
          this.transform.position = newPosition;
          this.transform.LookAt (target.position, this.transform.up);
      }
  }
  

The only thing I need is the argument for anim.Play ();. (It's a string)

avatar image Nova-1504 Nova-1504 · Dec 03, 2016 at 12:30 AM 0
Share

Ok, now I figured THAT out, but I don't know the name of the walking animation. It gives me infinite errors on Play (Invalid layer index "-1".

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by wesleywh · Dec 04, 2016 at 06:57 PM

If you're using a Navmesh, reading from your comments you are, it makes life so much easier.

Attach a "NavMeshAgent" to your NPC that will be following someone. Make sure that the area has a navmesh attached to it and has been baked. If you don't know how to do this there is a video tutorial here:

https://www.youtube.com/watch?v=mP7ulMu5UkU

On the "NavMeshAgent" you can also state how close you want the NPC/Object to get to its destination (follow distance in our case). Then in our code its as easy as adding the following to make the NPC/Object travel to its destination (in your case the player):

 this.GetComponent<NavMeshAgent>().destination = player.transform.position;

An example is also here in the unity docs:

https://docs.unity3d.com/Manual/nav-MoveToDestination.html

So really you could change:

 void Update ()
       {
           this.Follow (this.followTarget, fSpeed: this.followSpeed);
       }

to

 void Update ()
       {
           this.GetComponent<NavMeshAgent>().destination = this.followTarget;
       }

Then you could completly remove the Follow function all together. The NavMeshAgent will avoid walls and calculate the best path to get to your player at the agents set speed. Simply set the settings in the inspector for the NavMeshAgent on how fast you want it to move, how tall it is, the height of the slope it can travel up, etc.

The navmesh agent will slowly rotate to look at it's destination. If you want it to snap to look at it's destination then place

 this.transform.LookAt (target.position, this.transform.up);

back into your update and it will alway be looking at your target (snap to it).

Comment
Add comment · Show 1 · 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 07:49 PM 0
Share

Look at the falling animation problem, it's based on Nav$$anonymous$$esh. The instant I remove the Nav static from the floor, the falling stops, but the character doesn't follow.

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

Animation not working, invalid layer index "-1" 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