Physics based player controller problems

My physics based player controller is slippery awkward and the player cant go faster than its max speed when affected by outside forces like and explosion is there any way to fix these problems?


Code:

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

public class PlayerController : MonoBehaviour
{
	// Movement Variables
	private Vector3 movement;
	public float speed;
	public float groundSpeed = 50f;
	public float airSpeed = 20f;
	public float maxSpeed = 10f;
	public float jumpForce = 5f;
	public float groundDistance = 1.1f;
	public Rigidbody rb;

	// Camera Variables
	public float sensitivity = 200;
	public Transform player;
	public Transform cam;
	private float xRotation = 0f;

	// Runs when the game starts
	private void Start()
	{
		rb = GetComponent<Rigidbody>();
		Cursor.lockState = CursorLockMode.Locked;
	}

	private void Update()
	{
		// Runs Camera Function
		Look();
	}

	// Runs every physics update
	private void FixedUpdate()
	{
		// Runs Movement Function
		Movement();
	}

	// Does Movment Stuff
	private void Movement()
	{
		// AddsForce to move the player using wasd or arrow keys
		float x = Input.GetAxisRaw("Horizontal");
		float z = Input.GetAxisRaw("Vertical");

		rb.AddForce(transform.forward * z * speed);
		rb.AddForce(transform.right * x * speed);

		// Changes player's acceleration in the air
		if (IsGrounded() == false)
		{
			speed = airSpeed;
		}
		if (IsGrounded() == true)
		{
			speed = groundSpeed;
		}

		// Uses rb.velocity to jump instead of addforce
		if (Input.GetButton("Jump") && IsGrounded() == true)
		{
			rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
		}

		// Limits Speed
		if (rb.velocity.magnitude > maxSpeed)
		{
			rb.velocity = rb.velocity.normalized * maxSpeed;
		}
	}

	// Checks If Player Is Grounded
	private bool IsGrounded()
	{
		return Physics.Raycast(transform.position, Vector3.down, groundDistance);
	}

	// Does First Person Camera Stuff
	private void Look()
	{
		float mouseX = Input.GetAxisRaw("Mouse X") * sensitivity * Time.deltaTime;
		float mouseY = Input.GetAxisRaw("Mouse Y") * sensitivity * Time.deltaTime;

		xRotation -= mouseY;
		xRotation = Mathf.Clamp(xRotation, -90f, 90f);

		cam.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
		player.Rotate(Vector3.up * mouseX);
	}
}

Edit: I’m also looking for an effective coded gravity solution that works much like the default unity gravity

Hello @protonhero,

First off, kudos to you for commenting your code! That’s a good practice. Secondly, this bit of code is your problem regarding the explosions:

// Limits Speed
         if (rb.velocity.magnitude > maxSpeed)
         {
             rb.velocity = rb.velocity.normalized * maxSpeed;
         }

Any time you adjust the velocity on a Rigidbody directly it is going to affect the Rigidbody ANY time it has a velocity. The forces on it become superfluous since you are directly controlling the velocity.

A better way to achieve what you want is to limit the movement force when the player is above a certain velocity.

// AddsForce to move the player using wasd or arrow keys
         float x = Input.GetAxisRaw("Horizontal");
         float z = Input.GetAxisRaw("Vertical");
 
//MOVED FROM BELOW
if (rb.velocity.magnitude < maxSpeed)
{
         rb.AddForce(transform.forward * z * speed);
         rb.AddForce(transform.right * x * speed);
}

Notice that I switched the value comparison so that you are checking if you are below maxSpeed before adding the force.

As for the “slippery” movement, I would play around with the different ForceModes. The AddForce method will have some overloads that allow you to specify the ForceMode. Impulse or Acceleration might get you the effect you desire.

Side note: I’m not entirely sure what the poster above me is suggesting, but my cursory reading of their comment raises suspicion. Generally you do NOT want to change the mass of a Rigidbody during runtime unless your game actually calls for a change in mass (i.e. something shrinks). Otherwise the physics can get nutty. I haven’t tested their solution, though, so I am not 100% recommending against it. I am just suspicious of their method. (Sometimes you have to “hack” things to get them to work, but I don’t think you need to in this case.)

Let me know if you have any additional questions or need something explained.

-Jason