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;
}
All of the objects are on the same layer
Answer by PultroN93 · Mar 31, 2021 at 07:20 PM
Try:
if (Physics.Raycast(cameraCenter, camera.forward, out hit, 100f, ~groundMask))
You missing the ~ symbol. Now every layer you set under "groundMask" will be ignored by ray.
Your answer
Follow this Question
Related Questions
I'm Trying to get a 3D Model to follow a Raycast 0 Answers
NPC gets up inside terrain (disconsidering the terrain collider) 1 Answer
I can't get RayCast to report the collider I want. 1 Answer
Hookshot in Unity 2D not working,Hookshot 2D not working 0 Answers
C# Error while trying to set up shot damage and headshots 0 Answers