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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rigidbody Instantiate not working, strange behavior

Discussion in 'Scripting' started by VirusPunk, Nov 11, 2015.

  1. VirusPunk

    VirusPunk

    Joined:
    Aug 21, 2015
    Posts:
    25
    Unless I have constraints turned on for X, Y, and Z my Rigidbody object for some reason slowly moves to the right side on it's own while rotating.

    Also my code to Instantiate the Rigidbody does nothing at all.

    Code (CSharp):
    1.     public void GunsmokeStartTimer()
    2.     {
    3.         gunsmoke.SetActive(true);
    4.         gunsmoke.GetComponent<ParticleSystem>().Play();
    5.         Rigidbody clone = (Rigidbody)Instantiate(shell, transform.position,
    6.             transform.rotation) as Rigidbody;
    7.         clone.velocity = transform.TransformDirection(-Vector3.forward * 70);
    8.     }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Weird motions in rigidbodies usually arise from overlapping colliders. The physics system doesn't like that, it tries to move things apart so they don't overlap, and this can result in unexpected motions or rotations.

    As for your Instantiate code not doing anything at all, I don't believe it.
     
  3. VirusPunk

    VirusPunk

    Joined:
    Aug 21, 2015
    Posts:
    25
    I recently upgraded to v5.22 from v3 (sad, I know) so I was used to having a mesh collider as well as the Rigidbody attached. I removed the mesh collider, that solved the weird floating away bug. By nothing at all I meant that it doesn't work properly, it shows a clone of the object in question during run-time, but I see it nowhere in game, as if it has the mesh renderer unticked (but it isn't).
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    So, yeah, if you remove the mesh collider then you can't have overlapping colliders, so that'll solve that.

    Of course a Rigidbody without a collider of some sort isn't a rigid body at all, is it?

    As for the mystery of the missing clone: have you tried double-clicking the clone in the Hierarchy view, and looking in the Scene view to see where it is? Perhaps the physics engine has shot it off into the ether, because it was overlapping with something else. (Be sure to have the game paused when you do this, as if it's moving at high velocity it could be very hard to pin down otherwise.)
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can use rigidbodys without colliders. I built a wave physics system around the idea. But I'll admit it is a relatively edge use case.

    I'm also picking that the instantiation is occurring off camera. Double clicking or pressing F should find it. Or you can just force its transform values to zeros.
     
    JoeStrout likes this.
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Looks like it is instantiating at whatever the prefabs coordinates are rather than at a specified spot.
     
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1.  Rigidbody clone = (Rigidbody)Instantiate(shell, ransform.position, transform.rotation) as Rigidbody;
    should be ->
    Code (CSharp):
    1.  Rigidbody clone = (Rigidbody)Instantiate(shell, ransform.position, transform.rotation);
    or

    Code (CSharp):
    1.  Rigidbody clone = Instantiate(shell, ransform.position, transform.rotation) as Rigidbody;
    also what kind of Object is your "shell"

    depending on your game 2D / 3D

    Code (CSharp):
    1.  clone.velocity = transform.TransformDirection(-Vector3.forward * 70);
    moves the object in Z direction