Changing script to use AddForce instead of move

I have tried to figure out how to use AddForce instead of what I currently use because everyone I ask says that it is better and also more what i’m going for, but I don’t know how to change it because every time I try I just keep accelerating instead of stopping at the desired speed (i’m also not quite sure how it works). Please help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;

    public float speed = 12f;
    public float sprintSpeed = 9f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    void Update()
    {
        // Check if the player is grounded
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        // Reset the downward velocity when grounded
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        // Jumping
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * Physics.gravity.y);
        }


    }

    void FixedUpdate()
    {
        // Handle horizontal movement with Rigidbody physics
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        // Apply horizontal movement
        if (Input.GetKey(KeyCode.LeftShift))
        {
            rb.MovePosition(rb.position + move * sprintSpeed * Time.fixedDeltaTime);
        }
        else
        {
            rb.MovePosition(rb.position + move * speed * Time.fixedDeltaTime);
        }


        // Apply vertical jump velocity
        if (velocity.y != 0)
        {
            rb.velocity = new Vector3(rb.velocity.x, velocity.y, rb.velocity.z);
            velocity.y = 0;
        }

    }
}

Instead of using AddForce, you can set rb.velocity directly (which seems easier since you’re already doing it). It looks as if your sprintSpeed is the amount you move per second. velocity is in units per second. So in your case just set rb.velocity=transform.forward*sprintSpeed;. The system will automatically use time.deltaTime to move it a fraction of that per frame.

Setting it once will keep you moving at that speed every frame. But rigidbody friction will slow you down. You can set friction to 0 through the physics materials, or you use that line every frame which will “reset” the speed, even for bounces – you will appear to always be grinding away the way you faces, possibly pushing obstacles.

Of course, during a jump you probably don’t want to touch the speed at all. But if you must, rb.velocity=new Vector3(transform.forward.x*sprintSpeed, rb.velocity.y, transform.forward.z*sprintSpeed); will keep the “flat” speed steady, while preserving the jump’s natural up and down.

1 Like

Do you have any other tutorials for a rigidbody player controller because in the comments the guy who made it said it wasn’t really any good. I have seen this guy a dozen times when searching for Rigidbody tutorials but i’m not sure if his scripts are good. (also if anyone has any good tutorials for rigidbody movement please tell me about them)

I’m sure they are PERFECT scripts for his needs.

Are they perfect for you? Perhaps not. That part is on you.

If you need a specifically-engineered script, you will need to specifically engineer it.

https://forum.unity.com/threads/cant-find-a-good-basic-rigidbody-movement-script.1582104/#post-9786129