- Home /
Raycast bug?
My raycast method doesn't return true even though it hits something:
if (Physics.Raycast(particles[par].position, Vector3.up, hit, terrainHeight)) {
Debug.Log("True");
}
It is hitting my terrain mesh, the reason I know this is happening is because I have used, Debug.DrawLine with the same direction, origin and height. Why isn't the function returning true?
It might be that your Debug.DrawLine correctly showed a line from the origin to the terrain, but what if the line was upside down - (if the particle was lower than you thought) you would not be able to tell.
The fact that you are using Vector3.up as the direction suggests that your terrain is in the air - is that right?
$$anonymous$$y terrain is over the particles, as the particles are at y coordinate 0, but I would like to place them on the terrain and yes, the mesh has a mesh collider and is not hidden from raycasting with any tags.
Answer by Bunny83 · Oct 22, 2014 at 01:26 PM
Is your terrain upside down? If not, how can you possible hit it from below? The terrain collider is like a mesh collider. You can only hit the surface from "outside". That's actually true for any collider. However colliders like BoxCollider / SphereCollider are closed volumes so you usually aren't inside the collider, but if you are you wouldn't hit the collider either. Raycast and Linecast are "directed". If you reverse your raycast it should work, something like:
if (Physics.Raycast(particles[par].position + Vector3.up*terrainHeight, -Vector3.up, hit, terrainHeight)) {
Debug.Log("True");
}
edit
Sorry had a typo ^^ fixed the code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613165652im_/https://answers.unity.com/themes/thub/images/avi.jpg)