Search Unity

My Raycast isn't Hitting the Target

Discussion in 'Scripting' started by RaGEn, Jul 25, 2017.

  1. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Hello,

    I'm working on a game where the AI uses a raycast to find their target. However for some reason when the Human AI uses their raycast it goes off in a random direction. It doesn't even try to hit the enemy. The Alien's raycast works perfectly though.

    If anyone could shred some light on why this is occurring I'd be very grateful.

    Code (CSharp):
    1.     GameObject FindClosestEnemy()
    2.     {
    3.         GameObject[] gos;
    4.         gos = GameObject.FindGameObjectsWithTag(EnemyFaction);
    5.         GameObject closest = null;
    6.         float distance = Mathf.Infinity;
    7.         Vector3 position = transform.position;
    8.  
    9.         foreach (GameObject go in gos)
    10.         {
    11.             Vector3 diff = go.transform.position - position;
    12.             float curDistance = diff.sqrMagnitude;
    13.             RaycastHit hit;
    14.             BehindWall = true;
    15.             Debug.DrawRay(transform.position, go.transform.position, Color.green);
    16.             if (go.transform.FindChild ("Marksmen") == true)
    17.             {
    18.                 Attackable = false;
    19.             }
    20.  
    21.             if (go.transform.FindChild ("Marksmen") == false)
    22.             {
    23.                 Attackable = true;
    24.             }
    25.             if (Physics.Raycast (transform.position, (go.transform.position - transform.position), out hit) && FriendlyFaction == ("Humans"))
    26.             {
    27.                 print (hit.collider.gameObject.name);
    28.             }
    29.  
    30.             if (Physics.Raycast (transform.position, (go.transform.position - transform.position), out hit) && hit.collider.gameObject.tag == (EnemyFaction))
    31.             {
    32.                 BehindWall = false;
    33.             }
    34.  
    35.             if (Physics.Raycast (transform.position, (go.transform.position - transform.position), out hit) && hit.collider.gameObject.tag == ("Wall"))
    36.             {
    37.                 BehindWall = true;
    38.             }
    39.             if (curDistance < distance && BehindWall == false && Attackable == true)
    40.             {
    41.                 closest = go;
    42.                 distance = curDistance;
    43.             }
    44.         }
    45.         return closest;
     
  2. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Raycast wants a direction or point A to point B
    When you take the enemy transform.posion and subtract your position, you are getting the difference between them.
    so I would remove the subtraction part and give it a try.
    if (Physics.Raycast (transform.position, go.transform.position, out hit)
     
    RaGEn likes this.
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    That is not correct. The first parameter is the origin position, the second parameter is a direction vector. You're thinking of Linecast.
     
    RaGEn likes this.
  5. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Sadly that doesn't seem to be helping, thanks for the try.
     
  6. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    this helped me, it's worth checking out:
     
    RaGEn likes this.
  7. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Thanks for all the help guys, I've switched to a linecast and that seems to be working just fine!
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Glad you got it working!

    Just so you know, this line wasn't accurately showing you where your ray was going:
    Code (CSharp):
    1. Debug.DrawRay(transform.position, go.transform.position, Color.green);
    You may have an easier time formatting like this:
    Code (CSharp):
    1. Vector3 direction = (go.transform.position - transform.position).normalized;
    2. Ray ray = new Ray(transform.position, direction);
    3.  
    4. RaycastHit hit;
    5. bool hitSomething = Physics.Raycast(ray, out hit); // distance defaults to infinity
    6.  
    7. if(hitSomething) {
    8.     // do stuff
    9. }
    10.  
    11. // DrawRay will draw from origin to (origin + direction), so you need to multiply in the distance.
    12. Debug.DrawRay(ray.origin, ray.direction * Mathf.Infinity, hitSomething ? Color.green : Color.red);
    Additionally, it seems that your logic is doing multiple identical raycasts per foreach iteration, which seems unnecessary. You should only need one raycast per loop, and then check the results for all your secondary conditionals.
     
    sirhamy and RaGEn like this.
  9. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Wow thanks for that knowledge! I really appreciate you taking time to expand on this for me. Raycasts and linecasts aren't my strong point by any means, as you can probably tell. But this nice break down helps a lot!
     
    LiterallyJeff likes this.