Gravity movement + Movement scrips in general

I was searching for a script to copy and learn from when I stumbled upon this script, there are a few changes I want to make regarding gravity. Right now I just fall at the same speed until I hit the ground. I want it to act like “real life” where when you fall your speed increases over time.

(also if anyone has a good working movement script please tell me i’ve been searching for hours now.)

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

public class PlayerMovement : MonoBehaviour
{
    float playerHeight = 2f;

    [SerializeField] Transform orientation;

    [Header("Movement")]
    [SerializeField] float moveSpeed = 6f;
    [SerializeField] float airMultiplier = 0.4f;
    float movementMultiplier = 10f;

    [Header("Sprinting")]
    [SerializeField] float walkSpeed = 4f;
    [SerializeField] float sprintSpeed = 6f;
    [SerializeField] float acceleration = 10f;

    [Header("Jumping")]
    public float jumpForce = 5f;

    [Header("Keybinds")]
    [SerializeField] KeyCode jumpKey = KeyCode.Space;
    [SerializeField] KeyCode sprintKey = KeyCode.LeftShift;

    [Header("Drag")]
    [SerializeField] float groundDrag = 6f;
    [SerializeField] float airDrag = 2f;

    float horizontalMovement;
    float verticalMovement;

    [Header("Ground Detection")]
    [SerializeField] Transform groundCheck;
    [SerializeField] LayerMask groundMask;
    [SerializeField] float groundDistance = 0.2f;
    public bool isGrounded { get; private set; }

    Vector3 moveDirection;
    Vector3 slopeMoveDirection;

    Rigidbody rb;

    RaycastHit slopeHit;

    private bool OnSlope()
    {
        if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
        {
            if (slopeHit.normal != Vector3.up)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
    }

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;
    }

    private void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        MyInput();
        ControlDrag();
        ControlSpeed();

        if (Input.GetKeyDown(jumpKey) && isGrounded)
        {
            Jump();
        }

        slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
    }

    void MyInput()
    {
        horizontalMovement = Input.GetAxisRaw("Horizontal");
        verticalMovement = Input.GetAxisRaw("Vertical");

        moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
    }

    void Jump()
    {
        if (isGrounded)
        {
            rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
            rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
        }
    }

    void ControlSpeed()
    {
        if (Input.GetKey(sprintKey) && isGrounded)
        {
            moveSpeed = Mathf.Lerp(moveSpeed, sprintSpeed, acceleration * Time.deltaTime);
        }
        else
        {
            moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
        }
    }

    void ControlDrag()
    {
        if (isGrounded)
        {
            rb.drag = groundDrag;
        }
        else
        {
            rb.drag = airDrag;
        }
    }

    private void FixedUpdate()
    {
        MovePlayer();
    }

    void MovePlayer()
    {
        if (isGrounded && !OnSlope())
        {
            rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
        }
        else if (isGrounded && OnSlope())
        {
            rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
        }
        else if (!isGrounded)
        {
            rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
        }
    }
}

Search “Unity character controller” on YouTube or Google etc
A lot of people posted courses about it
It would also be a good starting point to refer to Asset Store’s asset

These are contents that have nothing to do with me personally
It’s just an example

and I recommend this asset.

To give you my personal tip, there are cases where you don’t necessarily have to buy an asset for analysis of an asset.

Read the documentation provided in the asset store.

Through it, it is possible to grasp the approximate design structure.
If you see it and like it or want to study it further, buy it.

As for the drop speed,
It doesn’t look like it’s written in the document,
I remember it was made of how much gravity would be affected, how much would it accelerate
For more information, contact asset developer

You could disable useGravity on your rigidbody, and implement it yourself in the player.

private void Update()
{
    var checkSphere = Physics.CheckSphere (groundCheck.position, groundDistance,  groundMask);
     if (isGrounded && !checkSphere) {
         //The player was grounded last frame (isGrounded)
         //The player is no longer grounded this frame (checkSphere). It just became ungrounded
         justBecameUngrounded = true;
         timeBecameUngrounded = Time.time;     
      }
     isGrounded = checkSphere;
//...
}

  private void FixedUpdate()
    {
        MovePlayer();
        ApplyGravity ();
    }

