using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public class Move3DCube : MonoBehaviour
{
public Transform pivot;//你要进行3D拖动的物体
public Vector3 pivotOffset = Vector3.zero;//位置默认为中心点
//public Transform target;// 像一个被选中的对象(用于检查Camera(摄像机)和target(目标)之间的对象)
/// <summary>
/// 游戏对象和3D物体之间的初始距离
/// </summary>
public float distance = 10.0f; //摄像机与3D物体初始距离
public float minDistance = 2f; //摄像机与3D物体最小距离
public float maxDistance = 15f; //摄像机与3D物体最大距离
public float zoomSpeed = 1f; //滚轮拖动速度倍率
/// <summary>
/// 鼠标右键拖动3D物体的速度
/// </summary>
public float xSpeed = 250.0f;
public float ySpeed = 250.0f;
public bool allowYTilt = true;
/// <summary>
/// Y轴锁定范围,鼠标拖动3D物体旋转Y轴最大旋转角度90,最小旋转角度-90
/// </summary>
public float yMinLimit = -90f;
public float yMaxLimit = 90f;
/// <summary>
/// 插值运算
/// </summary>
private float x = 0.0f;
private float y = 0.0f;
/// <summary>
/// 目标旋转角度
/// </summary>
private float targetX = 0f;
private float targetY = 0f;
/// <summary>
/// 目标旋转范围
/// </summary>
public float targetDistance = 0f;//目标距离
private float xVelocity = 1f; //x速度
private float yVelocity = 1f; //y速度
private float zoomVelocity = 1f; //速度倍率
private void Start()
{
var angles = transform.eulerAngles; //欧拉角,物体旋转属性的,以度为单位的欧拉角
targetX = x = angles.x; //初始化目标3D游戏物体旋转角X轴
targetY = y = ClampAngle(angles.y, yMinLimit, yMaxLimit); //初始化范围限定目标3D游戏物体旋转角Y轴
targetDistance = distance; //初始化和目标3D游戏物体距离为10
}
private void LateUpdate()
{
if (!pivot) return; //如果不存在设定的目标
#region 滚轮拖动
var scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll > 0.0f) targetDistance -= zoomSpeed; //滚轮前滑
else if (scroll < 0.0f) targetDistance += zoomSpeed;//滚轮后滑
targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);//目标距离限定范围,规定滚轮最大拖放和最小拖放
#endregion
#region 欧拉角旋转角度
//LeftControl左Ctrl键,RightControl右Ctrl键
if (Input.GetMouseButton(1) || (Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))))
{
targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
if (allowYTilt)
{
targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);//范围限定(targetY,-90,90)
}
}
#endregion
//Mathf.SmoothDampAngle(平滑阻尼角度)具有平滑效果,最好使用Mathf.Lerp(起始位置,目标位置,速度)
x = Mathf.Lerp(x, targetX, xVelocity);
//x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
// y = allowYTilt ? Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f) : targetY;//判断allowYTilt成立:左边,不成立:右边
y = allowYTilt ? Mathf.Lerp(y, targetY, yVelocity) : targetY;
Quaternion rotation = Quaternion.Euler(y, x, 0); //旋转角度
distance = Mathf.Lerp(distance, targetDistance, zoomVelocity);
//distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;
transform.rotation = rotation;
transform.position = position;
}
/// <summary>
/// 限定值,介于定义的Min和Max之间
/// </summary>
/// <param name="angle">目标选择的角度</param>
/// <param name="min">最小旋转度数</param>
/// <param name="max">最大旋转度数</param>
/// <returns></returns>
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360) angle += 360;
if (angle > 360) angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
unity 物体3D显示效果(可拖动,放大缩小)
猜你喜欢
转载自blog.csdn.net/m0_71624363/article/details/132012646
今日推荐
周排行