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

How heavy do I need my rigidbody to be in relation to gravity?

Discussion in 'Physics' started by AxxD, Apr 11, 2016.

  1. AxxD

    AxxD

    Joined:
    Feb 13, 2016
    Posts:
    14
    I want to simulate the weight of a human weighing 100kg. If I've set the rigidbodys mass to 100, what does the gravity need to be to not fall like a feather?
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    less mass will fall faster, but you can add force to manipulate a result.
     
  3. AxxD

    AxxD

    Joined:
    Feb 13, 2016
    Posts:
    14
    I'm trying to make the jumping feel realistic in my game. The problem is that if I make my player able to jump higher, it will take much longer (unrealistically) for it to fall.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Just tick off gravity for that rigidbody and add your own force each frame in fixedupdate.
     
  5. AxxD

    AxxD

    Joined:
    Feb 13, 2016
    Posts:
    14
    Uhh, I feel like I'm already doing that.
    Code (CSharp):
    1. void FixedUpdate () {
    2.         float duck_force = Input.GetAxis("Duck");
    3.         if(isgrounded){
    4.             can_jump = true;
    5.             if (duck_force > 0.2)
    6.             {
    7.                 speed_multipler = 0.5f;
    8.             }
    9.             else
    10.             {
    11.                 speed_multipler = 1f;
    12.             }
    13.         }
    14.         else {
    15.             can_jump = false;
    16.             speed_multipler += air_accelerate;
    17.         }
    18.  
    19.         speed_multipler /= speed_divider;
    20.  
    21.         float forward_momentum = speed_multipler * Input.GetAxis("Vertical");
    22.         float sideways_momentum = Input.GetAxis("Horizontal");
    23.         float jump_height;
    24.         lookX += Input.GetAxis("Mouse X") * 2;
    25.         lookY += Input.GetAxis("Mouse Y") * 2;
    26.         lookY = Mathf.Clamp(lookY, -90, 90);
    27.  
    28.         if (can_jump)
    29.         {
    30.             jump_height = jump_power * Input.GetAxis("Jump");
    31.         }
    32.         else
    33.         {
    34.             jump_height = 0f;
    35.         }
    36.         Vector3 forwardVel = transform.forward * move_speed * forward_momentum;
    37.         Vector3 horizontalVel = transform.right * move_speed * sideways_momentum;
    38.         Vector3 jumpVel = transform.up * (move_speed / 2)  * jump_height;
    39.         Player.GetComponent<Rigidbody>().velocity = jumpVel + forwardVel + horizontalVel;
    40.         PlayerCam.transform.rotation = Quaternion.Euler(-lookY, lookX, 0f);
    41.         Player.GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(0f, lookX, 0f));
    42.     }
    43.  
    Oh and gravity is at -80 on the Y axis.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    Sorry to be perhaps pedantic but I just wanted to mention that body mass has no effect on world gravity. Gravity is applied the same on a mass of 1 as it would to a mass of 100. Forces that are applied are affected by the objects mass however. Falling like a feather is related to drag, not the feathers mass.

    Therefore, increasing global gravity affects all dynamic rigid-bodies in the same way.

    It might be worth applying your own gravity as a standard force to give you fine-grained control. Also, perhaps looking at your own drag function.

    Not sure if it'd help or not but here's a great video that goes into this problem a little: GDC (Bennett Foddy).
     
    JustJunuh and Edy like this.
  7. KristoE

    KristoE

    Joined:
    Oct 13, 2012
    Posts:
    90
    Just to add on to that, if you are looking for a realistic effect, I believe the way Unity physics works is it considers one distance unit to be 1 meter, so if you want realistic results, be sure to check that your character is of appropriate height. Also I believe the mass in unity should represent kilograms, but that shouldn't really matter that much I think. Also, if you are not using the gravitational force that unity provides, be sure to apply yours correctly. The force you should apply to a body for earths gravity should be approximately 9.81(force of gravity on earths surface) * mass of the object.
    Be sure to multiply the force with mass, I've seen people forget it more often than you'd believe. :)

    But when you want lighter objects fall slower, then going with drag should be the best solution. What amount of drag to apply to how heavy objects I'm not really sure of. But if you are not going for some realistic simulator game type, experimenting should get you decent enough results.
     
  8. AxxD

    AxxD

    Joined:
    Feb 13, 2016
    Posts:
    14
    Thanks heaps for your feedback. I'm not really looking for realism, it's just that would be better than what I have at the moment. What I have at the moment which I don't like, is that it suddenly goes up instead of arching, and then the air accelerate kicks in. I want something to feel somewhat like the source engine.
     
  9. KristoE

    KristoE

    Joined:
    Oct 13, 2012
    Posts:
    90
    In your code, it seems that you are setting the upward velocity to a certain value, jumpVel.
    Based on the code, I would think your character would just get high enough that the isGrounded would return false and stay there, unless the isGrounded keeps returning true.

    But what should fix your problem is

    replace
    Code (CSharp):
    1. Player.GetComponent<Rigidbody>().velocity = jumpVel + forwardVel + horizontalVel;
    with
    Code (CSharp):
    1. Vector3 tempVel = Player.GetComponent<Rigidbody>().velocity;
    2. tempVel.x = (forwardVel+horizontalVel).x;
    3. tempVel.z = (forwardVel+horizontalVel).z;
    4. tempVel.y += jumpVel.y;
    5. Player.GetComponent<Rigidbody>().velocity = tempVel;
    Could probably do it cleaner and better, but this should work.

    EDIT: I just read that you have gravity at -80. That is over 8 times the gravity we have here on earth, if your game character is not 16 units tall it will not look realistic.
    Also, the way you are calculating jump velocity right now would result in 0 jump speed if you are standing still. You are multiplying the values of which one is move speed/2, if move speed is 0 the whole equation will be 0.
     
    Last edited: Apr 11, 2016