unity 物体3D显示效果(可拖动,放大缩小)

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值