void ApplyGravity()
{
      Vector3 newRBVelocity = rb.velocity;

      if (isGrounded) {
            newRBVelocity.y = Physics.gravity.y;//Apply constant gravity
        } else {
            float newVelocityY;
            if (justBecameUngrounded) {
                newVelocityY = 0.0f;
                justBecameUngrounded = false; //Reset flag            
            } else {           
                const float TimeToReachTerminalVelocity = 5.0f;         
                newVelocityY = Mathf.Lerp (0, Physics.gravity.y, (Time.time - timeBecameUngrounded) / TimeToReachTerminalVelocity); //Velocity starts at 0, ends at terminal velocity
                newVelocityY = Mathf.Clamp(newVelocityY, Physics.gravity.y, 0.0f); //Dont fall faster than terminal velocity
            }
            newRBVelocity.y = newVelocityY;
        }

        rb.velocity = newRBVelocity;
    }

Your air drag is too high and is causing your character to fall very slowly. Try leaving drag at zero and adjust your move force according to the state of your character. But if you still feel you need more ground friction then you can add a physics material and adjust the friction.

And you don’t need a different code path for slopes. All ground can be considered a slope to some degree.

Here’s a very basic controller:

using UnityEngine;
public class SimpleRigidbodyController : MonoBehaviour // add this to a default capsule
{
    Rigidbody rb;

    void Start()
    {
        rb=GetComponent<Rigidbody>();
        rb.freezeRotation=true;
    }

    void FixedUpdate()
    {
        Vector3 force=((transform.right*Input.GetAxisRaw("Horizontal"))+(transform.forward*Input.GetAxisRaw("Vertical"))).normalized;
        if (Physics.SphereCast(transform.position,0.5f,-transform.up,out RaycastHit hit,0.55f))  // are we grounded?
        {
            force=Vector3.ProjectOnPlane(force,hit.normal)*6; // align input force to the ground and increase the force
            if (Input.GetKey(KeyCode.LeftShift)) force*=2;  // sprinter
            if (Input.GetButton("Jump")) rb.AddForce(Vector3.up*6,ForceMode.Impulse);      // jumper
        }
        rb.AddForce(force*5);  // air movement force of 5
    }
}

Thanks, I tried using your simple Character controller but upon trying it I build up speed extremely fast. How do I add a maximum speed value to the sprinting and walking? Or is there a better way to do that?

You shouldn’t need to set a maximum speed value. Simply reduce the amount of force you add to the rigidbody or increase the friction.

This changes the grounded force amount from 6 to 3:

force=Vector3.ProjectOnPlane(force,hit.normal)*3;

Or you can limit the velocity indirectly by limiting the force like this:

using UnityEngine;
public class SimpleRigidbodyController : MonoBehaviour
{
    Rigidbody rb;

    void Start()
    {
        rb=GetComponent<Rigidbody>();
        rb.freezeRotation=true;
    }

    void FixedUpdate()
    {
        Vector3 force=((transform.right*Input.GetAxisRaw("Horizontal"))+(transform.forward*Input.GetAxisRaw("Vertical"))).normalized;
        if (Physics.SphereCast(transform.position,0.5f,-transform.up,out RaycastHit hit,0.55f))  // are we grounded?
        {
            force=Vector3.ProjectOnPlane(force,hit.normal)*5; // align input force to the ground and increase the force
            force-=rb.velocity*0.2f;   // adjust the input force according to our current velocity. This will prevent us from moving too fast and also resists sliding around
            if (Input.GetKey(KeyCode.LeftShift)) force*=2;  // sprinter
            if (Input.GetButton("Jump")) rb.AddForce(Vector3.up*6,ForceMode.Impulse);      // jumper
        }
        rb.AddForce(force*5);  // air movement force of 5
    }
}

I always start from this one:

https://discussions.unity.com/t/855344

That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!