How do i add terrain collider to my randomly generated terrain? (764852)

Yes, i did this thread long ago but i still cant do it right, every time i try it it messes everything up. So i need a terrain collider or mesh collider to automatically apply to my randomly generated terrain via script so my player doesn’t fall always through the terrain. Can someone help me with the code?

Have you tried adding a terrain collider?

i cant add it to the terrain because always when the game is paused or not playing the terrain is not in hiearchy, also if i would add it it wouldn’t apply to the curves and player would go through the curves and hills

Maybe shoot a raycast down from player (or get the terrain somehow) then just add the terrain collider with .AddComponent<TerrainCollider>(); and if you cant use a terraincollider use a mesh collider?

It depends on how you make your terrain

terrain collider didnt work so i tried mesh collider. It does nothing with stopping my player from falling. only when i turn Convex then it creates one but its not the shape as the terrain. here’s the picture - https://imgur.com/a/pOJm98Y

Terrain collider did not work because it was not a terrain but if you want to roam with mesh collider you could do something like this .GetComponent<MeshCollider>().convex = true; if that suits your code :slight_smile:

Looks like a cool project you got going on aswell, I like it :slight_smile:

nah, it just says
Error CS0029 Cannot implicitly convert type ‘bool’ to ‘UnityEngine.MeshCollider’

here’s my code so you can take closer look of it and see what will suit it

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Chunk : MonoBehaviour {
    public Vector3Int coord;

    [HideInInspector]
    public Mesh mesh;

    MeshFilter meshFilter;
    MeshRenderer meshRenderer;
    MeshCollider meshCollider;
    bool generateCollider;

    public void DestroyOrDisable () {
        if (Application.isPlaying) {
            mesh.Clear ();
            gameObject.SetActive (false);
        } else {
            DestroyImmediate (gameObject, false);
        }
    }

    // Add components/get references in case lost (references can be lost when working in the editor)
    public void SetUp (Material mat, bool generateCollider) {
        this.generateCollider = generateCollider;

        meshFilter = GetComponent<MeshFilter> ();
        meshRenderer = GetComponent<MeshRenderer> ();
        meshCollider = GetComponent<MeshCollider> ();

        if (meshFilter == null) {
            meshFilter = gameObject.AddComponent<MeshFilter> ();
        }

        if (meshRenderer == null) {
            meshRenderer = gameObject.AddComponent<MeshRenderer> ();
        }

        if (meshCollider == null && generateCollider) {
            meshCollider = gameObject.AddComponent<MeshCollider> ();
//here's the part i added the thing you gave me -
            meshCollider = gameObject.AddComponent<MeshCollider>().convex = true;
        }
        if (meshCollider != null && !generateCollider) {
            DestroyImmediate (meshCollider);
        }

        mesh = meshFilter.sharedMesh;
        if (mesh == null) {
            mesh = new Mesh ();
            mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
            meshFilter.sharedMesh = mesh;
        }

        if (generateCollider) {
            if (meshCollider.sharedMesh == null) {
                meshCollider.sharedMesh = mesh;
            }
            // force update
            meshCollider.enabled = false;
            meshCollider.enabled = true;
        }

        meshRenderer.material = mat;
    }
}

Try changing meshCollider = gameObject.AddComponent<MeshCollider>().convex = true;
to

gameObject.AddComponent<MeshCollider>().convex = true;

I tried this in my project and it adds a new MeshCollider with convex on

nothing happens, no mesh collider, no colliding… but everything is correct in compiler:rage: should i provide you with my project so you can try and see that will work better?

Wait, terrain doesn’t have a collider by default?

Maybe trying:

MeshCollider meshCollider = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;

as described in the documentation would work?

https://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html

I tried it too but it just spawns a lot of mesh colliders on one chunk and turns the convex on… but it does not get scaled to normal terrain

The problem is that he is not using terrain but gameobject as ‘chunks’ and I don’t think terrain collider will work unless you have a ‘terrain’ added.

Weird… I mean, you could but it should work. It’s up to you

The example I provided adds mesh colliders not terrain colliders.

If you generate terrain as a lots of game objects, why not using prefabs that contain colliders and spawn them procedurally?

Sorry, misread what you said :slight_smile:

You can see from his/her screenshot he/she has loads of chunks Problem with convex, if i turn off convex the green line dissapears and there is no colliding with player (so my player in game keeps falling through the terrain) - Album on Imgur

Here’s the link for the project - https://drive.google.com/drive/folders/1fs_SI_gRGVqLBfl2Hs33D7qpoQsjUXRD?usp=sharing

So i got the Terrain scene to generate colliders so I’ve put the toggle for generate colliders

You now need to go to the Submarine scene and try helping me out with the colliders on that scene, so my submarine doesn’t go through the water. Thanks for the help in advance! I was kinda busy so i couldn’t answer as fast.

It’s alright, I will download it and give it a shot. Will come back with any updates :slight_smile:

So I downloaded it and played around a little and got it to kinda work but there is some issues you would need to take care of involving your meshes.

So for easy sake, I just added

    private void Start()
    {
            gameObject.AddComponent<MeshCollider>().convex = true;
    }

to your ‘chunk’ script which works without problems. It creates a mesh collider with convex on. This is done on all chunks as you most likely want that.

After that, for a gameobject to detect a collision you need either a ‘Rigidbody’ or you could shoot raycasts and what not but for simple sake, I tried with a rigidbody that seems to work alright. This is the settings I used on it (Its added to the submarine itself) Setting for rigidbody

If you were to not freeze the rotation, when you hit the terrain the submarine would spin around like crazy which would make you ill :stuck_out_tongue:

But the problem is that you cant go through certain holes in the terrain due to how meshes are generated. I’m not sure how you would fix it maybe someone who knows a bit more could help you.As seen here
But the request was to fix the collisions :slight_smile:

(Note I downloaded same version of unity as you had so it all would work)

If a rigibody won’t work for you I found this that could maybe help you but it won’t solve your mesh problem. Looks like a super fun project doe and hope you do well with it :slight_smile:

http://www.theappguruz.com/blog/collision-detection-without-rigid-body-in-unity

Can you give me the link for the finished product? try google drive or mediafire…
ty you so much for the help!

Sadly my upload speed is horrible so its probably just faster for you to write the 2 lines yourself heh… You could add me on discord if you still don’t understand Lunix46#4520

sended the friend request