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

Question Setting the target for attacks with button

Discussion in 'Scripting' started by matthewcooper, May 12, 2020.

  1. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    Instead of clicking to attack I am actually using a button so when I press/tap on a button it does the attack animation,How do I set the enemies as the target so they take damage? I want them to take damage when I press/tap the button on screen, I'm not clicking on them with a mouse. I'll include some of my code:

    Code (CSharp):
    1. using UnityEngine;
    2. namespace Combat {
    3. public class PlayerCombat : MonoBehaviour {
    4.     public  Animator animator;
    5.     [SerializeField] Transform target;
    6.     [SerializeField] float weaponDamage = 5f;
    7.    
    8.      // Use this for initialization
    9.     void Start() {
    10.        
    11.         animator = GetComponent<Animator>();
    12.      }
    13.     public void KnightHeroAttack() {
    14.         animator.SetTrigger("KnightHeroAttack");
    15.     }
    16.     //Animation Event
    17.     void Hit() {
    18.         Health healthComponent = target.GetComponent<Health>();
    19.         healthComponent.TakeDamage(weaponDamage);
    20.     }
    21.     }
    22. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    How do you want to choose the enemy?

    Automatically choose the nearest? If so, iterate the enemies and find the closest.

    Automatically choose the most-dangerous? If so, iterate the enemies and find the most-dangerous.

    See the pattern? Programming is all about identifying patterns.
     
  3. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    I basically want to be able to attack whatever enemy I'm looking at, it's 3D in a third person view, not sure if that helps.
     
  4. HypnotistDK

    HypnotistDK

    Joined:
    Feb 14, 2016
    Posts:
    15
    ThenrThen you can use raycast or you can set a trigger collider on your weapon
     
  5. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    I've got this so far but it's not working:


    Code (CSharp):
    1.  
    2.     void Hit() {
    3.  
    4.      var hits = Physics.SphereCastAll(transform.position, 2.0f, transform.forward); //Collect objects in front of player
    5.      Health nearestEnemy = null;
    6.      float distanceCheck = 10000;
    7.      foreach(var hit in hits) {
    8.  
    9.          if(hit.transform==transform) continue; //ignore self
    10.          if(hit.distance>distanceCheck) continue;
    11.          Health potentialEnemy = hit.transform.GetComponent<Health>();
    12.          if(potentialEnemy) {
    13.              
    14.                nearestEnemy=potentialEnemy;
    15.                distanceCheck=hit.distance;
    16.          }
    17.      }
    18.      if(distanceCheck<currentWeaponConfig.GetRange()) return; //this should actually also account for no enemy found
    19.      target=nearestEnemy;
    20.      OldHit();
    21. }
    22. }
     
    Last edited: May 14, 2020
  6. HypnotistDK

    HypnotistDK

    Joined:
    Feb 14, 2016
    Posts:
    15
    It's not working you say, but what happens when you run it?
     
  7. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    I've changed the code a lot now, it's just the weapon range that isn't working now, regardless of what value I set the weaponrange to in the inspector, it doesn't change at all.


    Code (CSharp):
    1. using UnityEngine;
    2. namespace Combat {
    3. public class PlayerCombat : MonoBehaviour {
    4.     public  Animator animator;
    5.     Transform target;
    6.     [SerializeField] float weaponRange = 2f;
    7.     [SerializeField] float weaponDamage = 5f;
    8.     public object currentWeaponConfig;
    9.         void Start() {
    10.        
    11.         animator = GetComponent<Animator>();
    12.      }
    13.     public void KnightHeroAttack() {
    14.         animator.SetTrigger("KnightHeroAttack");
    15.     }
    16.     //Animation Event
    17.     void OldHit() {
    18.         Health healthComponent = target.GetComponent<Health>();
    19.         healthComponent.TakeDamage(weaponDamage);
    20.     }
    21.     void Hit() {
    22.      var hits = Physics.SphereCastAll(transform.position, 2.0f, transform.forward); //Collect objects in front of player
    23.      Health nearestEnemy = null;
    24.      float distanceCheck = 10000;
    25.      foreach(var hit in hits) {
    26.         if(hit.transform == transform) continue; //ignore self
    27.         if(hit.distance > distanceCheck) continue;
    28.         Health potentialEnemy = hit.transform.GetComponent<Health>();
    29.         if(potentialEnemy && hit.distance < distanceCheck) {
    30.             nearestEnemy=potentialEnemy;
    31.             distanceCheck=hit.distance;
    32.     }
    33.     }
    34.     if(distanceCheck < GetRange() || nearestEnemy == null) return; //this should actually also account for no enemy found
    35.     target = nearestEnemy.gameObject.transform;
    36.     OldHit();
    37. }
    38.     public float GetRange(){
    39.         return weaponRange;
    40.     }
    41.     }
    42. }