using DG.Tweening;
using UnityEngine;
namespace YiYongjiaSuQi_202192 {
public class ZhanShiCameraMove : MonoBehaviour
{
[Header("旋转角度x,y缩放距离distance")]
public float x, y, distance;
[Header("X角度设置")]
public float minYangle;
public float maxYangle;
[Header("缩放的最小距离")]
public float distanceMin;
[Header("缩放的最大距离")]
public float distanceMax;
[Header("需要注视的物体")]
public Transform target;
public static ZhanShiCameraMove instance;
[Header("缩放的速度")]
public float scrollSpeed;
[Header("旋转的速度")]
public float rotSpeed;
// Start is called before the first frame update
private void Start()
{
//初始化最开始位置
instance = this;
transform.LookAt(target);
}
// Update is called once per frame
private void Update()
{
//如果鼠标右键按下
if (Input.GetMouseButton(1))
{
//获取鼠标X轴移动量
x = x + Input.GetAxis("Mouse X") * rotSpeed;
//获取鼠标Y轴移动量
y = y - Input.GetAxis("Mouse Y") * rotSpeed;
}
//根绝X,Y移动量计算旋转量
y = Mathf.Clamp(y, minYangle,maxYangle);
Quaternion rot = Quaternion.Euler(y, x, 0);
//根据计算的旋转量旋转计算机
transform.rotation = rot;
//如果滚轮发生滚动
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
//根据滚轮的值计算距离
distance = distance - Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
}
//对距离进行区间运算,保证距离在最大和最小之间
distance = Mathf.Clamp(distance, distanceMin, distanceMax);
//根据距离值计算摄像机的位置
Vector3 pos = rot * new Vector3(0, 0, -distance) + target.position;
//更改摄像机位置为计算的值
transform.position = pos;
}
}
}
360度旋转物体
最新推荐文章于 2024-04-28 22:17:04 发布