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. Dismiss Notice

Physics.OverlapSphere turret is detecting itself as the nearest target.

Discussion in 'Scripting' started by THECAKEISALIE93, Feb 24, 2015.

  1. THECAKEISALIE93

    THECAKEISALIE93

    Joined:
    Sep 16, 2013
    Posts:
    30
    The script I am using for my turret is using an OverlapSphere to detect an array of objects near the turret. Now the issue I am running into is that the turret is seeing itself as the nearest target and nothing else. I have tried using a layermask that made the turret only look for objects on the layer called "Enemy", that did not have any positive results. I have been at this for hours and I cannot find a solution. I may have been using the layermask incorrectly, more than likely I was. If anyone has an idea of how the turret can stop targeting itself, please throw in your two cents. Thanks ahead of time, every post is appreciated!!! :D

    Code (JavaScript):
    1. var target : Transform;                        //the target that the turret is focused on
    2. var targets : Collider[];                    //List of objects near the turret
    3. var range : int;                            //Distance from turret to enemy
    4. var aiming = false;                            //Is turret aiming?
    5. var turretVertical : Transform;                //part of turret that rotates up/down
    6.  
    7. function Start () {
    8.  
    9. }
    10.  
    11. function Update () {
    12. target = transform;
    13. FindTargets();
    14. DetermineTarget();
    15.  
    16. if(aiming) {
    17.     LookAtTarget();
    18.     }
    19.  
    20. }
    21.  
    22. function DetermineTarget() {
    23.     aiming = false;
    24.     if(targets.length >= 1) {
    25.         for (var i = 0; i < targets.length;i++) {
    26.             if(targets[i].transform.tag == "Enemy") {
    27.                 aiming = true;
    28.                 if(target == transform) {
    29.                 }else{
    30.                 if(Vector3.Distance(targets[i].transform.position, transform.position)<= Vector3.Distance(target.transform.position, transform.position)){
    31.  
    32.                     target = targets[i].transform;
    33.                        
    34.                     }
    35.                 }
    36.             }
    37.         }
    38.     }
    39. }
    40.  
    41. function FindTargets() {
    42.     targets = Physics.OverlapSphere(transform.position, range);
    43.  
    44.  
    45. }
    46.  
    47. function LookAtTarget() {
    48.     transform.LookAt(target.transform.position);
    49.     transform.eulerAngles.x = 0;
    50.     transform.eulerAngles.z = 0;
    51.     turretVertical.transform.LookAt(target.transform.position);
    52.    
    53.    
    54. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    One idea is to turn off the collider on yourself (set its .enabled field to false), then do the Physics.OverlapSphere call, then turn the collider back on.
     
  3. THECAKEISALIE93

    THECAKEISALIE93

    Joined:
    Sep 16, 2013
    Posts:
    30
    Now when you mean the collider on myself, do you mean the turret? Because I just tried that and it acted like it wanted to work, but it ended up targeting itself permanently. Something I want to add, it is an automated turret that is not controlled by the player.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    Looking at the docs here:

    http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

    It has some limitations, such as it only checks bounding box. Did you notice that?

    What I mean is, turn off the collider on the thing doing the check, so that as far as unity is concerned, it doesn't even HAVE a collider at the moment you are collecting objects that are in the sphere, then turn it back on.
     
  5. THECAKEISALIE93

    THECAKEISALIE93

    Joined:
    Sep 16, 2013
    Posts:
    30
    I just added a line that disabled the collider and then re-enable it after it does the check. The turret didn't even acknowledge any of the near objects for the array and did not set a target. In a way it fixed it but now it isn't even building an array of near objects.
     
  6. THECAKEISALIE93

    THECAKEISALIE93

    Joined:
    Sep 16, 2013
    Posts:
    30
    I just removed lines 28,29,30,34 and 35 and now the targeting works perfectly. The only drawback is that those lines were supposed to make the turret fire at the enemy that was the closest out of two or more. For example, if there were three enemies, one of them was in the front and two were following behind. The turret would choose the one in the front. But if one of the enemies from the back moved closer than the one in the front, the turret would fire at them instead.
     
  7. hballamco

    hballamco

    Joined:
    Nov 15, 2019
    Posts:
    3
    Thank you from the future :)
     
    Kurt-Dekker likes this.