unity3d模拟Scene视图camera控制

本文详细介绍了一个Unity相机控制脚本的实现原理和使用方法。该脚本通过中键滚动实现相机前进后退,右键拖动实现原地旋转,中键拖动实现上下左右平移。文章深入解析了脚本中的关键参数设置,如缩放速度、拖拽速度和旋转速度,并解释了如何通过输入模块控制相机的动态变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

中键滚动,camera前进后退
右键拖动,camera原地旋转
中键拖动,camera上下左右平移

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraCtrl : MonoBehaviour
{
    Transform m_trans;
    public float m_zoomSpeed = 1.0f;
    public float m_dragSpeed = 10.0f;
    public float m_rotateSpeed = 5.0f;
    Vector3 m_lastMousePos;

    bool m_isDraging = false;
    // Start is called before the first frame update
    void Start()
    {
        m_trans = this.transform;

    }

    private void LateUpdate()
    {
        Zoom();
        Rotate();
        Drag();
    }

    void Zoom()
    {
        Vector3 pos = m_trans.position;
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            pos -= m_trans.forward * m_zoomSpeed;
        }

        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            pos += m_trans.forward * m_zoomSpeed;
        }

        m_trans.position = pos;

    }

    void Rotate()
    {
        Vector3 angle = m_trans.eulerAngles;
        if (Input.GetMouseButton(1))
        {
            float x = Input.GetAxis("Mouse X");
            float y = Input.GetAxis("Mouse Y");
            angle.x -= y* m_rotateSpeed;
            angle.y += x* m_rotateSpeed;

        }

        Quaternion rotation = Quaternion.Euler(angle);
        m_trans.rotation = rotation;

    }

    private void Drag()
    {
        if (Input.GetMouseButton(2))
        {
            if (m_isDraging == false)
            {
                m_isDraging = true;
                m_lastMousePos = Input.mousePosition;
            }
            else {
                Vector3 newMousePos = Input.mousePosition;
                Debug.Log(newMousePos);
                Vector3 delta = newMousePos - m_lastMousePos;
                m_trans.position += CalcDragLength(gameObject.GetComponent<Camera>(), delta) * m_dragSpeed;
                m_lastMousePos = newMousePos;
            }
        }

        if (Input.GetMouseButtonUp(2))
        {
            m_isDraging = false;
        }
    }

    private Vector3 CalcDragLength(Camera camera, Vector2 mouseDelta)
    {
        float rectHeight = -1;
        float rectWidth = -1;
        if (camera.orthographic)
        {
            rectHeight = 2 * camera.orthographicSize;
            //rectWidth = rectHeight / camera.aspect;
        }
        else
        {
            rectHeight = 2  * Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
        }
        rectWidth = Screen.width * rectHeight / Screen.height;
        Vector3 moveDir = -rectWidth / Screen.width * mouseDelta.x * camera.transform.right - rectHeight / Screen.height * mouseDelta.y * camera.transform.up;

        return moveDir;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值