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

how can i turn this script touch controlled with button

Discussion in '2D' started by krabbypatty123, May 28, 2020.

  1. krabbypatty123

    krabbypatty123

    Joined:
    Jan 11, 2020
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;please explein every detail its very importent to me
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playercombat : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.  
    9.  
    10.  
    11.     public Transform AttackPoint;
    12.  
    13.     public float AttackRange = 0.5f;
    14.  
    15.     public LayerMask enemyLayers;
    16.  
    17.     public int attackDamage = 40;
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Input.GetKeyDown(KeyCode.Space))
    22.         {
    23.             Attack();
    24.         }
    25.     }
    26.  
    27.  
    28.     void Attack()
    29.     {
    30.         animator.SetTrigger("Attack");
    31.  
    32.         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, AttackRange, enemyLayers);
    33.  
    34.         foreach(Collider2D enemy in hitEnemies)
    35.         {
    36.             enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
    37.         }
    38.     }
    39.  
    40.  
    41.  
    42.  
    43.     void  OnDrawGizmosSelected()
    44.     {
    45.  
    46.         if (AttackPoint == null)
    47.             return;
    48.         Gizmos.DrawWireSphere(AttackPoint.position, AttackRange);
    49.     }
    50.  
    51.      
    52.     }
    53.  
     
  2. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    did you try

    Code (CSharp):
    1.   if (Input.GetMouseButtonDown(0))
    2.         {
    3.             Attack();
    4.         }
     
  3. krabbypatty123

    krabbypatty123

    Joined:
    Jan 11, 2020
    Posts:
    4
    yes thanku you saved me
     
    brigas likes this.