Search Unity

Rigidbody character not falling with gravity

Discussion in 'Physics' started by relic1882, Apr 28, 2015.

  1. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    I'm trying out using a Rigidbody for my game characters rather than Character Controllers because it seems I have easier options for what I'm doing later. Anyway, I'm using rigidBody.AddForce(moveDirection) where the moveDirection comes from the horizontal and vertical inputs to move the character around the screen. The problem is, that when I fall off of an edge, gravity stops working when I let go of the controls. The character falls while moving, but not while standing still. I can't figure it out. Project Gravity is set to -9.81 on the Y axis. Character's Mass is 2. Drag is 0. Angular Drag is 0.05

    The other thing that I don't understand is that I have to shrink him down all the way until my scale under transform is around 0.2 just for him to fall at all even with my issue. The model was made with MakeHuman and Blender and the scale factor on the import is 1.

    Code for the movement is:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WeightLifterDudeMovement : MonoBehaviour {
    5.  
    6.     Rigidbody rigidBody;
    7.     Animator anim;
    8.  
    9.     bool allowMovement;
    10.     public float moveSpeed = 10f;
    11.  
    12.     Vector3 moveDirection;
    13.  
    14.  
    15.     void Start () {
    16.  
    17.         rigidBody = GetComponent<Rigidbody>();
    18.         anim = GetComponent<Animator>();  
    19.    
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.  
    25.         GetInput();
    26.         rigidBody.transform.LookAt(transform.position + moveDirection);
    27.         rigidBody.AddForce(moveDirection);
    28.         //rigidBody.velocity = moveDirection;
    29.  
    30.     }
    31.  
    32.     void GetInput()
    33.     {
    34.         float hMovement = Input.GetAxis("Horizontal");
    35.         float vMovement = Input.GetAxis("Vertical");
    36.  
    37.         moveDirection = new Vector3(hMovement * moveSpeed, 0.0f, vMovement * moveSpeed);
    38.  
    39.         if (moveDirection != Vector3.zero)
    40.         {
    41.             anim.SetBool("isWalking", true);
    42.         }
    43.         else
    44.         {
    45.             anim.SetBool("isWalking", false);
    46.         }
    47.     }
    48.  
    49. }
    And this is the result of things so far.




    Can someone point me in the right direction? Thanks.
     
  2. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    I found the problem with the gravity. I am using Apply Root Motion to my character for movement. I'm really hoping to adapt this feature for the realism. When I shut it off, the character falls and works properly. Is there a way around this so I can still use root motion and have the gravity work correctly?
     
    chzwhz likes this.
  3. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Try playing around with Animate Physics. Check it if it's not, or vice versa.

    And you would want to move rigidbodies in FixedUpdate instead
     
  4. Its_Jeremy_B

    Its_Jeremy_B

    Joined:
    Mar 24, 2016
    Posts:
    4
    Did anyone ever find a fix for this?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Their fix, had they found one 2+ years ago or whatever, may or may not even be relevant to you.
    You should post a thread on the forums, if you are stuck, including your code and a description of what's happening vs. what the desired outcome is.

    Then, people might be able to assist you. :)
     
  6. Xype

    Xype

    Joined:
    Apr 10, 2017
    Posts:
    339
    Well since someone seems to need to get it going, might as well chime in. First guesses considering I've been there, character model just is not set up properly. Capsule collider with kinematic would probably fix I'm right up (not optimal but I'tll get ya going for prototyping). You need a physics object to apply physics to :)
     
  7. mekartikshah

    mekartikshah

    Joined:
    Jan 10, 2017
    Posts:
    89
    I am facing the same issue, any idea on how to solve?
     
  8. shoother

    shoother

    Joined:
    Jul 18, 2016
    Posts:
    4
    its definitely a bug. I resolved it by reapply the 2D box collider on the object and gravity started to work again.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    None of the above is related to 2D physics. What bug are you referring to?
     
  10. PaulKhill

    PaulKhill

    Joined:
    Jul 30, 2020
    Posts:
    15
    So I'm most likely coming after the war. Lots of good solution in here, but my issue came from something else.
    Basically : the mesh attached to my player wasn't touching the ground (but reacting to my controls).
    Eventually, it was because my box collider and rigid body were applied to my player, not to the mesh that was attached to it. So i switched all that to my child-mesh et voilà !
    (if anyone comes across that issue and has tried everything else)