Search Unity

Raycast too Short?

Discussion in 'Physics' started by Arjuna1356, Jan 27, 2016.

  1. Arjuna1356

    Arjuna1356

    Joined:
    Jan 15, 2016
    Posts:
    3
    Hey guys, I've got a very strange problem with my project right now, and it's that a Raycast I'm trying to use to determine line-of-sight isn't working.


    http://postimg.org/image/f5iii9w8d/


    http://postimg.org/image/bzxws2dm5/

    I don't know if you can see the images I'm trying to upload, but I included the URLs just in case.

    Basically, for some reason the Raycast just seems to stop in thin air a short way away from the zombie. I've been checking through, but there are absolutely no colliders that can be interfering with it.

    The code I'm using for the line-of-sight checking is:

    Code (CSharp):
    1.  
    2. void OnTriggerStay(Collider other)
    3.     {
    4.         if(other.transform.tag == "Player")
    5.         {
    6.             if (hasTarget == false)
    7.             {
    8.                 playerInSight = false;
    9.  
    10.                 Vector3 dir = other.transform.position - transform.position;
    11.                 float angle = Vector3.Angle(dir, transform.forward);
    12.  
    13.                 if (angle < (FOV / 2))
    14.                 {
    15.                     RaycastHit hit;
    16.  
    17.                     Debug.DrawRay(raycastObj.transform.position, dir.normalized, Color.green);
    18.  
    19.                     if (Physics.Raycast(raycastObj.transform.position, dir.normalized, out hit, 999))
    20.                     {
    21.                         Debug.Log(hit.transform.tag);
    22.                         Debug.Log(hit.collider.gameObject.name);
    23.                         if (hit.transform.tag == "Player")
    24.                         {
    25.                             if (hit.transform.GetComponent<playerHealth>().isAlive)
    26.                             {
    27.                                 playerInSight = true;
    28.  
    29.                                 playerPos = hit.transform.position;
    30.  
    31.                                 hasTarget = true;
    32.                             }
    33.                         }
    34.                     }
    35.                 }
    36.             }
    37.         }
    38.     }
    39.  
    I already tried using a ludicrously large range for the Raycast, but for some reason it's still not working. And it's not registering a hit at all. The debug log is totally blank, so I'm assuming it's not going into the if statement for the Raycast function.

    In fact, I'm not sure if it's even working at all. I tried going as close as possible to the Raycast, but it's still not detecting a hit. I really have no idea what else can be done.

    Any help you can provide will be great. Thanks a lot, guys!

    Edit: Okay, I did some testing, and apparently, it sort of works if I move the Raycast object out by a few units, but even then it only detect the player if I cut across the ray as though it were a tripwire pointing straight out from the zombie, and even then only at a close range...
     
    Last edited: Jan 27, 2016
  2. Arjuna1356

    Arjuna1356

    Joined:
    Jan 15, 2016
    Posts:
    3
    Ach...blast... Figured it out. Apparently, the Raycast was casting at an angle upwards, which more often than not meant it was aimed at the sky. Fixed it by setting the target direction's Y attribute to zero before processing it. Thanks for anybody who took a look!