Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using UnityEditor.ShortcutManagement;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public Transform viewPoint;
- public float mouseSensitivity = 1f;
- private float verticalRotStore;
- //Vertical Rotation Score
- public bool invertLook;
- public CharacterController charCon;
- private float activeMoveSpeed;
- public float moveSpeed = 5f, runSpeed = 8f;
- private Vector3 moveDir, movement;
- private Vector2 mouseInput;
- private Camera cam;
- public float jumpForce = 12f;
- public float gravityMod = 2.5f;
- public Transform groundCheckPoint;
- private bool isGrounded;
- public LayerMask groundLayers;
- // Start is called before the first frame update
- void Start()
- {
- Cursor.lockState = CursorLockMode.Locked;
- cam = Camera.main;
- }
- // Update is called once per frame
- void Update()
- {
- mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;
- transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + mouseInput.x, transform.rotation.eulerAngles.z);
- verticalRotStore += mouseInput.y;
- verticalRotStore = Mathf.Clamp(verticalRotStore, -60f, 60f);
- if(invertLook)
- {
- viewPoint.rotation = Quaternion.Euler(verticalRotStore, viewPoint.rotation.eulerAngles.y, viewPoint.rotation.eulerAngles.z);
- }
- else
- {
- viewPoint.rotation = Quaternion.Euler(-verticalRotStore, viewPoint.rotation.eulerAngles.y, viewPoint.rotation.eulerAngles.z);
- }
- moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
- if(Input.GetKey(KeyCode.LeftShift))
- {
- activeMoveSpeed = runSpeed;
- }
- else
- {
- activeMoveSpeed = moveSpeed;
- }
- float yVel = movement.y;
- movement = ((transform.forward * moveDir.z) + (transform.right * moveDir.x)).normalized * activeMoveSpeed;
- movement.y = yVel;
- if(charCon.isGrounded)
- {
- movement.y = 0f;
- }
- charCon.Move( movement * Time.deltaTime);
- movement.y += Physics.gravity.y * Time.deltaTime * gravityMod;
- isGrounded = Physics.Raycast(groundCheckPoint.position, Vector3.down, 0.25f, groundLayers);
- if(Input.GetButtonDown("Jump") && isGrounded)
- {
- movement.y = jumpForce;
- }
- }
- private void LateUpdate()
- {
- cam.transform.position = viewPoint.position;
- cam.transform.rotation = viewPoint.rotation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement