Unity三种摄像机旋转方式

1.按下鼠标右键可以实现摄像机上下左右旋转

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

public class CameraRotate : MonoBehaviour
{
    //旋转速度
    public float rotationSpeed = 5f;
    //上下旋转角度限制
    public float maxVerticalAngle = 90f;
    public float minVerticalAngle = -90f;
    //旋转缓冲速度
    public float lerpSpeed = 10f;
    private float targetRotationX = 0f;
    private float targetRotationY = 0f;
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            // 获取鼠标输入的旋转增量
            float rotationXInput = -Input.GetAxis("Mouse Y");
            float rotationYInput = Input.GetAxis("Mouse X");
            // 根据旋转速度进行摄像机的旋转
            targetRotationX += rotationXInput * rotationSpeed;
            targetRotationY += rotationYInput * rotationSpeed;
            // 对上下旋转角度进行限制
            targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);
            // 根据旋转角度更新摄像机的欧拉角,Quaternion.Lerp可以使摄像机旋转更加平滑
            Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);
        }
    }
}

2.按下鼠标右键可以实现摄像机围绕某个物体上下左右旋转

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

public class CameraRotate1 : MonoBehaviour
{
    public Transform target;
    public float rotationSpeed = 5f;
    public float maxVerticalAngle = 90f;
    public float minVerticalAngle = -90f;
    public float lerpSpeed = 200f;
    public float distance = 10;

    private float targetRotationX = 0f;
    private float targetRotationY = 0f;

    void Start()
    {
        if (target == null)
            Debug.LogError("Please assign a target to the orbit camera!");
    }
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            float rotationXInput = -Input.GetAxis("Mouse Y");
            float rotationYInput = Input.GetAxis("Mouse X");

            targetRotationX += rotationXInput * rotationSpeed;
            targetRotationY += rotationYInput * rotationSpeed;

            targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);

            Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);
        }
        transform.position = target.position - transform.forward * distance;
    }
}

3.摄像头始终跟随在某个物体的正后方

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

public class CameraRotate2 : MonoBehaviour
{
    public Transform target;
    public float followSpeed = 2f;
    public float followHeight = 4f;
    public float distance = 8f;

    private Vector3 velocity = Vector3.zero;
    void Start()
    {
        if (target == null)
            Debug.LogError("Please assign a target to the orbit camera!");
    }
    void LateUpdate()
    {
        Vector3 targetPosition = target.position - (target.forward * distance)+new Vector3(0,followHeight,0);
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, 1f / followSpeed);
        transform.LookAt(target);
    }
}
  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity摄像机旋转是通过修改摄像机旋转属性来实现的。在Unity中,摄像机旋转属性由三个欧拉角表示:pitch(俯仰角),yaw(偏航角)和roll(翻滚角)。 你可以通过以下步骤来实现摄像机旋转: 1. 获取摄像机组件:首先,你需要获取场景中的摄像机对象,并通过代码获取摄像机组件。 2. 修改摄像机旋转属性:根据你想要的旋转效果,修改摄像机的pitch、yaw和roll属性。例如,如果你想让摄像机绕Y轴旋转,可以修改yaw属性。 3. 应用旋转:将修改后的摄像机旋转属性应用到摄像机组件上。可以使用Transform组件提供的Rotate函数来实现旋转。 下面是一个简单的示例代码,演示了如何实现摄像机旋转: ```csharp using UnityEngine; public class CameraRotation : MonoBehaviour { public float rotationSpeed = 10f; void Update() { // 获取摄像机组件 Camera camera = GetComponent<Camera>(); // 按下鼠标左键进行旋转 if (Input.GetMouseButton(0)) { // 获取鼠标在屏幕上的移动距离 float mouseX = Input.GetAxis("Mouse X"); // 修改摄像机的yaw属性 Vector3 rotation = new Vector3(0f, mouseX * rotationSpeed, 0f); camera.transform.Rotate(rotation); } } } ``` 上述示例代码将允许你使用鼠标的水平移动来控制摄像机围绕Y轴旋转。你可以根据自己的需求修改旋转速度和旋转的方向。同时,你还可以探索其他旋转方法,例如绕X和Z轴旋转,或者使用Quaternion来实现更复杂的旋转效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒冷的晚风

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值