- Home /
Terrain collider is inaccurate
I have a terrain with a terrain collider and when I walked around on it I noticed that the collisions seemed inaccurate but almost correct, so I looked at the collision geometry with the physics debugger and found this (collision geometry is in green):
As you can see, it's mostly correct but very inaccurate in many places on slopes. Is something wrong or is there a setting to control this? What's weird is that it looks like the collision geometry has faces that can perfectly match the rendered geometry, it just doesn't. This kind of accuracy is definitely unacceptable as characters clip into and float above slopes.
Edit
To be clear, this is definitely not z-fighting in the physics debugger's display as I found this by my character floating and clipping through terrain hills. It's hard to show in a screenshot, but if you zoom in you can see clear differences between the rendered gray faces and the collider's green faces. I've whipped up a very simple project to show this and I'm surprised no one else is seeing this as it's incredibly simple to reproduce. In this project I just have a terrain with hills, a character controller with a simple movement/camera script, and a couple pins in the land that show obvious points of floating or clipping. You can also look at the areas of these pins with the physics debugger and see it is because of the difference between the terrain collider's faces and the rendered faces. I made this from scratch in a few minutes using only built-in Unity assets and I'm using Unity 2019.3.0f3. It's also here.
Answer by lgarczyn · Jan 12, 2020 at 01:40 AM
This looks like z-fighting, which would just be a rendering issue. The height difference looks too small to affect your rigidbodies in a meaningful way. To get more help please provide a picture of a character clipping or floating, as well as the movement script.
I can see it looking like that but it's definitely not z-fighting, if you actually zoom in on the editor you can see that almost all of the artifacts in the screenshot are actual height differences in the faces that cause very noticeable clipping and floating. I added an example project to clearly show it using very simple base assets.
Answer by Bunny83 · Jan 12, 2020 at 02:13 AM
Unity uses PhysX for 3D physics.PhysX has different collider types and Unity's colliders pretty much map one to one to the physX types. The Terrain collider is not a mesh collider but a height field. The height field has some features we can not control with Unity, like the tesselation flags for example. Though like ceandros said I don't see any issue in your image beside the possible z-fighting. So it's not really clear what your question or concrete issue is.
This is why I'm confused as the physics debugger seems to show that the collider's faces are controlled by a height map of the same resolution as the rendered faces, they are just simply off in their height values. I added a project to clearly show what I'm talking about, but my issue is simply that the terrain collider's height values do not match the rendered height values in all locations as shown in the physics debugger. There are no options on the terrain collider component so I'm not sure what to do about it just being wrong in places.
This seems to be caused by the PhysX heightfield being triangulated in the opposite way to the terrain mesh (see screenshot from your example file), so Bunny's probably right that it has to do with the tessellation flag. What I find bizarre is that this seems like it'd be a frequently reported issue, but I went as far back as Unity 2017 LTS and it's also present there, yet this is apparently the only time it has been reported. The bigger the height difference and the larger the distance between heightfield samples the more visible the error is. With only two ways to do it, it seems like it'd be a trivial fix. If there is no way to change the PhysX triangulation, as Bunny says, and no way to change it on the Unity terrain side either (I'm guessing that's the case), would it be worth filing an issue on the issue tracker? Edit: I went ahead and reported it as a bug, hopefully it's not some weird limitation and that gets things moving. Edit 2: This has been fixed in 2019.3.0f5/2020.1.0.a19. At the time I tested a18 was the most recent version so it was still present.
Answer by ICE-jdeacutis · Jun 10, 2021 at 10:30 AM
Technically you may be able to solve this using a custom terrain shader. If the size of the quads can be determined, the triangle vertices and UV's could be flipped. Not exactly trivial though.
Your answer
Follow this Question
Related Questions
Physics Materials on SpeedTree? 0 Answers
Object Flies into Air Upon Collision, or goes through hill depending on isKinematic settings 1 Answer
Raycast to Terrain (Conditional Statements) 1 Answer
Car falls through terrain 2 Answers
A.I. units partially sinking in to the terrain, on just one location 0 Answers