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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Collision won't trigger with instantiated objects

Discussion in 'Scripting' started by stuvpic, Apr 3, 2020.

  1. stuvpic

    stuvpic

    Joined:
    Apr 2, 2020
    Posts:
    2
    Hi,
    I'm generating a terrain and instantiating an object on top of it that spins randomly.
    Because i'm spinning randomly I need to be able to find when the objects collide with the floor and the lowest point of my mesh.
    the extent of the renderer.bounds are always 0,0,0.
    the collision function is never called.

    I have been struggling with this for hours now
    Code (CSharp):
    1.  // Start is called before the first frame update
    2.     void Start() {
    3.         StartCoroutine(WaitForManager());
    4.     //GENERATING TERRAIN
    5.         TerrainData TData = new TerrainData();
    6.         float[,] heightmap = new float[32, 32];
    7.         TData.SetHeights(0, 0, heightmap);
    8.         TData.size = new Vector3(256, 1, 256);
    9.         var TerrainCreated = Terrain.CreateTerrainGameObject(TData);
    10.         TerrainCreated.AddComponent<Rigidbody>();
    11.         TerrainCreated.GetComponent<Rigidbody>().useGravity = false;
    12.     //FETCH THE OBJECTS
    13.         ObjList = Resources.LoadAll("Models", typeof(GameObject));
    14.  
    15.         InstantiateObj(ObjList[0]);
    16.        
    17.     }
    18.  
    19.     void InstantiateObj(Object obj)
    20.     {
    21.     //INSTANTIATE AN OBJECT + ALL THE NEEDED COMPONENTS
    22.         pos = new Vector3(0, 1, 0);
    23.         var ObjCreated = (GameObject)Instantiate(ObjList[0], pos, Quaternion.identity);
    24.         ObjCreated.AddComponent<MeshFilter>();
    25.         MeshFilter Mesh = ObjCreated.GetComponent<MeshFilter>();
    26.         ObjCreated.SetActive(true);
    27.         ObjCreated.AddComponent<MeshCollider>();
    28.         ObjCreated.AddComponent<MeshRenderer>();
    29.         ObjCreated.GetComponent<MeshCollider>().sharedMesh = Mesh.mesh;
    30.         ObjCreated.GetComponent<MeshCollider>().convex = true;
    31.         ObjCreated.AddComponent<Rigidbody>();
    32.         ObjCreated.GetComponent<Rigidbody>().useGravity = false;
    33.         ObjCreated.GetComponent<Rigidbody>().isKinematic = true;
    34.         ObjCreated.AddComponent<randomRot>();
    35.     }
    Code (CSharp):
    1.  //SCRIPT APPLIED AS COMPONENT TO THE OBJECT
    2. Renderer rend;
    3. void OnCollisionStay(Collision jeej)
    4.     {
    5.         print("lol");
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         rend = GetComponent<Renderer>();
    11.         print(rend.bounds);
    12.     }
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Don't your terrain also need a collider?
     
  3. stuvpic

    stuvpic

    Joined:
    Apr 2, 2020
    Posts:
    2
    There's a terrain collider and I also tried with a box collider
     
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Have you tried OnCollisionEnter(), and OnCollisionExit()?

    Why by the way does the terrain have a rigidbody?
     
    Last edited: Apr 5, 2020