Game视图实现Scene鼠标滑动观看

43 篇文章 0 订阅
5 篇文章 0 订阅
using UnityEngine;
using System.Collections;

public class bl_UHTCamera : MonoBehaviour {

    // WASDQE Panning
    public float minPanSpeed = 0.1f;    // Starting panning speed
    public float maxPanSpeed = 1000f;   // Max panning speed
    public float panTimeConstant = 20f; // Time to reach max panning speed

    // Mouse right-down rotation
    public float rotateSpeed = 10; // mouse down rotation speed about x and y axes
    public float zoomSpeed = 2;    // zoom speed

    float panT = 0;
    float panSpeed = 10;
    Vector3 panTranslation;
    bool wKeyDown = false;
    bool aKeyDown = false;
    bool sKeyDown = false;
    bool dKeyDown = false;
    bool qKeyDown = false;
    bool eKeyDown = false;

    Vector3 lastMousePosition;
    new Camera camera;

    void Start()
    {
        camera = GetComponent<Camera>();
    }

    void Update()
    {
        //
        // WASDQE Panning

        // read key inputs
        wKeyDown = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
        aKeyDown = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow);
        sKeyDown = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow);
        dKeyDown = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow);
        qKeyDown = Input.GetKey(KeyCode.Q);
        eKeyDown = Input.GetKey(KeyCode.E);

        // determine panTranslation
        panTranslation = Vector3.zero;
        if (dKeyDown && !aKeyDown)
            panTranslation += Vector3.right * Time.deltaTime * panSpeed;
        else if (aKeyDown && !dKeyDown)
            panTranslation += Vector3.left * Time.deltaTime * panSpeed;

        if (wKeyDown && !sKeyDown)
            panTranslation += Vector3.forward * Time.deltaTime * panSpeed;
        else if (sKeyDown && !wKeyDown)
            panTranslation += Vector3.back * Time.deltaTime * panSpeed;

        if (qKeyDown && !eKeyDown)
            panTranslation += Vector3.down * Time.deltaTime * panSpeed;
        else if (eKeyDown && !qKeyDown)
            panTranslation += Vector3.up * Time.deltaTime * panSpeed;
        transform.Translate(panTranslation, Space.Self);

        // Update panSpeed
        if (wKeyDown || aKeyDown || sKeyDown ||
            dKeyDown || qKeyDown || eKeyDown)
        {
            panT += Time.deltaTime / panTimeConstant;
            panSpeed = Mathf.Lerp(minPanSpeed, maxPanSpeed, panT * panT);
        }
        else
        {
            panT = 0;
            panSpeed = minPanSpeed;
        }

        //
        // Mouse Rotation  鼠标控制game面板 实现scene面板滑动
        if (Input.GetMouseButton(1))
        {
            // if the game window is separate from the editor window and the editor
            // window is active then you go to right-click on the game window the
            // rotation jumps if  we don't ignore the mouseDelta for that frame.
            Vector3 mouseDelta;
            if (lastMousePosition.x >= 0 &&
                lastMousePosition.y >= 0 &&
                lastMousePosition.x <= Screen.width &&
                lastMousePosition.y <= Screen.height)
                mouseDelta = Input.mousePosition - lastMousePosition;
            else
                mouseDelta = Vector3.zero;

            var rotation = Vector3.up * Time.deltaTime * rotateSpeed * mouseDelta.x;
            rotation += Vector3.left * Time.deltaTime * rotateSpeed * mouseDelta.y;
            transform.Rotate(rotation, Space.Self);

            // Make sure z rotation stays locked
            rotation = transform.rotation.eulerAngles;
            rotation.z = 0;
            transform.rotation = Quaternion.Euler(rotation);
        }

        lastMousePosition = Input.mousePosition;

        //
        // Mouse Zoom
        camera.fieldOfView -= Input.mouseScrollDelta.y * zoomSpeed;
    }
}

第二种,将以下脚本添加到Camera上即可

using UnityEngine;
using System.Collections;

public class CameraFlyController : MonoBehaviour
{
    private float speed = 4f;

    private Transform tr;

    private Vector3 mpStart;
    private Vector3 originalRotation;

    private float t = 0f;

    // 
    void Awake()
    {
        tr = GetComponent<Transform>();
        t = Time.realtimeSinceStartup;
    }

    // 
    void Update()
    {
        // Movement
        float forward = 0f;
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { forward += 1f; }
        if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) { forward -= 1f; }

        float right = 0f;
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { right += 1f; }
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { right -= 1f; }

        float up = 0f;
        if (Input.GetKey(KeyCode.E) || Input.GetKey(KeyCode.Space)) { up += 1f; }
        if (Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.C)) { up -= 1f; }

        float dT = Time.realtimeSinceStartup - t;
        t = Time.realtimeSinceStartup;

        tr.position += tr.TransformDirection(new Vector3(right, up, forward) * speed * (Input.GetKey(KeyCode.LeftShift) ? 2f : 1f) * dT);

        // Rotation
        Vector3 mpEnd = Input.mousePosition;

        // Right Mouse Button Down
        if (Input.GetMouseButtonDown(1))
        {
            originalRotation = tr.localEulerAngles;
            mpStart = mpEnd;
        }

        // Right Mouse Button Hold
        if (Input.GetMouseButton(1))
        {
            Vector2 offs = new Vector2((mpEnd.x - mpStart.x) / Screen.width, (mpStart.y - mpEnd.y) / Screen.height);
            tr.localEulerAngles = originalRotation + new Vector3(offs.y * 360f, offs.x * 360f, 0f);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值