Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with animation event...

Discussion in 'Animation' started by unity_srMIybJwy0a9Rw, Jul 29, 2020.

  1. unity_srMIybJwy0a9Rw

    unity_srMIybJwy0a9Rw

    Joined:
    Jul 27, 2020
    Posts:
    13
    Code (CSharp):
    1. public class PlayerCombat : MonoBehaviour
    2. {
    3. public Animator animator;
    4. public Transform attackPoint;
    5. public LayerMask enemyLayers;
    6. public float attackRange = 0.5f;
    7. public int attackDamage = 20;
    8. public float attackRate = 2f;
    9. float nextAttackTime = 0f;
    10. // Update is called once per frame
    11. void Update()
    12. {
    13. if(Time.time >= nextAttackTime)
    14. {
    15. if (Input.GetKeyDown(KeyCode.O))
    16. {
    17. Attack();
    18. nextAttackTime = Time.time + 1f / attackRate;
    19. }
    20. }
    21. }
    22. void AttackAnimation()
    23. {
    24. animator.SetTrigger("Attack");
    25. }
    26. public void Attack()
    27. {
    28. //play attack animation
    29. AttackAnimation();
    30. //detect enemies
    31. Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
    32. //Damage them
    33. foreach (Collider2D enemy in hitEnemies)
    34. {
    35. Debug.Log("We hit" + enemy.name);
    36. enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
    37. }
    38. }
    39. private void OnDrawGizmosSelected()
    40. {
    41. if (attackPoint == null)
    42. return;
    43. Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    44. }
    45. }
    46.  
    OK, so I have two points in my attack animation where I want to cause damage. I've created an event that causes damage on the attack but it loops the attack now. I have turned off Loop on my attack animation and the problem consists. Can anyone suggest a fix?