Paste
Of Code


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.EventSystems;


public class PlayerMovement : MonoBehaviour
{
    [Header("References")]
    [SerializeField] private Rigidbody rb;
    [SerializeField] private Transform orientation;
    [SerializeField] private CinemachineVirtualCamera cinemachineCamera;
    [Space]
    [Header("Movement settings")]
    [SerializeField] private float walkSpeed;
    [SerializeField] private float runSpeed;
    [SerializeField] private float jumpHeight;
    [Space]
    [Header("Ground Settings")]
    [SerializeField] private LayerMask ground;
    [SerializeField] private float groundDrag;
    [SerializeField] private bool isGrounded;
    [SerializeField] private float playerHeight;


   
    float horizontalInput;
    float verticalInput;
    Vector3 moveDirection;


    private void Awake()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;
    }

    private void Update()
    { // Custom Functions ONLY > For Clean Code & Readability Purposes!!\\
        HandleInput();
        SpeedControl();
        IsOnGround();
       


    }

    private void FixedUpdate()
    { // Custom Functions ONLY > For Clean Code & Readability Purposes!!\\
        HandleMovement();


        
    }


    void HandleInput() // Completed \\
    {
        horizontalInput = Input.GetAxisRaw("Horizontal");
        verticalInput = Input.GetAxisRaw("Vertical");
    }

    private void HandleMovement()
    {
        moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;

        rb.AddForce(moveDirection.normalized * walkSpeed * 10f, ForceMode.Force);
    }

    private void HandleJump() // NOT YET \\
    {
        
    }  

    private void HandleGroundCheck() // NOT YET \\
    {

    }

    private void IsOnGround() // Completed \\
    {
        isGrounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, ground);

        if (isGrounded)
        {
            rb.drag = groundDrag;
        }
        else
        {
            rb.drag = 0;
        }
    }

    private void SpeedControl() // Completed \\
    {
        Vector3 flatvel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

        if(flatvel.magnitude > walkSpeed)
        {
            Vector3 limitedVel = flatvel.normalized * walkSpeed;
            rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
        }
    }

    private void HandleCinemachineCamera()
    {
       
    }



}

Toggle: theme, font