How do I get my Enemy to use a walking animation when its moving? I have a script that makes it follow the player, but I don’t know how to incorporate a animation into it? Any ideas?
Have you set up your Animator yet? That’s the first step (read tutorials on the Animator if you have to). Make sure that your animator has a “float” type variable representing forward motion. Let’s call it “MovementForward”. I recommend you use that float in a blend tree that blends between your standing and walking (and running, if you have it) animations.
In code, you will want to compare your position to your previous position, and then set MovementForward to the difference. BUT, you don’t want this to just be the change in position; you really want it to be the speed. I recommend you also put some smoothing on that value using Mathf.Lerp or the animation may look jerky.
That part of the code will probably look like:
Animator myAnimator;
void Awake() {
myAnimator = GetComponent<Animator>();
}
float smoothedSpeed = 0f;
void LateUpdate() {
float positionChange = (transform.position - lastFramePosition).magnitude;
float thisFrameSpeed = positionChange / Time.deltaTime;
smoothedSpeed = Mathf.Lerp(smoothedSpeed, thisFrameSpeed, 0.5f);
myAnimator.SetFloat("MovementForward", smoothedSpeed);
lastFramePosition = transform.position;
}
Do you use Legacy or Animator system?
http://docs.unity3d.com/Manual/Animations.html
In Unity 5, the API changed a lot, meaning you now have to usually GetComponent from objects.
So, just like @StarManta stated in his code snippet,
Animator myAnimator;
void Start()
{
myAnimator = GetComponent<Animator>();
}
is required in case you use Animator system.
If you use Legacy animation system, then you need
Animation myAnimation;
void Start()
{
myAnimation = GetComponent<Animation>();
}