unity实现相机位置移动

在unity场景中经常有通过键盘中W、S、A、D、Q、E等按键控制相机移动的需求,相机位置更新

控制代码如下:

private void Update()
    {
        if (!_active)
            return;

        // Translation
        if (_enableTranslation)
        {
            transform.Translate(Vector3.forward * Input.mouseScrollDelta.y * Time.deltaTime * _translationSpeed);
        }

        // Movement
        if (_enableMovement)
        {
            Vector3 deltaPosition = Vector3.zero;
            float currentSpeed = _movementSpeed;

            if (Input.GetKey(_boostSpeed))
                currentSpeed = _boostedSpeed;

            if (Input.GetKey(KeyCode.W))
            {
                deltaPosition += transform.forward;
            }
                
            if (Input.GetKey(KeyCode.S))
                deltaPosition -= transform.forward;

            if (Input.GetKey(KeyCode.A))
                deltaPosition -= transform.right;

            if (Input.GetKey(KeyCode.D))
                deltaPosition += transform.right;

            // Calc acceleration
            CalculateCurrentIncrease(deltaPosition != Vector3.zero);

            transform.position += deltaPosition * currentSpeed * _currentIncrease;
        }

        // Return to init position
        if (Input.GetKeyDown(_initPositonButton))
        {
            transform.position = _initPosition;
            transform.eulerAngles = _initRotation;
        }
    }

当需要限制相机位置时通过在Update函数中添加下判断相机当前的x/y/z位置信息即可实现

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用以下步骤来实现Unity相机移动并聚焦鼠标点击位置: 1. 在场景中创建一个空对象,并将其命名为“CameraController”或其他你喜欢的名字。 2. 将主相机(或任何你想要控制的相机)作为“CameraController”对象的子对象。 3. 在“CameraController”对象上添加一个脚本,并将其命名为“CameraFocus”。 4. 在“CameraFocus”脚本中添加以下代码: ```csharp public float movementSpeed = 10f; public float focusSpeed = 5f; private Camera mainCamera; void Start() { mainCamera = GetComponentInChildren<Camera>(); } void Update() { if (Input.GetMouseButton(0)) { Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { Vector3 targetPosition = hit.point; targetPosition.y = transform.position.y; Vector3 currentPosition = transform.position; Vector3 moveDirection = (targetPosition - currentPosition).normalized; transform.position += moveDirection * movementSpeed * Time.deltaTime; } } if (Input.GetMouseButton(1)) { Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { Vector3 targetPosition = hit.point; Quaternion targetRotation = Quaternion.LookRotation(targetPosition - transform.position); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, focusSpeed * Time.deltaTime); } } } ``` 5. 这段代码将使相机在鼠标左键点击时移动至点击位置,并使相机在鼠标右键点击时聚焦于点击位置。 注意:这段代码是基于鼠标输入的,如果你想使用触摸输入,你需要将代码相应地修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值