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 mangonoah · Mar 21, 2016 at 12:45 AM · animationanimation clipanimationstate

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?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · 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 mangonoah · Mar 21, 2016 at 02:26 AM 0
Share

@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?

avatar image Sluethen · Mar 21, 2016 at 05:06 PM 0
Share

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

avatar image
0

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);


Comment
Add comment · 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
0

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

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 Gimly161 · May 09, 2016 at 02:58 PM 0
Share

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

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

86 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

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


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