Search Unity

Resolved Rigidbody AddForce Slightly Goes To Side Constantly

Discussion in 'Physics' started by HermesTheBird, Apr 20, 2023.

  1. HermesTheBird

    HermesTheBird

    Joined:
    May 2, 2021
    Posts:
    7
    I am making a ragdoll character, i used configurable joints for the body parts. the main part of the body is hips.


    Here is the structure of character :
    Thu Apr 20 14:02:18 +03 2023.png
    Hips configurable joint :
    Thu Apr 20 14:07:37 +03 2023.png

    And this is the script i wrote, basically it checks if the player can move and if it's velocity is less than its limit and adds force :
    Code (CSharp):
    1.     void MovePlayer(){
    2.         if(hips.velocity.magnitude < currentSpeedLimit && canMove){
    3.             movementVector = playerInput._horizontal * -cam.right + playerInput._vertical * -cam.forward;
    4.             print(movementVector);
    5.  
    6.             if(movementVector.magnitude > 0){
    7.                 RotatePlayer();
    8.                 hips.AddForce(-movementVector * moveForce * forceMultiplier * Time.deltaTime,
    9.                         ForceMode.VelocityChange);
    10.             }
    11.             else
    12.             {
    13.                 hips.angularVelocity = Vector3.zero;
    14.                 hips.velocity = Vector3.zero;
    15.             }
    16.         }
    17.     }
    18.  
    This is the RotatePlayer method if you think it will be relevant

    Code (CSharp):
    1.  void RotatePlayer()
    2.     {
    3.         float targetAngle = Mathf.Atan2(movementVector.x, movementVector.z) * Mathf.Rad2Deg;
    4.         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVel, turnSmoothTime);
    5.         GetComponent<ConfigurableJoint>().targetRotation = Quaternion.Euler(0,- angle,0);
    6.  
    7.     }
    8.  


    This is the video : https://www.veed.io/view/7ad1e697-492c-40e9-a068-9e99cff0d988?sharingWidget=true&panel=share

    At the start i just press w to go forward because i looked diagonal, character doesnt go straight even when i look straight forward.
    Later i try to go diagonal by pressing w and a keys at the same time you can see that from the character's face but this character just goes straight. I think character is incluenced under old forces and i couldn't figure out how to eleminate this behiavour.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    In the video above, release movement controls once you turn the camera and your character starts sliding. See what happens. Then push forward, then see what happens.

    The problem is that you never alter existing velocity, just keep adding. Velocity is only reset when your control movement vector is zero.

    However, in this scenario if you push forward, then without releasing controls turn, this happens:
    upload_2023-4-20_15-55-28.png
    You keep adding more and more velocity, and velocity is a vector. Because results keep accumulating, the character keeps moving in the old direction until you cancel it. It is like flying in space under newtonian mechanics.

    To alter the behavior, try this:
    * Read current velocity of character. (where the character is going)
    * Read desired velocity (where the character should be going)
    * Alter velocity using acceleration (limited by character stats), until the character is going in desired direction at desired speed.

    Also note that currently pressing forward will push your character into ground, because camera is looking down. You might want to project movement vectors onto ground plane, then normalize them.

    Also, in your calculation you negate movement vector twice, which is not necessary.
     
  3. HermesTheBird

    HermesTheBird

    Joined:
    May 2, 2021
    Posts:
    7
    Thanks your suggestions fixed the issue!! By changing the code from adding force to changing velocity of the character. Also I used Vector3.ProjectOnPlane to fix the movementVector. There is still some sliding very small but it is about the mass and friction of the character and it can be ignored