RayCast not accurate on non-colliders (3D)
Okay, so here's the breakdown.
This wasn't an issue before, and I don't know how long ago it became an issue.
I have a level designed in Blender and imported. It has a Navigation mesh to detect RayCast and to move the character.
I also have different objects, items to pick up, which have colliders on them.
Now, when I click, the Raycast goes out. As soon as I click, I return the transform position of wherever/whatever is clicked. Objects with colliders return the correct position, whereas, without a collider, it is the point, plus some extra?
Nothing else happens at this moment other than returning a value.
For instance, with the collider, the characters left eye returns (-8.8, 1.8, 17.1), but without it is (-8.9, -1.5, 11).
I have another object which is sat on the ground. The coordinates are (-13, 0.5, 13.7) with collider and (-14.8, -1.5, 9.8) without collider.
Even just moving the mouse off the object without turning off the colliders makes the "jump", and clearing the navmesh makes no difference.
The constant in this is the -1.5 value.
I'd post my code, but I don't know how, however, it is rather basic/generic.
The most obvious solution is to add a mesh collider to my level, but my level is complex, with mountains and valleys (although on a 20x20 area). I want to keep the "strain" as low as possible, as it's a mobile game.
I just find it odd why it suddenly started happening, especially when I've seen others not have this issue.
Hey!
I would like to help, but what you are writing really confuses me: raycasts do use physics, and as such they don't work at all if an object doesn't have a collider. Are you talking about Physics.Raycast()
call? Or do you use a different system? Is it possible that sometimes you use raycastHit.point
while other times raycastHit.collider.transform.position
? These are not the same things (the first tells you where the raycast hit, the second tells you the position of the object hit, the center so to say).
Also, Navmesh has nothing to do with physics, and so raycasts.
So I think you will have to add colliders for raycasts, but there are a few options. First of all, mark your terrain static, that will help a lot with the simulation. Then, if you can model your environment with Unity's terrain system, that already has a collider which is relatively efficient. Finally, you can also try some more complex techniques, like turning physics on and off based on areas, but that might not help with performance.
Finally, it would definitely help if you posted some code, the bugs are usually in the details. You can just copy-paste it into a post here, select it, and then click the code button (the one that has 101010 on it) to format it as code.
Answer by OutOfRam · Jul 30, 2018 at 03:15 PM
Hello,
I think you may be confused about how the Unity Physics.Raycast system works. The unity raycast system casts a ray against all colliders in the scene and returns detailed information on what was hit. In that sense there is no way for a raycast to interact with an object that does not have a collider. It is my belief that the reason your values are shifting as described above when you turn off the collider is because the raycast is merely hitting a different collider in your scene. To test this, try printing not only the position of the raycast hit, but also the name of the game object to which the collider is attached.