- Home /
accuracy problem with ray cast
Hi I'm trying to instantiate an empty game object in the place a direct vertical ray hit the objects in the editor mode, now here is the problem
the blue arrow shows the direction of the ray, the yellow arrow is the surface of the object, the object that is marked by red arrow is the one that is created but the one with green arrow is the place that is should have been.
here is the code :
var hitInfo = Physics.RaycastAll(new Vector3(x,int.MaxValue,z), -Vector3.up);
if (hitInfo.Length > 0)
{
Debug.Log("We hit something");
foreach (var hit in hitInfo)
{
var slp = 180 - Vector3.Angle(hit.normal, -Vector3.up);
if (slp <= 90 + slope)
{
GameObject go = new GameObject("Node_"+x+"_"+hit.point.y+"_"+z);
go.transform.position = new Vector3(x, hit.point.y, z);
go.AddComponent<Node>();
}
}
}
What am I doing wrong? or if the stuff is correct, how can I solve this?
What is the numerical difference between expected and actual placement?
in that example it was 0.3 units, farther testing is even worse
That big sphere in the red arrow is the exact position of hitInfo.point in simple raycast on the same vector ! it jumps to the side of the geometry totally out of the direction I cast the ray
this is unity 5 x64 b18 by the way not sure if it's a bug in that version or what
Answer by Paulius-Liekis · Jan 12, 2015 at 02:45 PM
You use int.MaxValue as starting distance. That could lead to precision issues, if we're talking about small numbers. Try using smaller offset and see if the problem goes away.
Answer by KurtGokhan · Jan 12, 2015 at 02:47 PM
I think you shouldn't use int.MaxValue in this situation. Have a max height defined that suits your needs. If it is too big like int.MaxValue it may cause floating point errors.
var hitInfo = Physics.RaycastAll(new Vector3(x,1000,z), -Vector3.up);
You can increase 1000 to suit your needs. Your game shouldn't take more space than 100000 anyways. That is going to cause floating point errors.
Ok, that change solved the accuracy but what if for example I import a DE$$anonymous$$ from something like Grome, there will be huge mountains like 4000$$anonymous$$ there!
Ah tested more 1,000,000 is pretty big for every terrain I think and it still has nice accuracy, think that is good enough even for AAA games to generate navigation grids proceduraly now :D (the thing I'm working on is a sand box for AI :D)
Thanks for help
Well..if you have really big maps, then you end up doing workarounds. People who do something like space simulators/shooters where they have really big distances, they end up splitting it into multiple spaces and then simulating everything in your "local" space in order to avoid very big numbers.
Yeah I am also actually working on a procedural AI and I encounter problems like this. For example my game space should be at distance 100,000 to 120,000 to origin. Then I simply subtract 100,000 and it becomes from 0 to 20,000. Workarounds like this are necessary.
Your answer
Follow this Question
Related Questions
Accuracy not working properly 1 Answer
How to give your enemy gun inaccuracy 1 Answer
More precise Vector3 2 Answers
How to Implement First Shot Accuracy? 2 Answers
Problem in Shooting Accuracy 0 Answers