PlayerController
Thu Apr 28 2022 14:07:10 GMT+0000 (Coordinated Universal Time)
Saved by @DanDHenshaw
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Controls controls;
private CharacterController controller;
[SerializeField] private GameObject head;
private AudioSource audioSource;
private Animator animator;
[Header("Player")]
[SerializeField] private float walkSpeed = 4f;
[SerializeField] private float jumpHeight = 1.5f;
[SerializeField] private float gravity = -19.62f;
[SerializeField] [Range(0f, 1f)] private float smoothInputSpeed = 0.2f;
[SerializeField] private AudioClip[] footstepSounds;
private Vector3 velocity;
private Vector2 currentInput;
private Vector2 smoothInputVelocity;
[Header("Player Grounded")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float radius;
[SerializeField] private LayerMask groundLayer;
[Header("Mouse")]
[SerializeField] private float sensitivityX = 100f;
[SerializeField] private float sensitivityY = 100f;
private float xRotation = 0f;
[Header("Weapons")]
[SerializeField] private GameObject revolver;
[SerializeField] private GameObject shotgun;
bool canSwap = true;
private Vector3 lastPosition;
private float moveMinimum = 0.01f;
private void Awake()
{
controls = new Controls();
controls.Player.Primary.performed += _ => Primary();
controls.Player.Secondary.performed += _ => Secondary();
controls.Player.Jump.performed += _ => Jump();
controller = GetComponent<CharacterController>();
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
Move();
Look();
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (IsMoving())
{
animator.SetBool("Walking", true);
}
else
{
animator.SetBool("Walking", false);
}
lastPosition = transform.position;
}
private void Move()
{
Vector2 input = controls.Player.Movement.ReadValue<Vector2>();
currentInput = Vector2.SmoothDamp(currentInput, input, ref smoothInputVelocity, smoothInputSpeed);
Vector3 direction = (-currentInput.y * transform.forward) + (-currentInput.x * transform.right);
controller.Move(direction * Time.deltaTime * walkSpeed);
}
private void Look()
{
Vector2 mouseLook = controls.Player.Look.ReadValue<Vector2>();
float mouseX = mouseLook.x * sensitivityX * Time.deltaTime;
float mouseY = mouseLook.y * sensitivityY * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -75f, 75f);
head.transform.localRotation = Quaternion.Euler(-xRotation, 0, 0);
transform.Rotate(Vector3.up * mouseX);
}
private void Jump()
{
if (IsGrounded())
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
private void Primary()
{
if (canSwap)
{
canSwap = false;
shotgun.SetActive(false);
revolver.SetActive(true);
Invoke("CanSwapWeapons", 0.75f);
}
}
private void Secondary()
{
if (canSwap)
{
canSwap = false;
revolver.SetActive(false);
shotgun.SetActive(true);
Invoke("CanSwapWeapons", 0.75f);
}
}
public void Footstep()
{
audioSource.clip = footstepSounds[Random.Range(0, footstepSounds.Length)];
audioSource.Play();
}
bool IsMoving()
{
float distance = Vector3.Distance(transform.position, lastPosition);
return (distance > moveMinimum);
}
private void CanSwapWeapons()
{
canSwap = true;
}
private bool IsGrounded()
{
bool isGrounded = Physics.CheckSphere(groundCheck.position, radius, groundLayer);
Debug.Log(isGrounded);
return isGrounded;
}
private void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(groundCheck.position, radius);
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}



Comments