【Unity】记录鼠标控制角色视角转动

12 篇文章 0 订阅
本文介绍了一个Unity教程中的FPS视角控制实现。通过调整摄像机层级,实现两个摄像机分别渲染武器和场景。使用鼠标X轴偏移控制角色水平旋转,Y轴偏移控制视角垂直旋转,同时限制旋转角度避免万向节死锁问题。代码中展示了`PlayerMovementController`类的实现,通过`FixedUpdate`和`PlayerRotateControl`方法处理旋转逻辑。
摘要由CSDN通过智能技术生成

前言

  • 这里使用的资源来自unity learn中的FPS教程demo
  • 下方是已经调整好的层级关系
  • 在Eye_view中可以看到有两个摄像机,一个负责只渲染武器(方便后期瞄准时,移动枪支后,弹道依旧准确),另一个相机负责渲染除了武器,以外的其他…
  • 本篇文章记录的便是通过鼠标移动eye_view这个对象的旋转
    在这里插入图片描述

准备工作

  • 鼠标移动需要鼠标的X,Y轴偏移值
  • 需要一个float作为鼠标灵敏度
  • 左右旋转的时候,需要控制角色跟着旋转
  • 上下旋转的时候,只需要控制视角旋转即可,角色物体不需要旋转

注意一下

  • 旋转角度需要限制一下,不然旋转超过90度会出现欧拉角万向节死锁的问题
public class PlayerMovementController : MonoBehaviour
{
    public float rotateSpeed = 180;
    [Range(1, 2)] public float rotateRatio = 1;
    public Transform playerTransform;
    public Transform eyeViewTransform;
    public float MaxViewAngle = 65f;
    private float tmp_viweRotationOffset;

    private void FixedUpdate()
    {
        PlayerRotateControl();
    }

    private void PlayerRotateControl()
    {
        if (playerTransform == null || eyeViewTransform == null)
        {
            return;
        }

        //x控制的是水平方向旋转,也就是控制沿Y轴旋转,控制Player的水平方向转动
        float offset_x = Input.GetAxis("Mouse X");
        //控制垂直方向旋转,也就是沿x轴旋转,这里只控制EYE_VIEW照相机在垂直方向的转动
        float offset_y = Input.GetAxis("Mouse Y");
        playerTransform.Rotate(Vector3.up * (offset_x * rotateSpeed * rotateRatio * Time.fixedDeltaTime));
        //对旋转角进行累加才能够进行对最大角度的限制
        tmp_viweRotationOffset -= offset_y * rotateSpeed * rotateRatio * Time.fixedDeltaTime;
        //限制一下旋转的角度
        tmp_viweRotationOffset = Mathf.Clamp(tmp_viweRotationOffset, -MaxViewAngle, MaxViewAngle);
        Quaternion EyeLocalQuaternion = Quaternion.Euler(new Vector3(tmp_viweRotationOffset,
            eyeViewTransform.localEulerAngles.y,
            eyeViewTransform.localEulerAngles.z));
        //注意旋转角是四元素,需要用四元素处理
        eyeViewTransform.localRotation = EyeLocalQuaternion;
    }
}
  • 最后再给组件赋好值即可
    在这里插入图片描述
要实现Unity视角跟随鼠标转动,可以按照以下步骤进行: 1. 在Unity中创建一个摄像机,并将其放置在场景中。 2. 创建一个空物体,将其命名为“Player”,并将其放置在场景中。 3. 将摄像机作为“Player”的子对象,并将其位置设置为相对于“Player”对象的偏移量(例如,将其放置在“Player”对象的后方一定距离的位置)。 4. 编写脚本来控制摄像机的旋转。在脚本中,使用Input.GetAxis函数获取鼠标移动的偏移量,并将其应用于摄像机的旋转中。 以下是一个示例脚本: ```C# public class CameraController : MonoBehaviour { public Transform player; public float sensitivity = 5.0f; public float smoothing = 2.0f; private Vector2 smoothedVelocity; private Vector2 currentLookingPos; void Start() { Cursor.lockState = CursorLockMode.Locked; } void Update() { Vector2 inputValues = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")); inputValues = Vector2.Scale(inputValues, new Vector2(sensitivity * smoothing, sensitivity * smoothing)); smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputValues.x, 1f / smoothing); smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputValues.y, 1f / smoothing); currentLookingPos += smoothedVelocity; transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y, Vector3.right); player.localRotation = Quaternion.AngleAxis(currentLookingPos.x, player.up); } } ``` 在这个脚本中,我们首先定义了一个“Player”对象,以及一些控制摄像机旋转的参数。在Start函数中,我们锁定了鼠标,这样它就不会离开游戏窗口。在Update函数中,我们使用Input.GetAxis函数获取鼠标移动的偏移量,并将其平滑处理,然后将其应用于摄像机的旋转中。最后,我们使用Quaternion.AngleAxis函数来将旋转转换为四元数,并将其应用于摄像机和“Player”对象中。 如果一切正常,你现在应该能够在Unity中看到摄像机跟随鼠标转动了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值