- Home /
Physics.Raycast not working as expected
Hi there,
I am currently trying to use Physics.Raycast however I am not having any luck... I was expecting that when the ray passes the ground for the yellow ray and the debug to show. The rays are being shot from the centre of the sphere, once the rays collide with the ground a second ray should appear.
This is my current code:
for (int i = 0; i < points.Count; i++) { Vector3 direction = (PlanetMesh.current.planetCore - points[i]).normalized; Ray ray = new Ray(PlanetMesh.current.planetCore, -direction); RaycastHit hit; if (Physics.Raycast(ray, out hit, 1000, ground)) { Debug.DrawRay(PlanetMesh.current.planetCore, -direction * 1000, Color.yellow, 0.5f); Debug.Log("Hit the ground"); } Debug.DrawRay(PlanetMesh.current.planetCore, -direction * 1000, Color.green, 0.5f); }
As you can see in the picture the green rays are always firing which is as expected, just unsure why my IF statement is not working... Any guidance or help would be much appreciated. Thanks
Update: so it appears to recognise objects outside of the sphere... Therefore I think this is due the rays shooting from inside the sphere but not sure how to fix that?
Answer by airbagnr1 · Jan 24, 2020 at 10:02 PM
Turning on 'Queries hit backfaces' solved my issue! This can be found in Project settings -> Physics
Your answer
Follow this Question
Related Questions
Raycast returns null for no apparent reason 0 Answers
Need Help with RayCast. No Vector? 1 Answer
Raycast goes through 1 Answer
Shoot off multiple raycasts from 1 object? 2 Answers
Raycasting and foreach loops 1 Answer