前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >unity3d模拟Scene视图camera控制

unity3d模拟Scene视图camera控制

作者头像
立羽
发布2023-08-24 15:03:45
2340
发布2023-08-24 15:03:45
举报
文章被收录于专栏:Unity3d程序开发

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

代码语言:javascript
复制
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;
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-08-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档