- Home /
Raycasthit results only 0
Hello,
I was looking for this problem in other threads but yould not identify anything similar. What I want to do is the following: On pressing button "p" a plane shall appear on the surface of my terrain. I can choose in advance the x and z value; the y value shall be estimated with a raycast. The code for the raycast is standard:
//---------------------------------------------------------------------
// start and end of an orthographic ray that hits the terrain and tells us the elevation
private static float rayTestTerrain(float xPos, float zPos, RaycastHit rayYPosHit)
{
Vector3 startRayPoint = new Vector3( xPos, 1000f, zPos );
Ray orthoTestRay = new Ray( startRayPoint, Vector3.down );
//Debug.DrawLine( startRayPoint, endRayPoint, Color.red, 10, false );
RaycastHit lala = new RaycastHit();
// actual ray test
Physics.Raycast( orthoTestRay, out lala, Mathf.Infinity, U3D_ModelManager.m_TerrainLayerMask );
return lala.point.y;
}
The result, though, is always 0:
(139.9, 0.0, 171.6) UnityEngine.Debug:Log(Object)
Do you have any hints for me where to trace the error? The surface is a terrain, but I also created a simple plain on top to see if it gets a hit there, but negative...
Cheers!
sorry, a small correction, the plane DOES give a result, but not the terrain...
Looking at what other people posted, why are you using a raycast? Why not just have all your terrains in a list. Loop through that list. Check if your coordinates are within the bounds of the terrain (just some simple math) and then SampleHeight?
If it's working, either accept the answer that answered the question. Answer it yourself. Or close it :)
Answer by dorpeleg · Jul 03, 2013 at 01:01 PM
Instead of using raycast, you can use this:
But remember, this is only for terrains.
Weird, I tryed converting your answer to a comment (next time, you should post a comment and not a new answer) and it made it disappear :S
So if you can post agian as a comment....
uhm, sorry. thank you for your answer I said I need to use several terrains because I have a huge texture that I have to split. So I cannot tell which terrain to get the SampleHeight from (except quite a lot of effort to identify it)...
Why not? If you can explain a bit more.... If you really have to use ray cast, I think you need to do it like so:
if (raycat here)
{
(get your y value)
)
I try to explain more: there are 9 terrains arranged to a square. The user gives the x and y value where to create my object. That could be anywhere in the scene on top of any of the 9 terrains and I try to avoid to make a test to see at which terrain it is as long as there is a solution that safes me the time and the code and actually it should work... :-/
I did the raycast as shown in the code on top only without the if statement, but the resulting y value is always 0
That's what I mean, I think you need the if statement.
You can also raycast just to see which terrain you are above and then use the SampleHeight.
Your answer