您好,欢迎来到爱够旅游网。
搜索
您的当前位置:首页Unity3d 类似Scene窗口的鼠标变换控制

Unity3d 类似Scene窗口的鼠标变换控制

来源:爱够旅游网

刚刚在学习光线跟踪,通过控制Camera里的Transform来进行观察渲染结果是在不方便,就写了一个简单的鼠标控制,记录一下,以后要用的话就可以直接复制了 美滋滋。

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

public class CameraTransform : MonoBehaviour
{
    [SerializeField]
    Transform _camera;
    [SerializeField]
    float _translateSpeed=30;
    [SerializeField]
    float _rotateSensitivity=240;

    bool _bSpeedUp;
    float _acTranslateSpeed = 60;

    // Update is called once per frame
    void Update()
    {
        if (_camera == null)
            _camera = GameObject.FindGameObjectWithTag("Main Camera").GetComponent<Transform>();

        InputCtrl();
    }
    void InputCtrl()
    {
        if (Input.GetMouseButton(1))
        {
            Vector3 offset = Vector3.zero;
            bool bInput = false;
            CalTranslateOffset(out offset,out bInput);
            if (bInput)
                Translate(offset);
            bInput = false;
            Vector2 angleOffset = Vector2.zero;
            CalRotateOffset(out angleOffset, out bInput);
            if (bInput)
                Rotation(angleOffset);
        }
       

    }
    void CalRotateOffset(out Vector2 angleOffset, out bool bInput)
    {
        float h=Input.GetAxis("Mouse X");
        float v = Input.GetAxis("Mouse Y");
        if (Mathf.Abs(h) > 0.005 || Mathf.Abs(v) > 0.005f)
        {
            bInput = true;
            angleOffset = new Vector3(h * _rotateSensitivity * Time.deltaTime, v * _rotateSensitivity * Time.deltaTime);
        }
        else
        {
            bInput = false;
            angleOffset = Vector2.zero;
        }
    }


    void CalTranslateOffset(out Vector3 offset,out bool bInput)
    {
        offset = Vector3.zero;
        bInput = false;
        float currentSpeed = _translateSpeed ;
        if(Input.GetKey(KeyCode.LeftShift))
        {
            _bSpeedUp = true;
            currentSpeed = _acTranslateSpeed;
        }
        if (Input.GetMouseButton(1))
        {
            if (Input.GetKey(KeyCode.W))
            {
                bInput = true;
                offset += _camera.forward.normalized * currentSpeed * Time.deltaTime;
            }
            if (Input.GetKey(KeyCode.S))
            {
                bInput = true;
                offset += -1 * _camera.forward.normalized * currentSpeed * Time.deltaTime;
            }
            if (Input.GetKey(KeyCode.A))
            {
                bInput = true;
                offset += -1 * _camera.right.normalized * currentSpeed * Time.deltaTime;
            }
            if (Input.GetKey(KeyCode.D))
            {
                bInput = true;
                offset += _camera.right.normalized * currentSpeed * Time.deltaTime;
            }
        }
        _bSpeedUp = false;
    }
    void Translate(Vector3 offset)
    {
        Vector3 targetPosition = _camera.transform.position;
        targetPosition += offset;
        _camera.transform.localPosition = targetPosition;
    }
    void Rotation(Vector2 offset)
    {
        Vector3 targetAngle = _camera.localRotation.eulerAngles;
        targetAngle.x -= offset.y;
        targetAngle.y += offset.x;
        _camera.localRotation = Quaternion.Euler(targetAngle);

        //_camera.Rotate(-Vector3.right, offset.y);
        //_camera.Rotate(Vector3.up, offset.x);
    }

}

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务