Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Player attack sometimes wont work

Discussion in 'Scripting' started by Smiutschi, Jan 24, 2022.

  1. Smiutschi

    Smiutschi

    Joined:
    Jan 13, 2022
    Posts:
    8
    Hey Guys, I have a problem. Im pretty new to Unity and i cant find a solution. I added melee Combat to my Platformer and it works (mostly). I added "timeBtwAttack" so the Player is not able to Spam the Attack Key.
    My problem is that sometimes the Attack wont work. For example: I set the timeBtwAttack on 0.3 and press the Attackbutton 5 Times. Sometimes the first attack wont go off, sometimes 3 Attacks go off rly fast and the last 2 attacks wont work etc...
    What i want is:
    If i press the Attackbutton the Player should stand still and attack. After the attack the player should be able to move again.
    But the most important thing is that the attack should go off when i press the button.
    Maybe somebody can help me :)

    Thanks :)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerAttack : MonoBehaviour
    6. {
    7.     private Animator anim;
    8.     private float timeBtwAttack;
    9.     public float startTimeBtwAttack;
    10.  
    11.     public Transform attackLocation;
    12.     public float attackRange;
    13.     public LayerMask enemies;
    14.     public int damage;
    15.  
    16.  
    17.     private void Start()
    18.     {
    19.         anim = GetComponent<Animator>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         if (timeBtwAttack <= 0)
    25.         {
    26.             if (Input.GetButton("Fire1"))
    27.             {
    28.            
    29.                 anim.SetBool("isAttacking", true);
    30.                 Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackLocation.position, attackRange, enemies);
    31.  
    32.                 for (int i = 0; i < enemiesToDamage.Length; i++)
    33.                 {
    34.                     enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
    35.                 }
    36.             }
    37.             timeBtwAttack = startTimeBtwAttack;
    38.         }
    39.         else
    40.         {
    41.             timeBtwAttack -= Time.deltaTime;
    42.             anim.SetBool("isAttacking", false);
    43.         }
    44.      
    45.      
    46.  
    47.     }
    48.  
    49.     private void OnDrawGizmosSelected()
    50.     {
    51.         Gizmos.color = Color.red;
    52.         Gizmos.DrawWireSphere(attackLocation.position, attackRange);
    53.     }
    54. }
     
    Last edited: Jan 24, 2022
  2. brkyldrn

    brkyldrn

    Joined:
    May 24, 2020
    Posts:
    1
    I think there might be something missing with your "startTimeBtwAttack" value. Try initiating it, since it is not initiated, perhaps unwanted values might be assigned to that, thus occurring undesired attack cycles. Not sure if this helps tho. Good luck.
     
  3. unity_-QZbFZCi2xrPpQ

    unity_-QZbFZCi2xrPpQ

    Joined:
    Apr 21, 2021
    Posts:
    2
    For any one else having this problem just go watch brackeys video abour melle combat. He implements the attack rate at 18.51. Thats what worked for me.