Player_Control

mail@pastecode.io avatar
unknown
csharp
3 years ago
3.8 kB
5
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Control : MonoBehaviour
{
    private Camera mainCamera;
    private CapsuleCollider cc;
    private Rigidbody rb;

    private Vector3 inputDirection;
    private Vector3 velocity;

    [Header("Mouselook")]
    [SerializeField] private float mouseSensitivity = 120f;
    private float mouseX, mouseY;
    private float rotationX = 0f;

    [Header("Abilities")]
    [SerializeField] private float walkSpeed = 3f;
    [SerializeField] private float runSpeed = 5f;
    [Space(5)]
    [SerializeField] private float jumpForce = 1.2f;

    private float currentSpeed;

    [Header("Licenses")]
    [SerializeField] private bool canMove; private bool isMoving;
    [SerializeField] private bool canJump; private bool isJumping;

    private void Awake()
    {
        //Adds and adjusts collision.
        cc = gameObject.AddComponent<CapsuleCollider>();
        cc.center = new Vector3(0, 1, 0);
        cc.radius = 0.5f;
        cc.height = 2;

        //cc.material = Resources.Load("PhysicMats/Slippery") as PhysicMaterial;

        rb = gameObject.AddComponent<Rigidbody>();
        rb.constraints = RigidbodyConstraints.FreezeRotation;
        rb.interpolation = RigidbodyInterpolation.Interpolate;
        rb.collisionDetectionMode = CollisionDetectionMode.Continuous;
        rb.mass = 10;

        //Finds camera and parent it to player.
        mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
        mainCamera.transform.position = transform.position + new Vector3(0, 1.5f, 0);
        mainCamera.transform.parent = transform;

        Cursor.lockState = CursorLockMode.Locked;
    }

    private void Update()
    {
        Inputs();
        Rotation();
        MovingSpeed();

        if (onGround && isJumping)
        {
            isJumping = false;
        }
    }

    private void FixedUpdate()
    {
        Move();
    }

    void Inputs()
    {
        //Moving input
        inputDirection = (transform.right * Input.GetAxis("Horizontal")) + (transform.forward * Input.GetAxis("Vertical"));

        //Rotate input
        mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        //This is for camera
        rotationX -= mouseY;

        //Limitation for looking up/down
        rotationX = Mathf.Clamp(rotationX, -90f, 90f);

        //Jump
        if (onGround && Input.GetKeyDown(KeyCode.Space))
        {
            Jump();
        }


    }

    void MovingSpeed()
    {
        if (Input.GetKey(KeyCode.LeftShift) && currentSpeed != runSpeed)
        {
            currentSpeed = runSpeed;
        }

        else if (!Input.GetKey(KeyCode.LeftShift) && currentSpeed != walkSpeed)
        {
            currentSpeed = walkSpeed;
        }
    }

    void Move()
    {
        velocity = inputDirection.normalized * currentSpeed;;
        rb.velocity = new Vector3(velocity.x, rb.velocity.y, velocity.z);
    }

    void Rotation()
    {
        //Player
        transform.Rotate(Vector3.up * mouseX);

        //Camera
        mainCamera.transform.localRotation = Quaternion.Euler(rotationX, 0f, 0f);
    }

    void Jump()
    {
       // isJumping = true;
        rb.velocity = new Vector3(rb.velocity.x, Mathf.Sqrt(2 * jumpForce * -Physics.gravity.y), rb.velocity.z);
    }


    //For ground check
    bool onGround
    {
        get
        {
            return Physics.Raycast(transform.position + new Vector3(0, 0.5f, 0), Vector3.down, 1);
        }
    }
}