using UnityEngine;
///
/// 上帝视角
///
[AddComponentMenu("ExtendedFlycam/上帝视角")]
public class ExtendedFlycam : MonoBehaviour
{
static ExtendedFlycam _inst;
public static ExtendedFlycam Inst
{
get
{
if (_inst == null)
{
_inst = new ExtendedFlycam();
}
return _inst;
}
}
public float cameraSensitivity = 90;
public float climbSpeed = 4;
public float normalMoveSpeed = 10;
public float slowMoveFactor = 0.25f;
public float fastMoveFactor = 3;
///
/// 旋转X增量
///
[HideInInspector] public float rotationX = 0.0f;
///
/// 旋转Y增量
///
[HideInInspector] public float rotationY = 0.0f;
private Vector3 previousMousePosition;
public Vector3 initialRotationEulerAngles; // 保存初始角度
private void Awake()
{
_inst = this;
previousMousePosition = Input.mousePosition;
//DateTime now = new DateTime(2023, 7, 25, 23, 0, 0);
//DateTime acquisition_time = now.Date.AddHours(now.Hour);//采集时间
//var dateTime_0C = acquisition_time.AddHours(1);//当前小时不再计算
//Debug.Log("acquisition_time:" + acquisition_time + "\ndateTime_0C:" + dateTime_0C);
}
void Start()
{
Cursor.visible = true;
initialRotationEulerAngles = transform.localEulerAngles;
}
private void OnEnable()
{
initialRotationEulerAngles = transform.localEulerAngles;
}
private bool HasMouseMoved()
{
float mouseX = Input.GetAxisRaw("Mouse X");
float mouseY = Input.GetAxisRaw("Mouse Y");
if (mouseX != 0 || mouseY != 0)
{
previousMousePosition = Input.mousePosition;
return true;
}
return false;
}
Vector3 ClampAngle(float x, float y, float z, Vector3 v)
{
if (x <= -360)
x += 360;
if (x >= 360)
x -= 360;
if (y <= -360)
y += 360;
if (y >= 360)
y -= 360;
if (z <= -360)
z += 360;
if (z >= 360)
z -= 360;
v.Set(x, y, z);
return v;
}
void Update()
{
//initialRotationEulerAngles = transform.localEulerAngles;
if (Input.GetMouseButton(1))
{
if (HasMouseMoved())
{
// 累加旋转增量
rotationX += Input.GetAxis("Mouse X") * cameraSensitivity;
rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity;
rotationY = Mathf.Clamp(rotationY, -90, 90);
//Debug.Log(rotationX + "\n" + rotationX);
// 从initialRotationEulerAngles开始旋转,然后添加旋转增量。
Vector3 rotation = initialRotationEulerAngles + new Vector3(-rotationY, rotationX, 0);
transform.localRotation = Quaternion.Euler(rotation);
//transform.localEulerAngles = rotation;
}
}
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
//GetComponent().enabled = true;
transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else
{
transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
//transform.position += transform.up * climbSpeed * Time.deltaTime*100;
if (GetComponent().fieldOfView >= 60)
{
GetComponent().fieldOfView = 60;
return;
}
GetComponent().fieldOfView += Time.deltaTime * 100;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
//transform.position -= transform.up * climbSpeed * Time.deltaTime*100;
if (GetComponent().fieldOfView <= 10)
{
GetComponent().fieldOfView = 10;
return;
}
GetComponent().fieldOfView -= Time.deltaTime * 100;
}
if (Input.GetKeyDown(KeyCode.End))
{
Cursor.visible = (Cursor.visible == true) ? false : true;
}
}
///
/// 重置相机角度初始增量
///
public void init_mainCamera_rot()
{
rotationX = 0;
rotationY = 0;
}
}