Search Unity

Question Rigidbody Player sliding when moved by AddForce

Discussion in 'Physics' started by mosulimo, Jan 18, 2021.

  1. mosulimo

    mosulimo

    Joined:
    Jan 18, 2021
    Posts:
    6
    I'm running into that typical beginner issue of my player "Sliding on Ice" when I move. It feels like he's moving on a really slippery surface and it's not realistic.

    I'm using a Rigidbody and Capsule Collider on my character. He moves with rb.AddForce and this works fine because it gives the sort of realistic movement I want, however it just keeps him sliding. I have clamped the max speed in code so the velocity doesn't continue adding past a certain number. I would like the player to still move a little when you let go of the move input, but not just slide around.

    I don't know if this is an issue in code or something in the physics.

    ------------------------------------------------------------------------------------------------------------
    Code (CSharp):
    1.  
    2.     public float moveSpeed = 15f;
    3.     public float maxSpeed = 20f;
    4.  
    5.     private void Move()
    6.     {
    7.         float horizontal = Input.GetAxisRaw("Horizontal");
    8.         float vertical = Input.GetAxisRaw("Vertical");
    9.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    10.  
    11.         if (direction.magnitude >= 0.001 && rb.velocity.magnitude <= maxSpeed)
    12.         {
    13.             //to get him to move, rotate and face the direction of the 3rd person camera
    14.             float targetAngle = Mathf.Atan2(horizontal, vertical) * Mathf.Rad2Deg + cam.eulerAngles.y;
    15.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVel, turnSmooth);
    16.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    17.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    18.  
    19.             //movement physics
    20.             rb.AddForce(moveSpeed * moveDir);
    21.         }
    22.  
    23.  
    24.     }
    ------------------------------------------------------------------------------------------------------------

    All help appreciated, thanks
     
    lsugzdinis likes this.
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    You set direction equal to a normalised vector, so its magnitude will always be 1, so the following check for direction.magnitude >= 0.001 will always be true.

    You then always add force equal to direction * moveSpeed

    Just an additional thing to consider.... A moving object will continue to move at a constant velocity until an opposing force slows/stops it. That force may be drag, friction or an opposing addForce, but it won't just stop moving when you stop applying the force.
     
    Last edited: Jan 18, 2021
    mosulimo likes this.
  3. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    mosulimo likes this.
  4. mosulimo

    mosulimo

    Joined:
    Jan 18, 2021
    Posts:
    6
    AlTheSlacker likes this.
  5. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197

    turn drag up.
     
    RiverExplorer and mosulimo like this.
  6. ramsey05

    ramsey05

    Joined:
    Jan 18, 2020
    Posts:
    25
    just increase ur rigidbody's drag variable
     
    mosulimo likes this.
  7. MrExillion

    MrExillion

    Joined:
    Mar 24, 2018
    Posts:
    10
    I would suggest moving the center of mass upwards and downwards depending on your action if you want "slippery" like ice, but not slippery like no gravity. And as the others mention you need to use drag and angular drag, you might also want to increase solver iterations and solver velocity iterations if you're using ice with IK/FK-Limbs such that the simulation of limbs during a fall wont look too crazy, if its realism you aim for. having bunkers reactions to ragdolls can be very entertaing too. Any way do as suggested, and move center of mass if you want to be able to fall or not fall depending on motion as well, but ultimately Drag, and Angular Drag is what gets you inertia and slowing over time. For the latter you can also look into friction and physics materials which you can apply to the surface. Those are the key most important things.

    Oh specifically to keep inertia, do not use rotate, but torques, its like not using move, and instead using addforce, but for rotation :)
     
    Meerah29 and mosulimo like this.