Search Unity

Unity Rigidbody For Character Not Functioning as Intended

Discussion in 'Physics' started by aidangig56, Jul 23, 2021.

  1. aidangig56

    aidangig56

    Joined:
    Aug 10, 2012
    Posts:
    105
    Hello,

    I am working on a game in Unity. I am using a Rigidbody and Capsule Collider for my character but I have a few roadblocks hitting me.

    I need to get velocity and use gravity but the only way is with Is Kinematic off but then my jumping doesn't work and gravity have an effect on my player.

    Currently using MovePosition to move my rigidbody character but if I have is Kinematic on the character falls through the floor, if I do not I can move properly but my crosshair and animation sways don't work as I have no velocity. Crosshair movement and many mechanics rely on velocity.

    I guess my question is how can I achieve gravity, and velocity with a rigidbody character with is Kinematic on not affecting falling through floor. If that is not achievable can we do is Kinematic false?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    The reason your character falls through the floor is probably that the ground doesn't have a collider. Physics objects shouldn't normally move through the floor.
     
  3. aidangig56

    aidangig56

    Joined:
    Aug 10, 2012
    Posts:
    105
    Thank you for the response but it does have a capsule collider on it.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    The floor needs a collider, unless you're suggesting your floor has a capsule collider on it.
     
  5. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    If the body is kinematic then you need to manually do physics queries (capsuleCast, sphereCast, etc) in order to detect obstacles (that's how kinematic works). Then, you need to move the body to the desired destination (without penetrating obstacles). This is not an easy task, so if you really know what you are doing then go for it.

    For a non-kinematic body, jumping can be done by just changing its vertical velocity component:
    Code (CSharp):
    1. // assuming Up = y
    2. if( jumpButtonWasPressed )
    3.     rigidbody.velocity = new Vector3(
    4.        rigidbody.velocity.x ,
    5.        jumpSpeed ,
    6.        rigidbody.velocity.z ,
    7.    );
    I'm just setting a value, this is clearly not the only way to do it.

    If for some reason you don't want to use the RB gravity, you can implement your own:
    Code (CSharp):
    1. // assuming Up = y
    2. rigidbody.velocity += Vector3.down * yourGravity * Time.deltaTime
    Position changes are similar, if you want to move a body a certain displacement (based on a velocity value):
    Code (CSharp):
    1. Vector3 displacement = velocity * Time.deltaTime;
    2. rigidbody.MovePosition( rigidbody.position + displacement );
     
    aidangig56 likes this.
  6. aidangig56

    aidangig56

    Joined:
    Aug 10, 2012
    Posts:
    105
    Thank you guys that definitely put me on the right track but I now have an issue with my rigidbody where the player cannot be hit with its hit boxes. I have a separate script that is attached to the colliders for hit detection but it now takes all hits and does not absorb any damage.

    I guess my issue is with compound colliders where if the rigidbody is attached to the parent, the child components with colliders attached dont function properly.