How can i get my player to slow down when i stop moving?

My code

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

[RequireComponent(typeof(Rigidbody))] public class PlayerController : MonoBehaviour {
    public Rigidbody Player;
    public Camera Camera;
    public Vector3 jump;
    public float jumpForce = 2.0f;

    public bool isGrounded;
    Rigidbody rb;
    void Start(){
        rb = GetComponent<Rigidbody>();
        jump = new Vector3(0.0f, 2.0f, 0.0f);
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    void OnCollisionStay(){
        isGrounded = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("d")) 
        {
            Player.AddRelativeForce(4, 0, 0);
        }

        if (Input.GetKey("a")) 
        {
            Player.AddRelativeForce(-4, 0, 0);
        }

        if (Input.GetKey("w")) 
        {
            Player.AddRelativeForce(0, 0, 4);
        }

        if (Input.GetKey("s")) 
        {
            Player.AddRelativeForce(0, 0, -4);
        }

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded){

             rb.AddForce(jump * jumpForce, ForceMode.Impulse);
             isGrounded = false;
    }  } }

Hello, I hope this helps.

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

    public class PlayerController : MonoBehaviour
    {
        public float speed = 10.0f;
        public Rigidbody rb;
        public Vector2 movement;
    
        // Use this for initialization
        void Start()
        {
            rb = this.GetComponent<Rigidbody>();
        }
    
        // Update is called once per frame
        void Update()
        {
            movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        }
        void FixedUpdate()
        {
            moveCharacter(movement);
        }
        void moveCharacter(Vector2 direction)
        {
            rb.MovePosition((Vector2)transform.position + (direction * speed * Time.deltaTime));
        }
 if (Input.GetKeyDown(KeyCode.Space) && isGrounded){
 
              rb.AddForce(jump * jumpForce, ForceMode.Impulse);
              isGrounded = false;
    }
}

What this is doing instead of add force is it is moving the rigid body instead of the rigid body getting a separate source and having to wait to slow down.

If you have any problems with it leave a coment and I’ll come back to troubleshoot.

Two possible ways.

  1. Increase the Drag parameter for the Player RigidBody in the Inspector to 0.8 or so to add drag.

  2. Simplify your movement control like this.

     // Player movement control
     void FixedUpdate()
     {
         Vector3 currVelocity = playerRb.velocity;
    
         velocity.y = Input.GetAxis("Vertical") * speed * Time.deltaTime;
         velocity.x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         //        transform.Translate(velocity.x, 0, velocity.y);
         playerRb.AddRelativeForce(new Vector3(velocity.x, 0f, velocity.y) * speed);
    
         if(playerRb.velocity.magnitude > 1)
         {
             // Don't modify.
             playerRb.velocity = currVelocity;
         }
     }