Search Unity

Question Instantiated Object falls through the ground

Discussion in '2D' started by mpodlesny, Jul 31, 2020.

  1. mpodlesny

    mpodlesny

    Joined:
    Jun 29, 2020
    Posts:
    43
    So here's my dilemma. I have a 2D game where the character goes around destroying creatures. When the creatures die gold then appears in their place.

    However, when I instantiate the gold it falls through the floor and does not collide.

    1. The gold is instantiated from a prefab
    2. When I put the gold object manually on the screen it works fine.
    3. The gold has a rigidbody 2d and a collider that is not a trigger

    After researching it seems it as something to do with the object being a clone. I can not find anything in the unity docs that shows why when I instantiate the gold object its collider is not working.

    Any ideas?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    Physics doesn't know anything about the fact that it's come from a prefab/clone so it simply has to be the instantiated object settings. Try to compare the Rigidbody2D/Collider2D/GameObject properties between the "manual" one and the script-created one.

    You can look at the gizmo to ensure there's a collider active. If there's no gizmo then that could be a sign the GameObject or Collider2D is disabled, the collider is configured wrong or that the Rigidbody2D is set to not be simulated. Also check the Rigidbody2D body-type is correct to whatever it is you want, presumably Dynamic. Also check that the Layers are the same if you've got a special layer and have configured certain layers to only contact other layers.
     
  3. mpodlesny

    mpodlesny

    Joined:
    Jun 29, 2020
    Posts:
    43
    The instantiated gold is just a copy of what I put on the screen manually. There are no differences. Here's the code I use to instantiate the gold:


    Code (CSharp):
    1.             var floatnewX = transform.position.x;   // leave gold at enemy's x position
    2.             var bobsYPos = FindObjectOfType<Bob>().transform.position.y;    // leave gold at bob's y position
    3.             // drop some gold after the enemy has died
    4.             Instantiate(weaponGold, new Vector3(floatnewX, bobsYPos, gameObject.transform.position.z), Quaternion.identity);
    5.             Destroy(gameObject);
    As you can see it's a basic instantiation. Nothing fancy. I set the coordinates for the new gold will appear, then put it on the screen, then destroy the enemy object.

    When I manually put the gold on the screen it works fine. When I put an instance of the gold on the screen it does not.

    I am using Unity version 2019.4.6f1
     
  4. mpodlesny

    mpodlesny

    Joined:
    Jun 29, 2020
    Posts:
    43
    I figured it out. The gravity was set too high and falling right through the collider on the floor. Once I set it to fall slower, it worked fine.
     
    William_Heidemann likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    So that would mean that you're using discrete and it sounds like a very high gravity causing fast velocity. If you're using fast moving objects then you can use continuous. Use it sparingly because continuous collision detection can get expensive.

    Glad you got it working.
     
    mpodlesny likes this.
  6. mpodlesny

    mpodlesny

    Joined:
    Jun 29, 2020
    Posts:
    43
    thank you for the tip and the advice. Much appreciated.