Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do i add terrain collider to my randomly generated terrain?

Discussion in 'Scripting' started by owlthesheep3, Nov 8, 2019.

  1. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    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?
     
  2. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    Have you tried adding a terrain collider?
     
  3. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    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
     
  4. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    Maybe shoot a raycast down from player (or get the terrain somehow) then just add the terrain collider with
    Code (CSharp):
    1. .AddComponent<TerrainCollider>();
    and if you cant use a terraincollider use a mesh collider?

    It depends on how you make your terrain
     
  5. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    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
     
  6. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    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
    Code (CSharp):
    1. .GetComponent<MeshCollider>().convex = true;
    if that suits your code :)

    Looks like a cool project you got going on aswell, I like it :)
     
  7. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Chunk : MonoBehaviour {
    6.     public Vector3Int coord;
    7.  
    8.     [HideInInspector]
    9.     public Mesh mesh;
    10.  
    11.     MeshFilter meshFilter;
    12.     MeshRenderer meshRenderer;
    13.     MeshCollider meshCollider;
    14.     bool generateCollider;
    15.  
    16.     public void DestroyOrDisable () {
    17.         if (Application.isPlaying) {
    18.             mesh.Clear ();
    19.             gameObject.SetActive (false);
    20.         } else {
    21.             DestroyImmediate (gameObject, false);
    22.         }
    23.     }
    24.  
    25.     // Add components/get references in case lost (references can be lost when working in the editor)
    26.     public void SetUp (Material mat, bool generateCollider) {
    27.         this.generateCollider = generateCollider;
    28.  
    29.         meshFilter = GetComponent<MeshFilter> ();
    30.         meshRenderer = GetComponent<MeshRenderer> ();
    31.         meshCollider = GetComponent<MeshCollider> ();
    32.  
    33.         if (meshFilter == null) {
    34.             meshFilter = gameObject.AddComponent<MeshFilter> ();
    35.         }
    36.  
    37.         if (meshRenderer == null) {
    38.             meshRenderer = gameObject.AddComponent<MeshRenderer> ();
    39.         }
    40.  
    41.         if (meshCollider == null && generateCollider) {
    42.             meshCollider = gameObject.AddComponent<MeshCollider> ();
    43. //here's the part i added the thing you gave me -
    44.             meshCollider = gameObject.AddComponent<MeshCollider>().convex = true;
    45.         }
    46.         if (meshCollider != null && !generateCollider) {
    47.             DestroyImmediate (meshCollider);
    48.         }
    49.  
    50.         mesh = meshFilter.sharedMesh;
    51.         if (mesh == null) {
    52.             mesh = new Mesh ();
    53.             mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
    54.             meshFilter.sharedMesh = mesh;
    55.         }
    56.  
    57.         if (generateCollider) {
    58.             if (meshCollider.sharedMesh == null) {
    59.                 meshCollider.sharedMesh = mesh;
    60.             }
    61.             // force update
    62.             meshCollider.enabled = false;
    63.             meshCollider.enabled = true;
    64.         }
    65.  
    66.         meshRenderer.material = mat;
    67.     }
    68. }
     
  8. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    Try changing
    Code (CSharp):
    1.             meshCollider = gameObject.AddComponent<MeshCollider>().convex = true;
    to
    Code (CSharp):
    1. gameObject.AddComponent<MeshCollider>().convex = true;
    I tried this in my project and it adds a new MeshCollider with convex on
     
  9. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    nothing happens, no mesh collider, no colliding... but everything is correct in compiler:mad: should i provide you with my project so you can try and see that will work better?
     
  10. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Wait, terrain doesn't have a collider by default?
    Maybe trying:
    Code (CSharp):
    1. MeshCollider meshCollider = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
    as described in the documentation would work?

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

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    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
     
  12. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    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
     
  13. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    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?
     
  14. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    Sorry, misread what you said :)

    You can see from his/her screenshot he/she has loads of chunks https://imgur.com/a/pOJm98Y
     
  15. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    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.
     
  16. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    It's alright, I will download it and give it a shot. Will come back with any updates :)
     
  17. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    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
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.             gameObject.AddComponent<MeshCollider>().convex = true;
    4.     }
    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)

    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 :p

    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.
    But the request was to fix the collisions :)

    (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 :)

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

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    Can you give me the link for the finished product? try google drive or mediafire...
    ty you so much for the help!
     
  19. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    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
     
  20. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    sended the friend request
     
    Last edited: Apr 17, 2023
  21. unexpectedly

    unexpectedly

    Joined:
    May 16, 2020
    Posts:
    1
    and thats the end of the story folks, the story of how two strangers met on a forum post and became best friends.
     
    Scrimlex and Lunix46 like this.
  22. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    hey you're right, we're friends now!
     
  23. Lunix46

    Lunix46

    Joined:
    Sep 27, 2016
    Posts:
    26
    o_O how did you know this :D?
     
  24. sdf

    sdf

    Joined:
    Sep 6, 2012
    Posts:
    13
    1. meshCollider = gameObject.AddComponent<MeshCollider>(); meshCollider.convex = true;