#how do I fix choppy/rough movement in my rigidbody movement script?

1 messages · Page 1 of 1 (latest)

chrome mountain
#

Currently, the movement of my script is really choppy despite that my movement seems correct. putting it in update obviously made the movement speed tie to the framerate, and inside of fixed update it made the movement feel choppy and on low framerates you dont move where you're looking anymore

#
// Clamp the max speed (velocity) of the player on the X and Z axis
    void FixedUpdate()
    {
        Vector3 horizontalVelocity = new Vector3(body.linearVelocity.x, 0, body.linearVelocity.z);

        if (horizontalVelocity.magnitude > maxSpeed)
        {
            if (sprintAction.IsPressed() & moveAction.IsPressed())
            {
                sprintspeed = maxSpeed * SprintSpeed;
                horizontalVelocity = horizontalVelocity.normalized * sprintspeed;
            }
            else
            {
                horizontalVelocity = horizontalVelocity.normalized * maxSpeed;
            }

        }

        body.linearVelocity = new Vector3(horizontalVelocity.x, body.linearVelocity.y, horizontalVelocity.z);
        // how about we move some shit
        Vector2 moveValue = moveAction.ReadValue<Vector2>();
        if (sprintAction.IsPressed())
        {
            body.AddRelativeForce(moveValue.x * speed * sprintspeed, 0, moveValue.y * speed * sprintspeed);
        }
        else
        {
            body.AddRelativeForce(moveValue.x * speed, 0, moveValue.y * speed);
        }
        // Handle Jumping
        if (jumpAction.IsPressed() && grounded)
        {
            body.AddForce(0, jumpHeight, 0);
        }
        // Handle stopping (if enabled)
        if (grounded && moveValue == Vector2.zero && body.linearVelocity.x < StopSpeed && body.linearVelocity.z < StopSpeed && body.linearVelocity.x > -StopSpeed && body.linearVelocity.z > -StopSpeed)
        {
            Vector3 v = body.linearVelocity;
            v.x = 0;
            v.z = 0;
            body.linearVelocity = v;
        }```
#

this is just the fixedupdate section

#

"not moving where you look" issue may be also caused by 4k, I put the game at 4k to make it run at a lower framerate.

glacial salmon
#

Did you enable interpolaton on your Rigidbody?

#

Also is ether of these objects the same object that has the Rigidbody?

       me.rotation = Quaternion.Euler(0, lr, 0);
        head.localRotation = Quaternion.Euler(ud, 0, 0);```
#

If so, that will break your interpolaton. Any direct modification to the Transform will break your rotation