Player Control Unity

 avatar
unknown
csharp
a year ago
8.3 kB
5
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Player_Control : MonoBehaviour
{
    private CharacterController cc;
    private PlayerInput playerInput;
    private Controls controls;

    private Camera mainCamera;

    [Header("Mouse")]
    [SerializeField] private float mouseSensitivity = 120f;
    private float mouseX, mouseY;
    private float xRotation;

    [Header("Abilities")]
    [SerializeField] private float walkingSpeed = 3.2f;
    [SerializeField] private float runningSpeed = 4.6f;
    [SerializeField] private float crouchSpeed = 2.4f;
    [Space]
    [SerializeField] private float jumpForce = 2f;

    private Vector2 inputDirection;
    private Vector3 moveDirection;
    private Vector3 velocity;

    RaycastHit hit;

    //Licenses 
    private bool canMove;
    private bool canRun;
    private bool canCrouch;
    private bool canJump;

    //bool states
    private bool isCrouching = false;
    private bool isRunning = false;
    private bool isJumping = false;

    //Crouching
    private float orginalHeight;
    private float wantedHeight;
    private float crouchLerp;

    //Moving Speed 
    private float currentSpeed;

    //Jumping
    private float coolDownJumping;
    private float onGroundTimer;

    private void Awake()
    {
        playerInput = GetComponent<PlayerInput>();
        controls = new Controls();
        controls.Player.Enable();

        SetupCamera();
    }

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;

        SetupPlayer();
        SetupLicenses(true, true, true, true);

        orginalHeight = cc.height;
        wantedHeight = orginalHeight / 2;
        crouchLerp = 0;
    }

    void SetupPlayer()
    {
        cc = gameObject.AddComponent<CharacterController>();

        cc.center = new Vector3(0, 1, 0);
        cc.height = 2;
        cc.radius = 0.4f;
        cc.stepOffset = 0.3f;
    }

    void SetupCamera()
    {
        if (mainCamera == null)
        {
            mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
        }

        mainCamera.transform.position = transform.position + new Vector3(0, 1.5f, 0);
        mainCamera.transform.parent = transform;

        xRotation = 0;
    }

    public void SetupLicenses(bool canWeMove, bool canWeRun, bool canWeCrouch, bool canWeJump)
    {
        canMove = canWeMove;
        canRun = canWeRun;
        canCrouch = canWeCrouch;
        canJump = canWeJump;
    }

    private void Update()
    {
        if (isGrounded) AdjustSpeed();

        RotateCamera();
        RotatePlayer();
        Other();

        Crouch();
        Move();
    }

    void Other()
    {
        //stepoffset
        if (!isGrounded && cc.stepOffset != 0)
        {
            cc.stepOffset = 0;
        }

        else if (isGrounded && cc.stepOffset != 0.3f)
        {
            cc.stepOffset = 0.3f;
        }

        //Reset jumping once hit ground.
        if (isJumping && coolDownJumping <= 0 && isGrounded)
        {
            isJumping = false;
        }

        //Eliminates insta fast falling
        if (isGrounded && onGroundTimer < 0.2f)
        {
            onGroundTimer += Time.deltaTime;
        }

        if (coolDownJumping > 0)
        {
            coolDownJumping -= Time.deltaTime;
        }

        
        if (!isGrounded && onGroundTimer >= 0.2f && !isJumping)
        {
            velocity.y = 0;
            onGroundTimer = 0;
        }
    }

    void OnLook(InputValue value)
    {
        mouseX = value.Get<Vector2>().x * mouseSensitivity * Time.deltaTime;
        mouseY = value.Get<Vector2>().y * mouseSensitivity * Time.deltaTime;
    }

    void OnMove(InputValue value)
    {
        inputDirection = value.Get<Vector2>();
    }

    void OnCrouch(InputValue value)
    {
        if(value.isPressed && canCrouch)
        {
            crouchLerp = 0;

            if (!cantStand && isCrouching)
            {
                isCrouching = false;
            }

            else
            {
                isCrouching = true;
            }
        }
    }

    void OnJump(InputValue value)
    {
        if (value.isPressed && canJump && isGrounded && !isCrouching)
        {
            velocity.y = Mathf.Sqrt(jumpForce * -2f * Physics.gravity.y);
            isJumping = true;
            coolDownJumping = 0.2f;
        }
    }

    void OnShift(InputValue value)
    {
        //Action Type: Value , Control Type: Any
        if (value.isPressed)
        {
            isRunning = true;
        }

        else if (!value.isPressed)
        {
            isRunning = false;
        }
    }

    void AdjustSpeed()
    {
        
        if (!isCrouching)
        {
            //Walking
            if (currentSpeed != walkingSpeed && !isRunning || currentSpeed == crouchSpeed)
            {
                currentSpeed = walkingSpeed;
            }


            //Running
            else if (currentSpeed != runningSpeed && isRunning && canRun)
            {
                currentSpeed = runningSpeed;
            }

            if (!isRunning)
            {
                currentSpeed = walkingSpeed;
            }
        }

        //Crouching
        if (isCrouching && currentSpeed != crouchSpeed)
        {
            currentSpeed = crouchSpeed;
        }
        
    }

    void RotatePlayer()
    {
        transform.Rotate(Vector3.up * mouseX);
    }

    void RotateCamera()
    {
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);         // Limits mouse updown rotation

        mainCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    }

    private void Move()
    {
        moveDirection = ((transform.right * inputDirection.x) + (transform.forward * inputDirection.y)).normalized * currentSpeed;
        cc.Move(moveDirection * Time.deltaTime);

        velocity.y -= 9.81f * Time.deltaTime; //gravity
        cc.Move(velocity * Time.deltaTime);
    }

    void Crouch()
    {
        if (crouchLerp < 1)
            crouchLerp += 2.5f * Time.deltaTime;

        crouchLerp = Mathf.Clamp(crouchLerp, 0, 1);

        if (!isCrouching && cc.height != orginalHeight)
        {
            cc.height = Mathf.Lerp(cc.height, orginalHeight, crouchLerp);
            cc.center = Vector3.Lerp(cc.center, new Vector3(0, orginalHeight / 2, 0), crouchLerp);
            mainCamera.transform.position = Vector3.Slerp(mainCamera.transform.position, transform.position + new Vector3(0, 1.5f, 0), 5f * Time.deltaTime);
        }

        if (isCrouching && cc.height != wantedHeight)
        {
            cc.height = Mathf.Lerp(cc.height, wantedHeight, crouchLerp);
            cc.center = Vector3.Lerp(cc.center, new Vector3(0, wantedHeight / 2, 0), crouchLerp);
            mainCamera.transform.position = Vector3.Slerp(mainCamera.transform.position, transform.position + new Vector3(0, 0.5f, 0), 5f * Time.deltaTime);
        }
    }

    private bool isGrounded
    {
        get
        {
            return
                 //Physics.Raycast(transform.position + new Vector3(0, 0.25f, 0), Vector3.down, 0.5f);
                 Physics.Raycast(transform.position + new Vector3(0, 0.25f, 0), Vector3.down, out hit, 0.5f);
        }
    }

    private bool cantStand
    {
        get
        {
            return
                Physics.Raycast(transform.position + new Vector3(0, 1f, 0), Vector3.up, 2.5f);
        }
    }

    public bool onGround
    {
        get
        {
            return isGrounded;
        }
    }
    public bool onMove
    {
        get
        {
            if (moveDirection.x != 0 || moveDirection.z != 0) return true;
            else return false;
        }
    }
    public bool onRun
    {
        get
        {
            return isRunning;
        }
    }

    public string tagName
    {
        get
        {
            return hit.collider.tag.ToString();
        }
    }

}
Editor is loading...
Leave a Comment