Why is my animation is starting every frame?

using UnityEngine;
using System.Collections;

public class GoblinAttack : MonoBehaviour {
	public GameObject goblin;
	public float distance;
	public Animator anim;
	public float speed;
	public float health;
	public GameObject player;
	public bool walking = false;
	public bool attacking = false;


	// Use this for initialization
	void Start () {
		anim = GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void Update () {
		float step = speed * Time.deltaTime;

		goblin = GameObject.Find ("firstgoblin");
		player = GameObject.Find ("FPSController");
		distance = Vector3.Distance(goblin.transform.position, player.transform.position);
	

		if (distance < 10){
			transform.position = Vector3.MoveTowards(transform.position, player.transform.position, step);
			walking = true;
			anim.Play ("walking", -1, 0f);

			if (distance < 3){
				attacking = true;

			}

		}

	}

		
}

I have this script and it is supposed to make the goblin move toward me and start the walking animation if I’m closer than 10 units. It moves me and starts the animation, but every frame… It is checking the distance every frame, and if the distance is less than 10, it starts the animation over. How do I make the animation play through before it loops?

Maybe the animation is going too fast you may have out it too much together I can’t really tell without the project files but try that first

The problem is here:

anim.Play ("walking", -1, 0f);

In the documenation it states that the third parameter is used for the start time of the animation. So you always reset it to 0. Try just to omit it, i. e.

anim.Play ("walking", -1);

I don’t know if you still want to know the answer but I’m just going to say it

In your script the animation will START play if the distance is less than 10. because of the if statement your script will check every FRAME if the distance is less than 10, if so it will restart the animation. so your animation will restart every frame.

I have a kind of the same problem but also don’t know how to solve this