Search Unity

Raycast2D to detect walls between enemies (plz help)

Discussion in 'Scripting' started by HanbieRyu, Sep 2, 2018.

  1. HanbieRyu

    HanbieRyu

    Joined:
    Sep 2, 2018
    Posts:
    4
    Hello, I've been working on a sandbox game where the enemies chase certain targets and the targets run away from them. I was using this code that returns the closest enemy from all gameObjects tagged as "Enemy":

    Code (CSharp):
    1. GameObject FindClosestEnemy()
    2.     {
    3.         GameObject[] gos;
    4.         gos = GameObject.FindGameObjectsWithTag("Enemy");
    5.         GameObject closest = null;
    6.         float distance = Mathf.Infinity;
    7.         Vector3 position = transform.position;
    8.         foreach (GameObject go in gos)
    9.         {
    10.             Vector3 diff = go.transform.position - position;
    11.             float curDistance = diff.sqrMagnitude;
    12.            
    13.             if (curDistance < distance)
    14.             {
    15.                     closest = go;
    16.                     distance = curDistance;
    17.                     Debug.Log(hit.collider);
    18.             }
    19.         }
    20.         return closest;
    21.      }
    And I modified it so it wouldn't update the closest gameObject if there was a wall(gameObject tagged as "Wall"), which did not work:

    Code (CSharp):
    1. GameObject FindClosestEnemy()
    2.     {
    3.         GameObject[] gos;
    4.         gos = GameObject.FindGameObjectsWithTag("Enemy");
    5.         GameObject closest = null;
    6.         float distance = Mathf.Infinity;
    7.         Vector3 position = transform.position;
    8.         foreach (GameObject go in gos)
    9.         {
    10.             Vector3 diff = go.transform.position - position;
    11.             float curDistance = diff.sqrMagnitude;
    12.             RaycastHit2D hit = Physics2D.Raycast(position, go.transform.position);
    13.             if (curDistance < distance)
    14.             {
    15.                 if(!hit.collider.CompareTag("Wall"))
    16.                 {
    17.                     closest = go;
    18.                     distance = curDistance;
    19.                     Debug.Log(hit.collider);
    20.                 }
    21.  
    22.             }
    23.         }
    24.         return closest;
    25.      }
    Can somebody please help me with this? Any minor improvements would be great.
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Couple of things I spotted that might help:

    1) When you do a raycast it's just going to hit your source player straightaway unless you exclude that layer from the raycast. The 4th parameter of the Physics2D.Raycast function takes a layer mask - you can use that to make sure it only checks the ray against objects like walls.

    2) You should check that 'hit.collider' isn't null before comparing it against something (if the raycast didn't hit anything then it would be null)
     
  3. HanbieRyu

    HanbieRyu

    Joined:
    Sep 2, 2018
    Posts:
    4
    I actually did include all those in other functions(Physics2D.queriesStartinColliers etc). I think there is a problem with the algorithm? possibly, I'm not sure. Thanks for the help.