I am trying to use Unity Terrain with the Unity Physics package.
Currently I am generating a mesh from a given terrain and using that mesh as the custom mesh in the physics shape authoring component. I am using the terrain gameobject for the rendered representation. This works fine, but the work flow is less than ideal (we have to generate a new mesh each time the terrain is altered). I also have some concerns about collision accuracy relative to the visualized terrain in some parts of the terrain.
Is there a more direct way to use Unity Terrains with Unity Physics?
I noticed that there is a TerrainCollider in the Unity.Physics package, but I don’t see any authoring components to use with it. I don’t mind writing my own authoring component if necessary.
You can easily create a terrain collider from a terrain. Just write your own authoring component or a system that creates it. At least i haven’t found an issue yet
@daschatten , great work here. Question: has anyone modified this to create colliders for trees also? @Joachim_Ante_1 , is there any word regarding the dedicate DOTS terrain system?
Thank you! Works well, but collisions with other entities fail to happen.
Is that because the collider needs to be convex? And how would you go about doing that in ECS?
Edit: I was just being an idiot with the “collisions fail to happen” part. Was using the wrong collision masks. Still i would like to hear how one could turn this into a convex collider, as I’m in the process of learning DOTS!
Thanks alot for the code!
The only thing I bumped into was Objects don’t collide when then terrain is exactly flat in a region, and they fall through it. (I am using Unity 2020.1.1f1, havok physics 0.3.1-preview, Entities 0.14.0-preview.18, with havok physics enabled).
Just in case i forget and need it later or if someone else has the same issue.
I added a very small random range to each height point in your code:
var randomExtra = UnityEngine.Random.Range(0.00001f, 0.0001f); // NOTE: Added to original code to prevent flat terrain colliders not working
colliderHeights[j + i * size.x] = h + randomExtra;
I saw people strungling in this question in Unity Physics threads because they think the need to use physics shape component and co…
So for poeple who dont want to do any mistake in setup of Unity Physics terrain collision :
Just add the two scripts to your project and setup the terrain like on the screenshot include with the two scripts. (Don’t forget to create a dedicated terrain physics tag to be clean)
matrices = localToWorld.Value ;
elementsBatchCount = 1 ;
// Use, if you don't need pass shader properties
MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock() ;
// afer this parameter, other are optiional.