GlobalGameJame/Assets/Scripts/PlayerManager.cs

120 lines
4.0 KiB
C#
Raw Normal View History

2020-01-31 20:47:30 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
public float speed = 10;
public float runSpeed = 25;
public float rotateSpeed = 5;
public float mouseSensitive = 200;
private bool run = false;
private Camera playerCamera;
private Rigidbody rigidBody;
public List<GameObject> weaponList = new List<GameObject>();
[HideInInspector]public int selectedWeapon = 0;
public List<GameObject> weaponHandList = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
playerCamera = GetComponentInChildren<Camera>();
rigidBody = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
Move();
RunSwitch();
selectWeapon();
}
void FixedUpdate()
{
Rotate();
}
void Move()
{
rigidBody.MovePosition(
transform.position +
(transform.forward * (run ? runSpeed : speed) * Input.GetAxis("Vertical") * Time.deltaTime) +
(transform.right * (run ? runSpeed : speed) * Input.GetAxis("Horizontal") * Time.deltaTime)
);
}
void Rotate()
{
transform.Rotate(new Vector3(0, rotateSpeed * Input.GetAxis("Mouse X") * mouseSensitive * Time.deltaTime, 0));
if (
((playerCamera.transform.localEulerAngles.x >= 270 && playerCamera.transform.localEulerAngles.x <= 360) &&
playerCamera.transform.localEulerAngles.y == 0 &&
playerCamera.transform.localEulerAngles.z == 0) ||
((playerCamera.transform.localEulerAngles.x <= 90 && playerCamera.transform.localEulerAngles.x >= 0) &&
playerCamera.transform.localEulerAngles.y == 0 &&
playerCamera.transform.localEulerAngles.z == 0)
) {
playerCamera.transform.Rotate(new Vector3(rotateSpeed * -Input.GetAxis("Mouse Y") * mouseSensitive * Time.deltaTime, 0, 0));
} else if (
((playerCamera.transform.localEulerAngles.x >= 270 && playerCamera.transform.localEulerAngles.x <= 360) &&
playerCamera.transform.localEulerAngles.y == 180 &&
playerCamera.transform.localEulerAngles.z == 180)
) {
playerCamera.transform.Rotate(new Vector3(rotateSpeed * (-Input.GetAxis("Mouse Y") > 0 ? -Input.GetAxis("Mouse Y") : 0) * mouseSensitive * Time.deltaTime, 0, 0));
} else if (
((playerCamera.transform.localEulerAngles.x <= 90 && playerCamera.transform.localEulerAngles.x >= 0) &&
playerCamera.transform.localEulerAngles.y == 180 &&
playerCamera.transform.localEulerAngles.z == 180)
) {
playerCamera.transform.Rotate(new Vector3(rotateSpeed * (-Input.GetAxis("Mouse Y") < 0 ? -Input.GetAxis("Mouse Y") : 0) * mouseSensitive * Time.deltaTime, 0, 0));
}
}
void RunSwitch()
{
if (Input.GetAxisRaw("Run") > 0)
{
run = true;
}
else
{
run = false;
}
}
void selectWeapon()
{
if (weaponList.Count > 0 && weaponHandList.Count == weaponList.Count)
{
weaponHandList[selectedWeapon].SetActive(false);
if (Input.mouseScrollDelta.y > 0)
{
if (weaponList.Count > selectedWeapon + 1) {
selectedWeapon++;
}
else
{
selectedWeapon = 0;
}
}
else if (Input.mouseScrollDelta.y < 0)
{
if (selectedWeapon > 0)
{
selectedWeapon--;
}
else
{
selectedWeapon = weaponList.Count - 1;
}
}
weaponHandList[selectedWeapon].SetActive(true);
}
}
}