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 /
avatar image
0
Question by PolyvivalStudios · May 22, 2018 at 01:19 PM · animationscript.coroutine

Animation sometimes doesn't play until the end, sometimes it doesn't play at all and sometimes it plays more than once.

using UnityEngine; using System.Collections;

public class Enemy : MonoBehaviour {

 public Animator animator;

 private void Start()
 {
     animator = gameObject.GetComponent<Animator>();
 }

 public float health = 100f;




 IEnumerator Die()
 {
     animator.ResetTrigger("isWalking");
     animator.ResetTrigger("isAttacking");
     animator.SetTrigger("isDead");
     yield return new WaitForSeconds(seconds: 0.80f);
     animator.ResetTrigger("isDead");
     Destroy(gameObject);
 }


 public void TakeDamage(float damage)
 {
     health -= damage;
     if (health <= 0f)
     {
         StartCoroutine(Die());
     }
 }

} alt text

using System.Collections; using UnityEngine; using UnityEngine.AI;

public class EnemyController : MonoBehaviour {

 public float lookRadius = 10f;
 private bool isWalking = false;
 public Animator animator;
 

  // public Animation walkAnimation; 

 Transform target;
 NavMeshAgent agent;

 // Use this for initialization
 void Start () {
     target = PlayerManager.instance.Player.transform;
     agent = GetComponent<NavMeshAgent>();
     
 }

 // Update is called once per frame
 void Update()
 {
     float distance = Vector3.Distance(target.position, transform.position);

     if (distance <= lookRadius)
     {
         animator.SetBool("isWalking", true);
         animator.SetBool("isAttacking", false);

         agent.SetDestination(target.position);

         if (distance <= agent.stoppingDistance)
         {
             FaceTarget();
             animator.SetBool("isWalking", false);
             animator.SetBool("isAttacking", true);
             StartCoroutine(WaitBeforeAttack());
             
            
         }
     }
 }
     
 IEnumerator WaitBeforeAttack()
 {
     yield return new WaitForSeconds(1);
     TakeDamage();
 }
 void TakeDamage()
 {
     GameObject.Find("Player").GetComponent<PlayerHealth>().playerHealth -= 10;
 }





 void FaceTarget ()
 {
     Vector3 direction = (target.position - transform.position).normalized;
     Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
     transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 7f);
 }

 

}

untitled.png (31.2 kB)
Comment
Add comment · Show 2
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 PolyvivalStudios · May 22, 2018 at 05:46 PM 0
Share

Please help me!

avatar image winterfluxstudio PolyvivalStudios · May 22, 2018 at 06:04 PM 0
Share

no offence but 1) you haven't posted a full code example so we can see what you might be missing (where is TakeDamage() called from?) and 2) your code example needs to be formatted correctly (the top 2 lines are just plain text) which takes like... 10 seconds to fix? putting more effort into your question and how your format it will most likely result in people being more willing to help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by winterfluxstudio · May 22, 2018 at 06:01 PM

Try adding debug logs and see when/if they trigger to get a better idea what might be causing it.

It could potentially be an issue with your animation controller parameters. What are your animation state trasitions like? For example, one particular settings forces the AC to wait for the animation to "finish" before moving to the next animation. You can "Break" this to quickly switch between animations.

 using System.Collections; 
 using UnityEngine; 
 using UnityEngine.AI;
     
 public class EnemyController : MonoBehaviour {
     
     public float lookRadius = 10f;
     private bool isWalking = false;
     public Animator animator;
      
     // public Animation walkAnimation; 
     Transform target;
     NavMeshAgent agent;
     
     void Start () 
     {
         target = PlayerManager.instance.Player.transform;
         agent = GetComponent<NavMeshAgent>();
     }
     
     void Update()
     {
         float distance = Vector3.Distance(target.position, transform.position);
 
         if (distance <= lookRadius)
         {
             animator.SetBool("isWalking", true);
             Debug.Log("isWalking = true");
             animator.SetBool("isAttacking", false);
             Debug.Log("isAttacking= false");
             agent.SetDestination(target.position);
     
             if (distance <= agent.stoppingDistance)
             {
                  Debug.Log("call FaceTarget()");
                  FaceTarget();
                  animator.SetBool("isWalking", false);
                  Debug.Log("isWalking = false");
                  animator.SetBool("isAttacking", true);
                  Debug.Log("isAttacking = true");
                  StartCoroutine(WaitBeforeAttack());
             }
         }
     }
          
     IEnumerator WaitBeforeAttack()
     {
         yield return new WaitForSeconds(1);

         TakeDamage();
     }
     
     void TakeDamage()
     {
         GameObject.Find("Player").GetComponent<PlayerHealth>().playerHealth -= 10;
     }
     
     void FaceTarget ()
     {
         Vector3 direction = (target.position - transform.position).normalized;
         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 7f);
     }
      
 }
 
 


Comment
Add comment · Show 4 · 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 PolyvivalStudios · May 22, 2018 at 06:48 PM 0
Share

I'll do it now.

avatar image winterfluxstudio PolyvivalStudios · May 22, 2018 at 07:38 PM 0
Share

updated (with debug logs). a common issue with animation is the state machine and how it works. For example, your issue could potentially be due to the "exit time" of an animation. This means the system will wait for that animation to finish playing before moving to the next animation state. This can cause some user input or transitions to not register. (try this). click on the animation state connection (the white line) between two animations and deselect "has exit time". Re-test with Debug logs to see if problem is resolved.

avatar image PolyvivalStudios winterfluxstudio · May 22, 2018 at 07:46 PM 1
Share

I'll try that! Edit: The animations work PERFECTLY now, thanks to you. God Bless you.

Show more comments

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

230 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 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 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 add an AnimationEvent to an animation of a single instance with script? 0 Answers

How to enable an animation then disable after it is done playing no matter what fps you are running? 1 Answer

CORoutine does not completely stop 1 Answer

Animation created via script does not play 1 Answer

animator.GetCurrentAnimatorStateInfo(0).IsName not working 1 Answer


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