- Home /
RayCast Hit Point rounding itself...?
I've written a voxel system that smooths the top of a cubes four vertices rather than just maxing them to their corners. this is based on simplexnoise so sometimes a value is only 0.0001 above the integer or below, etc.
90% is working nicely at this point. I'm running waves of debugs on this code since last night and I've found that my raycast hit.point is only sending numbers to the tenth (1.1, 1.2) for collision math with makes my 15.95 turn into a 16, so it thinks I've hit an empty cube, when I really hit the 15.
here is the debug:
cube: (3.0, 16.0, 2.0) hit: (3.9, 16.0, 2.5) height: 15.95
I could just round everything to the tenth, but.... can hit.point not go anymore specific than this? Is this a setting somewhere?
Thank you.
Answer by fafase · May 02, 2015 at 02:39 PM
Only the debug is rounded, the actual value is not. For instance, use a public variable to receive the hit point and you will see some more accurate values.
public override string ToString ()
{
return UnityString.Format ("({0:F1}, {1:F1}, {2:F1})", new object[]{
this.x,
this.y,
this.z
});
}
This is the ToString used by default in debug for Vector3. It only prints one decimal. If you want to see more accurate values use your own Debug:
Vector3 v = hit.point;
Debug.Log(v.x+", "+v.y+", "+v.z);
this should print out more accurate values.
you're right thank you. converting it to a Vector3 is giving more accurate debugs...
That doesn't solve my problem though. I'm seeing it from slightly over values too now will have to keep looking.
Thank you.
@malkere What problem do you mean? No values are actually rounded, only those displayed in Debug.Log. The values itself have their maximum possible accuracy. $$anonymous$$eep in $$anonymous$$d that floating point values have a limited accuracy. The greater the values get the less significant digits behind the decimal points you have.
So everything you presented in your question is currently based on the wrong display of values in debug outputs. If you want to be sure that even very small numbers are displayed, use
x.ToString("F20")
// or in scientific notation:
x.ToString("E9")
or something like that. With the scientific notation you can be sure to see the absolute maximum precision possible. I choose 8 because with a single-precision floating point value (float) you only have about 6-9 decimal digits.
If you still have problems, it at least isn't related to wrongly displayed values anymore ^^. If a value is somehow "rounded" it's most likely due to floating point precision issues.
Your question only addresses the debug output. So if you need any further help, you should ask a seperate question and include more details about your actual problem. You may leave a comment here with a link to your other question if you want us to have a look.
I found my major problem it was a silly mistake unrelated to the topic. It did sound like I was clai$$anonymous$$g that as an incomplete answer, but that's not what I meant. $$anonymous$$nowing now that the point was being rounded during debug meant what I thought was my problem was actually not so I needed to look else where.
Ultimately it was because my height array has an extra two points on the x and z axis for smoothing purposes and in one of the four functions that make use of that I was calling directly x,z ins$$anonymous$$d of x+1,z+1.
Fafase's help led me to find that rather than wondering what was up with my raycasts which are working fine.
Thank you for the kind follow-up though Bunny83. Born in 83? me too =D