Search Unity

Object falling over when adding rigidbody

Discussion in 'Editor & General Support' started by Danneman, Mar 19, 2010.

  1. Danneman

    Danneman

    Joined:
    Sep 29, 2009
    Posts:
    190
    Im making a simple grouped object consisting of a cylinder and a sphere. The sphere is located on top, meaning its like a stickman of sorts.

    I want this stickman to be able to move, so I put in some code to do that. This works fine as long as the object doesnt have a rigidbody, but then it wont adhere to the laws of physics - which is a prerequisite. The problem with adding a rigidbody is that the stickman soon falls over since its not perfectly balanced.

    So, is there a way that I can have an object that isnt perfectly balanced with a rigidbody while not falling over?
     
  2. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    You can set the rigidbody to Kinematic by ticking the box named "Is Kinematic". In this mode the rigidbody will not be effected by force, gravity or any other form of physical interaction. It can be animated and transformed in this state too.

    Then, whenever you want to activate the physics on the object, just use this line in the script.

    Code (csharp):
    1. rigidbody.isKinematic = true;
     
  3. Danneman

    Danneman

    Joined:
    Sep 29, 2009
    Posts:
    190
    Thanks for the answer, Mortiis. However, what I really want to accomplish is that it has gravity but doesnt fall over. This is because the object is supposed to be an enemy moving over various surfaces with different height. If the object lacks gravity it wont move correctly.

    I was thinking I could prevent it from rotating in x- and z-direction inside the Update-function using:

    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.Euler( 0, transform.eulerAngles.y, 0 );
    3.  
    The problem is that it takes up a lot of overhead, since the function is constantly rotating back the falling object, making it move in a much less smooth fashion.
     
  4. Peter G

    Peter G

    Joined:
    Nov 21, 2009
    Posts:
    610
    You probably want to use a character controller for that and add your own gravity.
     
  5. svenskefan

    svenskefan

    Joined:
    Nov 26, 2008
    Posts:
    282
    try manipulating the center of gravity of the combined object.
    rigidbody.centerOfMass = new Vector3(0.0f, -0.3f,0.0f);

    or something similar..
     
  6. Danneman

    Danneman

    Joined:
    Sep 29, 2009
    Posts:
    190
    Peter G: Ive tried adding a character controller, which then replaces the collider of the object. This results in the object not colliding with anything, including the plane that I use for ground. I should read up on character controllers first though..

    svenskefan: Tjena! This works ok, but does not make the movement any smoother. The object still stutters forward, as opposed to when I for instance use "Is Kinematic" in which case it moves smoothly again.
     
  7. tscpp

    tscpp

    Joined:
    Sep 11, 2018
    Posts:
    1
    select mass and write 1, even if it is already is 1 and try again.
    Hope it works :D
     
  8. hzaabll

    hzaabll

    Joined:
    Jul 7, 2020
    Posts:
    1
    add a box collider to the surface beneth, works 100%
     
    SmithySFC and rodneya63 like this.
  9. SmithySFC

    SmithySFC

    Joined:
    Jan 15, 2021
    Posts:
    11
    Thanks thats a great solution, I added a small box collider to base of object - it now stands up correctly and uses gravity/physics properly.