Search Unity

Question Raycast Line detecting only in X vector?

Discussion in 'Physics' started by LazyGhost15, Dec 9, 2023.

  1. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    120
    Ok, so I made a script for my enemy that says if the player and the enemy are separated by a collider the player isn't detected and the enemy will not chase it, but if the player and enemy have no collider between them then the enemy chases the player. At least that is what I thought I did. For some reason only when the player is in the same position in the X vector does the Raycast detect that no collider is between them, but if the player isn't in the same X position as the enemy then the enemy doesn't detect the player. Here is the script of the enemy, I checked and the detection bool is the only thing stopping the enemy (The Raycast decides whether the bool is true or false) and the player nor the enemy has any other colliders to block the raycast:
    Code (CSharp):
    1. ISPlayerDetected = Physics.Raycast(transform.position, Player.transform.position);
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
  3. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    120
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    LazyGhost15 likes this.
  5. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    120
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    You should still use Linecast though because a raycast requires you calculate both the normalised direction and distance which is pointless waste if you wish to detect between two world points i.e. a line segment that you already have at hand.
     
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,442
    I had always wondered which API was calling the other API internally. I kinda wish the docs would share that sort of insight.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Does that actually happen in 3D physics? I'm not sure if the implementation detail is docs-worthy or how it'd help? If it's the same thing and there's no perf benefit then yes, worth mentioning for sure! I've never took a look at it TBH.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
  10. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,442
    Well, now that we see that Linecast is just a helper calling Raycast, your advice above has no performance benefit or loss (except a possible internal redundant .normalize), it's just deciding if you use the helper or not. But if it were the other way around, calculating the arguments before the API just calculated the inverse arguments would be wasteful.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Absolutely, so convenience only which is still a great advantage to the above question.
     
  12. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    120
    I tried Linecast and for some reason, it always returns true even tho no collider is intersecting the line between the player and the enemy.
    This is the line:
    Code (CSharp):
    1.  ISPlayerDetected = Physics.Linecast(this.transform.position, Player.transform.position);
    2.  
    I checked the bool and it always returns true unless the player is very very close to the enemy
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Well as indicated above, it's the same call as raycast so there cannot be any behavioural difference, only changes to the arguments you provide.

    If you don't specify distance in a raycast, it'll be out at infinity. You're also not specifying any layermask to filter stuff so would pick up the player too if it overlaps.
     
  14. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,442
    It's returning false when both this object and the player object overlap the ray's starting point.

    For your purposes, you're going to have to call one of the forms of Linecast or Raycast which returns more information about WHAT was hit by the ray. Look for the
    out RaycastHit hit
    forms of these functions. If the collider that was hit by the ray is associated with the player, then there was nothing between this object and the player.
     
  15. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    120

    Ok I checked that the collision is with the player and now it works thank you both.
     
    MelvMay likes this.