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 weapon range not working

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

  1. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    Could someone help me with this, I've tried using debug.log in various places and it should be working but it's not. The range of my weapon doesn't change when I change the number in the inspector or anything else I do, whether it's 2, 5 or 0.1 it never changes the actual range of the weapon, by default it's set to 2 but I've noticed that it actually can attack from quite far away, I'm pretty sure further than 2. Is it possibly something to do with the distancecheck?

    If anyone knows a better way to write this then I'm happy to change it.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Combat {
    4. public class PlayerCombat : MonoBehaviour {
    5.     public  Animator animator;
    6.     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.     public void KnightHeroAttack() {
    16.  
    17.         animator.SetTrigger("KnightHeroAttack");
    18.  
    19.     }
    20.     //Animation Event
    21.     void OldHit() {
    22.  
    23.         Health healthComponent = target.GetComponent<Health>();
    24.         healthComponent.TakeDamage(weaponDamage);
    25.  
    26.     }
    27.     void Hit() {
    28.  
    29.      var hits = Physics.SphereCastAll(transform.position, 2.0f, transform.forward); //Collect objects in front of player
    30.      Health nearestEnemy = null;
    31.      float distanceCheck = 1000;
    32.      foreach(var hit in hits) {
    33.  
    34.         if(hit.transform == transform) continue; //ignore self
    35.         if(hit.distance > distanceCheck) continue;
    36.         Health potentialEnemy = hit.transform.GetComponent<Health>();
    37.  
    38.         if(potentialEnemy && hit.distance < distanceCheck) {
    39.  
    40.             nearestEnemy=potentialEnemy;
    41.             distanceCheck=hit.distance;
    42.  
    43.     }
    44.  
    45.     }
    46.  
    47.     if(distanceCheck < GetRange() || nearestEnemy == null) return; //this should actually also account for no enemy found
    48.     target = nearestEnemy.gameObject.transform;
    49.     OldHit();
    50. }
    51.     public float GetRange(){
    52.  
    53.         return weaponRange;
    54.  
    55.     }
    56.  
    57.     }
    58.  
    59. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Are you debugging what hit.distance is returning?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Line 47 is the only place you check distance. Haven't you already by that point iterated everything you hit?

    If this is an instantaneous bullet cast, I think you want to check distance on each of the hits, otherwise you've already hit them all and done stuff.

    Also why do you have both
    distanceCheck
    and
    weaponRange
    ?
     
  4. qbflack2

    qbflack2

    Joined:
    Feb 12, 2020
    Posts:
    7
    Are you using raycasts or objects.
     
  5. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    I've debugged the hit.distance and it does return what I've set the weaponrange to but the range of my attacks don't actually change.
     
  6. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    Why is the initial distance check set so high? That's probably why you're able to hit enemies from so far away. Also the SphereCast has an override with a max distance so you can just set that to your weapon range.
     
  7. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    I'm very new to doing combat like this, how would I set that?
     
  8. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    I don't know what IDE you're using but in Visual Studio if you just type in "Physics.SphereCastAll(" you can use the arrow keys up and down to cycle through the possible overrides. I'm not on my dev computer right now so I can't pull up the exact one but I know it's there because this is exactly how I set up the melee combat in the project I'm working on right now.

    Edit: It looks like I used SphereCastAll(ray, radius, max distance) for mine. To set up a Ray just do something like this:

    Code (CSharp):
    1. Ray ray = new Ray(position you want to cast from,  direction of cast);
    And then change your SphereCastAll to read like this:

    Code (CSharp):
    1. Physics.SphereCastAll(ray, radius of attack, weapon range);
    Make sure to call the ray before you do the cast.
     
    Last edited: May 23, 2020