Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Cast ray from one specific object to another

Discussion in 'Scripting' started by mikedpw00, Jul 22, 2010.

  1. mikedpw00

    mikedpw00

    Joined:
    Jul 9, 2010
    Posts:
    7
    I'm trying to set up a situation where an enemy game object continuosly checks to see if the player object is "visible" if it is within a certain distance of the player. In other words, I want to make it so the enemy will not start chasing/attacking the player if there is a physical object between the player and the enemy (such as a wall in the level) that would block the enemy from seeing the player and therefore knowing that the player is even there.

    If there is an easy way to just provide two objects and have unity cast a ray from one to the other, I haven't found it. So far as I can tell I have to somehow find otu what direction the player is in relative to the enemy's orientation and provide that direction in a raycast statement. I don't know how to find that direction though. Ideas?
     
  2. CaptainKiyaku

    CaptainKiyaku

    Joined:
    Feb 8, 2009
    Posts:
    324
    Hm maybe this will work:

    Code (csharp):
    1.  
    2. float maxRange = 5;
    3. RaycastHit hit;
    4.  
    5. if(Vector3.Distance(transform.position, player.position) < maxRange )
    6. {
    7.     if(Physics.Raycast(transform.position, (player.position - transform.position), out hit, maxRange))
    8.     {
    9.         if(hit.transform == player)
    10.         {
    11.             // In Range and i can see you!
    12.         }
    13.     }
    14. }
    15.  
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  4. mikedpw00

    mikedpw00

    Joined:
    Jul 9, 2010
    Posts:
    7
  5. mikedpw00

    mikedpw00

    Joined:
    Jul 9, 2010
    Posts:
    7
    Actually, there's a bit of a problem. If you read the description of linecast it says it looks to see if a line going from the one object to the other intersects anything else in between. I would have thought that I should still be able to use this to tell if the enemy could see the player by just reversing the logic ( or make it so the enemy only sees the player if linecast returns false). However linecast seems to return true whether or not there are any objects between the player and the enemy, and I can't figure out why.

    Actually, I think I might know why. I think the enemy and player meshes are probably the colliders in the way. If I understand correctly I just need to figure out how to use this layermask doohicky.
     
  6. reculum

    reculum

    Joined:
    May 16, 2010
    Posts:
    60
    yes you could use layermasks, or you simply do this:
    (not spellchecked, pseudocode)

    Code (csharp):
    1.  
    2. source.layer = 2; //IgnoreRaycast Layer
    3. target.layer = 2;
    4.  
    5. linecast (...) // as mentioned above
    6.  
    7. source.layer = 0; //Normal Layer
    8. target.layer = 0;
    9.  
    additionally, you should decide if you really NEED the
    "out hintinfo", because if you only want to see IF there are obstacles - and not, where exacrly they are,
    you should use the overload method where no hitinfo is provided because this makes a difference in performance.

    hope than helped
     
  7. sprogobalionas

    sprogobalionas

    Joined:
    Dec 25, 2020
    Posts:
    2
    Nice code, but instead of multiple IF's you could've done it in one IF with using "&&".
     
    Powzone likes this.
  8. BPRICE

    BPRICE

    Joined:
    Mar 1, 2020
    Posts:
    8
    This worked perfectly for me!! Thank you.
     
  9. jshull

    jshull

    Joined:
    Nov 4, 2021
    Posts:
    3
    This only works if the object your using to raycast to another is at world position of 0, 0 ,0