- Home /
Raycast hit ignores everything except for the terrain
Hello! I just started learning unity and I'm trying to detect when the player looks at something using raycast hit. However, the issue I'm facing is that every object basically gets ignored by it except for one plane that the character's standing on. Here are some code snippets:
if (Physics.Raycast(cameraCenter, camera.forward, out hit, 100f, groundMask)) { lookObject = hit.transform.gameObject; print(lookObject); } else { lookObject = null; }
if (Physics.Raycast(cameraCenter, camera.forward, out hit, 100f))
Perhaps remove "groundMask" part.
Answer by unity_zgZIxccP2cRekQ · Apr 03, 2021 at 04:53 AM
Hey there, the reason you are experiencing this is because at the very end of you sending the raycast, you have put that the only thing that effects your raycast is anything with the layer of ground which you assign in the Unity editor. If you would like your raycast to be effected by everything, then you can just take off the last part of the raycast line, or the part where it says "groundMask". Then, everything with a collider will now effect the raycast. I hope this helped out.
Your answer
Follow this Question
Related Questions
Checking detection of gameobject correction 2 Answers
How to Check for Enemie in Front? Raycast is not Enough! 2 Answers
Decals on corners 1 Answer