84 lines
2.1 KiB
C#
84 lines
2.1 KiB
C#
|
|
using EasyInject.Attributes;
|
|
using System;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.Playables;
|
|
/// <summary>
|
|
/// 相机控制
|
|
/// </summary>
|
|
[GameObjectBean]
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
public Transform target; // 要围绕其旋转的目标物体
|
|
|
|
[Space(5)]
|
|
[Header("缩放配置")]
|
|
public float zoomSpeed = 5.0f; // 缩放速度
|
|
public float minDistance = 1.0f; // 最小距离
|
|
public float maxDistance = 30.0f; // 最大距离
|
|
public float speed = 1f; // 最大距离
|
|
public float distance = 0f; // 最大距离
|
|
|
|
public float yMin = 1;
|
|
public float yMax = 60;
|
|
private float xRot = 0;
|
|
private float yRot = 0;
|
|
|
|
|
|
public bool isPlaying = true;
|
|
void Start()
|
|
{
|
|
distance = Vector3.Distance(transform.position, target.position);
|
|
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
//鼠标围绕目标旋转
|
|
mouseRoundRoller();
|
|
|
|
// 检测鼠标滚轮输入并进行缩放
|
|
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
|
|
|
|
if (scrollInput != 0)
|
|
{
|
|
// 根据鼠标滚轮的方向改变距离
|
|
Camera camera = transform.GetComponent<Camera>();
|
|
|
|
float result = camera.fieldOfView - scrollInput * zoomSpeed;
|
|
// 限制距离范围
|
|
result = Mathf.Clamp(result, minDistance, maxDistance);
|
|
camera.fieldOfView = result;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 鼠标操作旋转
|
|
/// </summary>
|
|
void mouseRoundRoller()
|
|
{
|
|
if (Input.GetMouseButton(0)) // 检查鼠标左键是否按下
|
|
{
|
|
// 根据鼠标移动量调整旋转角度
|
|
float mouseX = Input.GetAxis("Mouse X") * speed;
|
|
float mouseY = Input.GetAxis("Mouse Y") * speed;
|
|
|
|
yRot -= mouseX;
|
|
xRot += mouseY;
|
|
|
|
// 限制上下旋转角度
|
|
xRot = Mathf.Clamp(xRot, -90, 90);
|
|
// 应用新的旋转角度
|
|
transform.rotation = Quaternion.Euler(xRot, yRot, 0);
|
|
|
|
// 更新位置以保持与目标物体的距离不变
|
|
|
|
Vector3 v = target.position - transform.forward * distance;
|
|
transform.position = v;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|