Search Unity

Question How do i do a special attack?

Discussion in 'Getting Started' started by MYM_Chapa, Jan 8, 2023.

  1. MYM_Chapa

    MYM_Chapa

    Joined:
    Dec 6, 2022
    Posts:
    15
    Hello guys, I've been thinking about how I can do a special attack. I want to do that when I press the letter E it does a lot of punches and stops after a while, for example it does a barrage of punches and then stops after 3 seconds, I already have the code for the combat system (i just follow a tutorial from Youtube:
    ) and i was wondering if someone can help me to make it.
    Code (CSharp):
    1.  public Transform AttackPoint;
    2.     public float attackRange = 0.5f;
    3.     public LayerMask enemyLayer;
    4.     public int attackDamage = 40;
    5.     public float attackRate = 2f;
    6.     float nextAttackTime = 0f;
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         //m1's
    12.         if(Time.time >= nextAttackTime)
    13.         {
    14.             if (Input.GetButtonDown("Fire1"))
    15.             {
    16.                 attack();
    17.                 nextAttackTime= Time.time + 1f / attackRate;
    18.  
    19.             }
    20.         }
    21.        
    22.        
    23.         //Barrage
    24.         if (Input.GetKey(KeyCode.E))
    25.         {
    26.  
    27.            
    28.  
    29.         }
    30.        
    31.     }
    32.     void attack()
    33.     {
    34.         //Detect enemies in the range of attack
    35.  
    36.         Collider2D[] hitEnemy = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayer);
    37.        
    38.         //Damage enemy
    39.  
    40.         foreach(Collider2D enemy in hitEnemy)
    41.         {
    42.             enemy.GetComponent<Dummy>().TakeDamage(attackDamage);
    43.  
    44.         }
    45.     }
    46.  
    47.     private void OnDrawGizmos()
    48.     {
    49.       if(AttackPoint == null)
    50.         { return; }
    51.  
    52.  
    53.  
    54.         Gizmos.DrawWireSphere(AttackPoint.position, attackRange);
    55.     }
    56. }
    57.  
    also i apologize for my grammar.
     
  2. MYM_Chapa

    MYM_Chapa

    Joined:
    Dec 6, 2022
    Posts:
    15
    I would also like to make it able to attack in the direction I'm facing
     
  3. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    Well done for getting it to work (mostly), also don't worry about your spelling/grammar, I've seen a LOT worse on here and other places :)
     
  4. MYM_Chapa

    MYM_Chapa

    Joined:
    Dec 6, 2022
    Posts:
    15
    thank you so much, appreciate it