DeepSeek教unity------Free Camera

模仿 Unity 的场景视图相机控件:平移、轨道、缩放,运行时可用!

using System.Collections;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    [Header("Params"), Space]
    [SerializeField] private float moveSpeed = 10f;
    [SerializeField] private float rotationSpeed = 100f;
    [SerializeField] private float panSpeed = 10f;
    [SerializeField] private float zoomSpeed = 10f;

    private float moveSpeedMod = 1;

    private bool _isFlying;
    private bool _isPanning;
    private float _sprint = 1f;
    private Coroutine moveSpeedCoroutine;



    void Update()
    {
        _isFlying = Input.GetMouseButton(1); 
        _isPanning = Input.GetMouseButton(2);
        bool shift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
        _sprint = shift ? 3f : 1f;

        if (_isFlying)
        {
            RotateCamera();
            MoveCamera();
            MoveCameraVerticaly();
            MoveSpeedModifcation();
        }
        else
        {
            ZoomCamera();
        }

        if (_isPanning && !_isFlying)
            PanCamera();
    }

    void RotateCamera()
    {
        Vector3 deltaMouse = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
        float rotationX = deltaMouse.y * rotationSpeed * Time.unscaledDeltaTime;
        float rotationY = deltaMouse.x * rotationSpeed * Time.unscaledDeltaTime;
        transform.eulerAngles += new Vector3(-rotationX, rotationY, 0);
    }

    void PanCamera()
    {
        Vector3 deltaMouse = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
        Vector3 move = -transform.right * deltaMouse.x + -transform.up * deltaMouse.y;
        transform.position += move * panSpeed * Time.unscaledDeltaTime;
    }

    void MoveCamera()
    {
        float horizontal = Input.GetAxis("Horizontal"); // A/D or 左右箭头
        float vertical = Input.GetAxis("Vertical");     // W/S or 上下箭头
        Vector2 movement = new Vector2(horizontal, vertical);

        Vector3 forward = transform.forward;
        forward.Normalize();

        Vector3 right = transform.right;

        Vector3 move = (forward * movement.y + right * movement.x) * moveSpeed * moveSpeedMod * _sprint * Time.unscaledDeltaTime;
        transform.position += move;
    }

    void MoveCameraVerticaly()
    {
        float movement = 0f;
        if (Input.GetKey(KeyCode.E)) movement += 1f;
        if (Input.GetKey(KeyCode.Q)) movement -= 1f;

        Vector3 up = transform.up;

        Vector3 move = up * movement * moveSpeed * moveSpeedMod * _sprint * Time.unscaledDeltaTime;
        transform.position += move;
    }

    void ZoomCamera()
    {
        float scroll = Input.mouseScrollDelta.y;
        if (scroll != 0f)
        {
            Vector3 forward = transform.forward;
            transform.position += forward * scroll * zoomSpeed * Time.unscaledDeltaTime;
        }
    }

    void MoveSpeedModifcation()
    {
        float scroll = Input.mouseScrollDelta.y;
        if (scroll != 0f)
        {
            moveSpeedMod += scroll * 0.1f;
            moveSpeedMod = Mathf.Clamp(moveSpeedMod, 0.01f, 2f);

            if (moveSpeedCoroutine != null)
            {
                StopCoroutine(moveSpeedCoroutine);
            }
            moveSpeedCoroutine = StartCoroutine(MoveSpeedPanel());
        }
    }

    IEnumerator MoveSpeedPanel()
    {
        if (moveSpeedMod < 0.1f)
        {
            Debug.Log($"{moveSpeedMod.ToString("F2").Replace('.', ',')}x");
        }
        else
        {
            Debug.Log($"{moveSpeedMod.ToString("0.00").Replace('.', ',')}x");
        }
        yield return new WaitForSeconds(1f);

        moveSpeedCoroutine = null;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值