unity 相机鼠标控制移动旋转缩放

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

public class CameraCtrl02 : MonoBehaviour
{
    private Vector3 newPos;
    private Vector3 newZoom;
    private Quaternion newRota;

    [SerializeField] private float panSpeed = 1;
    [SerializeField] private float moveTime = 1;
    [SerializeField] private float rotationAmount = 1;
    [SerializeField] private Vector3 zoomAmount = new Vector3(0,-1,1);

    private Vector3 dragStartPos, dragCurrentPos;//鼠标拖拽起始点,鼠标拖拽当前位置
    private Vector3 rotateStart, rotateCurrent;

    private Transform cameraTrans;
    private void Start()
    {
        newPos = transform.position;
        newRota = transform.rotation;

        cameraTrans = transform.GetChild(0);
        newZoom = cameraTrans.localPosition;
    }
    void Update()
    {
        HandleMovementInput();//通过键盘控制相机
        HandleMouseInput();//通过鼠标控制相机
    }

    private void HandleMouseInput()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Plane plane = new Plane(Vector3.up, Vector3.zero);
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float distance;
            if (plane.Raycast(ray,out distance))
            {
                dragStartPos = ray.GetPoint(distance);
            }
        }
        if (Input.GetMouseButton(0))
        {
            Plane plane = new Plane(Vector3.up, Vector3.zero);
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float distance;
            if (plane.Raycast(ray, out distance))
            {
                dragCurrentPos = ray.GetPoint(distance);
                Vector3 difference = dragStartPos - dragCurrentPos;
                newPos = transform.position + difference;
            }
        }
        newZoom += Input.mouseScrollDelta.y * zoomAmount;

        if (Input.GetMouseButtonDown(2))
            rotateStart = Input.mousePosition;
        if (Input.GetMouseButton(2))
        {
            rotateCurrent = Input.mousePosition;
            Vector3 difference = rotateStart - rotateCurrent;
            rotateStart = rotateCurrent;
            newRota *= Quaternion.Euler(Vector3.up * -difference.x / 20);
        }
    }

    private void HandleMovementInput()
    {
        //移动
        if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
            newPos += transform.forward * panSpeed * Time.deltaTime;
        if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
            newPos -= transform.forward * panSpeed * Time.deltaTime;
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
            newPos += transform.right * panSpeed * Time.deltaTime;
        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
            newPos -= transform.right * panSpeed * Time.deltaTime;
        transform.position = Vector3.Lerp(transform.position, newPos, moveTime * Time.deltaTime);
        //旋转
        if (Input.GetKey(KeyCode.Q))
            newRota *= Quaternion.Euler(Vector3.up * rotationAmount);
        if (Input.GetKey(KeyCode.E))
            newRota *= Quaternion.Euler(Vector3.down * rotationAmount);
        transform.rotation = Quaternion.Lerp(transform.rotation, newRota, moveTime * Time.deltaTime);
        //缩放
        if (Input.GetKey(KeyCode.R))
            newZoom += zoomAmount;
        if (Input.GetKey(KeyCode.F))
            newZoom -= zoomAmount;
        cameraTrans.localPosition = Vector3.Lerp(cameraTrans.localPosition, newZoom, moveTime * Time.deltaTime);

    }
}

参考B站up主 :BeaverJoe

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值