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:
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.
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.
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.
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.