using System.Collections;
using System.Collections.Generic;
public class PlayerMotor : MonoBehaviour
public static PlayerMotor instance;
private CharacterController controller;
private Vector3 playerVelocity;
private Vector3 moveInput;
private Vector3 moveDirection;
private bool canJump, canDoubleJump;
public float gravity = -9.8f;
public float JumpHeight = 2;
public float CrouchTimer;
public Transform groundCheckPoint;
public LayerMask whatIsGround;
public Animator animator;
public Transform FirePoint;
public List<Gun> allGuns = new List<Gun>();
public List<Gun> unlockableGuns = new List<Gun>();
public Transform attackPoint;
public GameObject objectToThrow;
public float throwCooldown;
public float throwUpwardForce;
controller = GetComponent<CharacterController>();
CrouchTimer += Time.deltaTime;
float p = CrouchTimer / 1;
controller.height = Mathf.Lerp(controller.height, 1, p);
controller.height = Mathf.Lerp(controller.height, 2, p);
isGrounded = controller.isGrounded;
canJump = Physics.OverlapSphere(groundCheckPoint.position, .25f, whatIsGround).Length > 0;
animator.SetFloat("MoveSpeed", moveInput.magnitude);
animator.SetBool("onGround", isGrounded);
public void ProcessMove(Vector2 input)
moveDirection = Vector3.zero;
moveDirection.x = input.x;
moveDirection.z = input.y;
controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
playerVelocity.y += gravity * Time.deltaTime;
if (isGrounded && playerVelocity.y < 0)
controller.Move(playerVelocity * Time.deltaTime);
moveInput = moveDirection * speed;
if(!UIController.instance.pauseScreen.activeInHierarchy)
if (isGrounded && canJump)
playerVelocity.y = Mathf.Sqrt(JumpHeight * -3.0f * gravity);
AudioManager.instance.PlaySFX(7);
playerVelocity.y = Mathf.Sqrt(JumpHeight * -3.0f * gravity);
AudioManager.instance.PlaySFX(7);
if (Physics.Raycast (cam.position, cam.forward, out hit, 50))
if (Vector3.Distance(cam.position, hit.point) > 2f)
FirePoint.LookAt(hit.point);
FirePoint.LookAt(cam.position + cam.forward * 30);
public void SwitchWeapon()
activeGun.gameObject.SetActive(false);
if(currentGun >= allGuns.Count)
activeGun = allGuns[currentGun];
activeGun.gameObject.SetActive(true);
UIController.instance.ammoText.text = "AMMO" + ":" + activeGun.currentAmmo;
FirePoint.position = activeGun.firePoint.position;
activeGun = allGuns[currentGun];
activeGun.gameObject.SetActive(true);
UIController.instance.ammoText.text = "AMMO" + ":" + activeGun.currentAmmo;
FirePoint.position = activeGun.firePoint.position;
public void AddGun(string gunToAdd)
bool gunUnlocked = false;
if(unlockableGuns.Count > 0)
for(int i=0; i < unlockableGuns.Count; i++)
if(unlockableGuns[i].gunName == gunToAdd)
allGuns.Add(unlockableGuns[i]);
unlockableGuns.RemoveAt(i);
i = unlockableGuns.Count;
currentGun = allGuns.Count -2;
if(activeGun.currentAmmo >0)
activeGun.currentAmmo --;
Instantiate(activeGun.bullet, FirePoint.position, FirePoint.rotation);
UIController.instance.ammoText.text = "AMMO" + ":" + activeGun.currentAmmo;
public void PickAmmo (int ammoPickedUp)
activeGun.currentAmmo += ammoPickedUp;
public void PauseUnpause()
if(UIController.instance.pauseScreen.activeInHierarchy)
UIController.instance.pauseScreen.SetActive(false);
Cursor.lockState = CursorLockMode.Confined;
UIController.instance.pauseScreen.SetActive(true);
Cursor.lockState = CursorLockMode.None;
GameObject projectile = Instantiate(objectToThrow, attackPoint.position, camera.rotation);
Rigidbody projectileRb = projectile.GetComponent<Rigidbody>();
Vector3 forceDirection = camera.transform.forward;
if(Physics.Raycast(camera.position, camera.forward, out hit, 500f))
forceDirection = (hit.point - attackPoint.position).normalized;
Vector3 forceToAdd = forceDirection * throwForce + transform.up * throwUpwardForce;
projectileRb.AddForce(forceToAdd, ForceMode.Impulse);
Invoke(nameof(ResetThrow), throwCooldown);
private void ResetThrow()