using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
[SerializeField]
public enum RotateController { KeyControllerRotate, MouseControllerRotate };
public class FirstPersonController : MonoBehaviour
{
public byte priority = 13;
public CinemachineVirtualCamera CM_FirstPerson;
public RotateController rotateController;
float moveH, moveV, rotaH, rotaV;
public float moveSpeed, RotaSpeed;
public bool CanRotate = true;
public int maxAngle;
private Vector3 resetPosition;
void Start()
{
if (CM_FirstPerson != null)
{
CM_FirstPerson.Priority = priority;
}
resetPosition = transform.position;
}
void Update()
{
Move();
if (CanRotate) {
Rota();
AngleLimit(maxAngle);
}
if (Input.GetMouseButtonDown(1))
{
CanRotate = !CanRotate;
}
}
void Move()
{
moveH = Input.GetAxis("Horizontal");
moveV = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.LeftControl))
{
return;
}
if (moveH != 0 || moveV != 0)
{
transform.Translate(moveH * moveSpeed * Time.deltaTime, 0, moveV * moveSpeed * Time.deltaTime);
transform.position = new Vector3(transform.position.x, resetPosition.y, transform.position.z);
}
}
void Rota()
{
if (rotateController == RotateController.KeyControllerRotate)
{
keyRotate();
}
else
{
mouseRotate();
}
}
void keyRotate()
{
if (Input.GetKey(KeyCode.LeftControl))
{
transform.Rotate(Vector3.up * moveH * RotaSpeed * Time.deltaTime, Space.World);
transform.Rotate(Vector3.right * -moveV * RotaSpeed * 0.7f * Time.deltaTime);
}
}
void mouseRotate()
{
rotaH = Input.GetAxis("Mouse X");
rotaV = Input.GetAxis("Mouse Y");
transform.Rotate(Vector3.up * rotaH * RotaSpeed * Time.deltaTime, Space.World);
transform.Rotate(Vector3.right * -rotaV * RotaSpeed * 0.7f * Time.deltaTime);
}
void AngleLimit(int maxAngle){
if(transform.localEulerAngles.x > maxAngle && transform.localEulerAngles.x < 180){
transform.localEulerAngles = new Vector3(maxAngle,transform.localEulerAngles.y,0);
}
if(transform.localEulerAngles.x < (360-maxAngle) && transform.localEulerAngles.x > 180){
transform.localEulerAngles = new Vector3(360-maxAngle,transform.localEulerAngles.y,0);
}
}
}