Search Unity

Question Tapping button to attack on mobile, problem with range

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

  1. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    I'm getting one error which I can't figure out how to fix, I'll include the code below, I'm trying to make it so when I tap on the attack button, it will deal damage to the closest enemy within a certain range, I just can't figure out the range part. Can someone help me with this?
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Combat {
    4. public class PlayerCombat : MonoBehaviour {
    5.     public  Animator animator;
    6.     [SerializeField] Transform target;
    7.     [SerializeField] float weaponRange = 2f;
    8.     [SerializeField] float weaponDamage = 5f;
    9.     public object currentWeaponConfig;
    10.         void Start() {
    11.        
    12.         animator = GetComponent<Animator>();
    13.  
    14.      }
    15.  
    16.      public void Update() {
    17.  
    18.          bool isInRange = Vector3.Distance(transform.position, target.position) < weaponRange;
    19.        
    20.      }
    21.     public void KnightHeroAttack() {
    22.  
    23.         animator.SetTrigger("KnightHeroAttack");
    24.  
    25.     }
    26.     //Animation Event
    27.     void OldHit() {
    28.  
    29.         Health healthComponent = target.GetComponent<Health>();
    30.         healthComponent.TakeDamage(weaponDamage);
    31.  
    32.     }
    33.     void Hit() {
    34.  
    35.      var hits = Physics.SphereCastAll(transform.position, 2.0f, transform.forward); //Collect objects in front of player
    36.      Health nearestEnemy = null;
    37.      float distanceCheck = 10000;
    38.      foreach(var hit in hits) {
    39.  
    40.          if(hit.transform == transform) continue; //ignore self
    41.          if(hit.distance > distanceCheck) continue;
    42.          Health potentialEnemy = hit.transform.GetComponent<Health>();
    43.          if(potentialEnemy) {
    44.            
    45.                nearestEnemy=potentialEnemy;
    46.                distanceCheck=hit.distance;
    47.          }
    48.      }
    49.  
    50.      if(distanceCheck < currentWeaponConfig.GetRange()) return; //this should actually also account for no enemy found
    51.      target = nearestEnemy.gameObject.transform;
    52.      OldHit();
    53. }
    54.  
    55.     }
    56.  
    57. }
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    your
    currentWeaponConfig
    is of type
    object
    which represents any kind of object (the base of every type in C#). Probably you want to specify the actual type of the variable in the first place like
    public WeaponConfig currentWeaponConfig;
    (Assuming that there is a class or struct named "WeaponConfig").
    Or, if you have several different classes for such config but they share some methods like "GetRange()" you should make a base class or interface which every config class implements and use that as type for the variable.
    If you have several WeaponConfig classes but not all of them should have GetRange() you need to box it:
    float range = (currentWeaponConfig as RangedWeaponConfig)?.GetRange() ?? 0;
     
  3. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    Last edited: May 16, 2020