Unity相机查看预览旋转模型

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraTargetMove : MonoBehaviour
{
    public Transform target;
    public Vector3 pivotOffset = Vector3.zero;
    public float distance = 10.0f;
    public float minDistance = 2f;
    public float maxDistance = 15f;
    public float zoomSpeed = 1f;
    public float xSpeed = 250.0f;
    public float ySpeed = 250.0f;
    public bool allowYTilt = true;
    public float yMinLimit = -90f;
    public float yMaxLimit = 90f;
    private float x = 0.0f;
    private float y = 0.0f;
    private float targetX = 0f;
    private float targetY = 0f;
    public float targetDistance = 0f;

    private void Start()
    {
        var angles = transform.eulerAngles;
        targetX = x = angles.x;
        targetY = y = ClampAngle(angles.y, yMinLimit, yMaxLimit);
        targetDistance = distance;

    }

    private void LateUpdate()
    {
        if (!target) return;
        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);
        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);
            }
        }

        x = targetX;
        y = targetY;
        Quaternion rotation = Quaternion.Euler(y, x, 0);
        distance = targetDistance;
        Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position + pivotOffset;
        transform.rotation = rotation;
        transform.position = position;
    }


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

阻尼感效果 相机查看模型

csharp 复制代码
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class MoveCameraByMouse : MonoSingleton<MoveCameraByMouse>

{

    // 围绕旋转的目标物体

    public Transform target;

    // 设置旋转角度

    public float x = 0f, y = 0f, z = 0f;

    public bool xFlag = false, yFlag = false;

    // 旋转速度值

    public float xSpeed = 10f, ySpeed = 10f, mSpeed = 5f;

    // y轴角度限制,设置成一样则该轴不旋转

    public float yMinLimit = -50, yMaxLimit = 80;

    // x轴角度限制,同上

    public float leftMax = -365, rightMax = 365;

    // 距离限制,同上

    public float distance = 3f, minDistance = 0.5f, maxDistance = 6f;

    // 阻尼设置

    public bool needDamping = true;

    public float damping = 3f;

    public float initX;

    public float initY;

    // 改变中心目标物体

    public void SetTarget(GameObject go)

    {

        target = go.transform;

    }

    void Start()

    {

        Vector3 angles = transform.eulerAngles;

        x = angles.y;

        y = angles.x;



        //pers();

    }

    void LateUpdate()

    {

        if (target)

        {

            if (Input.GetMouseButton(1))

            {

                // 判断是否需要反向旋转

                if ((y > 90f && y < 270f) || (y < -90 && y > -270f))

                {

                    x -= Input.GetAxis("Mouse X") * xSpeed * 0.02f;

                }

                else

                {

                    x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;

                }

                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;



                x = ClampAngle(x, leftMax, rightMax);

                y = ClampAngle(y, yMinLimit, yMaxLimit);

            }



            distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;

            distance = Mathf.Clamp(distance, minDistance, maxDistance);



            Quaternion rotation = Quaternion.Euler(y, x, z);

            Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);

            Vector3 position = rotation * disVector + target.position;



            // 阻尼感

            if (needDamping)

            {

                transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);

                transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);

            }

            else

            {

                transform.rotation = rotation;

                transform.position = position;

            }



        }

    }

    // 对数值进行限制;

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

    }

    // 初始

    public void pers()

    {

        this.x = initX;

        this.y = initY;

    }


    public void Reserpos(Vector2 vector)
    {
        //this.x = vector.x;

        //this.y = vector.y;
        this.x = 1f;
       
        this.y = 35f;
        this.z = 0;
        distance = 2; minDistance = 1f; maxDistance = 4;
       
    }
    // 正视图

    public void front()

    {

        this.x = 0f;

        this.y = 0f;

    }

    // 后视图

    public void back()

    {

        this.x = 180f;

        this.y = 0f;

    }

    // 左视图

    public void left()

    {

        this.x = 90f;

        this.y = 0f;

    }

    // 右视图

    public void right()

    {

        this.x = 270f;

        this.y = 0f;

    }

    // 俯视图

    public void top()

    {

        this.x = 0f;

        this.y = 90f;

    }

    // 仰视图

    public void bottom()

    {

        this.x = 0f;

        this.y = -90f;

    }

}
相关推荐
蔗理苦1 小时前
2024-12-24 NO1. XR Interaction ToolKit 环境配置
unity·quest3·xr toolkit
花生糖@1 小时前
Android XR 应用程序开发 | 从 Unity 6 开发准备到应用程序构建的步骤
android·unity·xr·android xr
向宇it1 小时前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
虾球xz2 小时前
游戏引擎学习第55天
学习·游戏引擎
虾球xz4 小时前
游戏引擎学习第58天
学习·游戏引擎
ue星空5 小时前
虚幻引擎结构之UWorld
游戏引擎·虚幻
ue星空5 小时前
虚幻引擎结构之ULevel
游戏引擎·虚幻
向宇it6 小时前
【从零开始入门unity游戏开发之——unity篇01】unity6基础入门开篇——游戏引擎是什么、主流的游戏引擎、为什么选择Unity
开发语言·unity·c#·游戏引擎
神洛华8 小时前
Y3地图制作1:水果缤纷乐、密室逃脱
编辑器·游戏引擎·游戏程序
向宇it11 小时前
【从零开始入门unity游戏开发之——C#篇26】C#面向对象动态多态——接口(Interface)、接口里氏替换原则、密封方法(`sealed` )
java·开发语言·unity·c#·游戏引擎·里氏替换原则