Search Unity

Question Physics.Raycast or Physics.Linecast return false no matter what

Discussion in 'Scripting' started by Pope_Goaty_Maximus_I, Sep 24, 2022.

  1. Pope_Goaty_Maximus_I

    Pope_Goaty_Maximus_I

    Joined:
    Sep 24, 2022
    Posts:
    2
    I've been having trouble with this for a while now so I decided to just make it do a raycast whenever I pressed E so that I could see whether or not it worked, and no matter how many colliders I point it at it's always false. This is the line I'm using:

    Debug.Log(Physics.Raycast(transform.position, transform.forward, Mathf.Infinity, 2, QueryTriggerInteraction.Ignore));

    from my understanding it should send a raycast from the gameobject which i control from the direction my camera is facing and if i direct it at a collider it should be true, yet it never fails to be false.
    Does anyone think they might be able to help? I've been stuck on this for days, but I'm not that good at unity yet.
     
  2. Chewbacca1234

    Chewbacca1234

    Joined:
    Jul 22, 2021
    Posts:
    25
    Hello,
    have you tried this
    Code (CSharp):
    1. Debug.Log(Physics.Raycast(transform.position, transform.forward));
    to make sure your layer mask is set up the correct way (not ignoring the particular colliders)?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    In all that time did you look at the docs and the code example?

    I only ask this because you're' specifying "2" as the LayerMask. That argument isn't described as a layer, it's a layer mask i.e. bit-mask. 2 in binary is bit 1 on only so you're asking to detect things on Layer 1. The code example even shows you that.

    Add a public LayerMask field. Set it in the inspector and pass it instead.

    Or to be honest, as the docs show, only specify the args that are mandatory i.e. pass original and direction.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    And if this is in any way mysterious to you, STOP now and get familiar with how it works:

    The difference between Layers vs LayerMasks:

    https://forum.unity.com/threads/raycast-layermask-parameter.944194/#post-6161542

    "There are 10 types of people in this world: those who understand binary, and those who don't."

    ALSO: Always use named arguments with
    Physics.Raycast()
    because it contains many poorly-designed overloads:

    https://forum.unity.com/threads/lay...ke-described-in-the-docs.744302/#post-6355392
     
  5. Pope_Goaty_Maximus_I

    Pope_Goaty_Maximus_I

    Joined:
    Sep 24, 2022
    Posts:
    2
    Thank you all for your help, it looks like the problem was a combination of confusing layers for layermasks and not knowing if the layermask I provide will be ignored or everything else will be ignored.
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    You're welcome.

    Good luck with your project!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    You know, I still struggle with this, between "does a mask say the camera WILL show it, or NOT show it" and "does the mask say the raycast WILL hit hit or NOT hit it..."

    I don't have room in my brain to track stuff like that, so I always just immediately check it whenever I put in new code. It's worth the time spent.

    I'll run the code and test it for GOOD and also test it for BAD, to make sure it isn't just working by luck.