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!
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.
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.
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)
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".
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).
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.