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?
Answer by Sluethen · Mar 21, 2016 at 02:09 AM
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
@Sluethen no it is a 40 frame animation. It's repeating because the "distance < 3" line is getting updated every frame and since it gets updated every frame, it tries to play it.. every frame. Any ideas how to stop that?
Try setting the value to 3ft maybe it doesn't know what "3" means in measurement for all i know it might think it's 3 Galaxy's. :D
Answer by roflcopetr · Mar 21, 2016 at 09:25 PM
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);
Answer by Gimly161 · May 09, 2016 at 01:28 PM
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
O$$anonymous$$ i have found a solution
You should do :
if (distance < 10){
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, player.transform.position, step);
walking = true;
if (distance < 3){
attacking = true;
}
And for the walking animation:
if (distance < 10 && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("walking")){
anim.Play ("walking", -1, 0f);
}
Your answer
Follow this Question
Related Questions
How do I keep the Animation > Add Property window open to add several bones at once? 0 Answers
Prevent keyframe interpolation in imported model animation in Unity 1 Answer
Animation which uses trigger is played twice before going back to the previous state. 0 Answers
Mecanim, get state percent complete when a transition occurs? 0 Answers
How do I play an animation state after another animation (like a toggle)? 0 Answers