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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Resolved Rigidbody AddForce Slightly Goes To Side Constantly

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

  1. HermesTheBird


    May 2, 2021
    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);
    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 =;
    14.                 hips.velocity =;
    15.             }
    16.         }
    17.     }
    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);
    7.     }

    This is the video :

    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


    Jan 27, 2013
    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:
    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


    May 2, 2021
    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