在 Unity 中为相机添加摇头效果

头部摆动效果广泛应用于第一人称射击游戏中,在提高玩家沉浸感方面发挥着关键作用。

在本教程中,我将展示如何在 Unity 中创建摇头效果。

第 1 步:设置播放器控制器

首先,我们需要创建一个玩家控制器:

  • 创建一个新的游戏对象(游戏对象 -> 创建空)并命名 "Player"
  • 创建一个新的胶囊(游戏对象 -> 3D 对象 -> 胶囊)并将其移动到 "Player" 对象内
  • 从 Capsule 中移除 Capsule Collider 组件,并将其位置更改为 (0, 1, 0)
  • 将主摄像机移动到 "Player" 对象内并将其位置更改为 (0, 1.64, 0)
  • 创建 一个新脚本,将其命名为 "SC_CharacterController" 并将以下代码粘贴到其中:

SC_CharacterController.cs

using UnityEngine;

[RequireComponent(typeof(CharacterController))]

public class SC_CharacterController : MonoBehaviour
{
    public float speed = 7.5f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Camera playerCamera;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 45.0f;

    CharacterController characterController;
    [HideInInspector]
    public Vector3 moveDirection = Vector3.zero;
    Vector2 rotation = Vector2.zero;

    [HideInInspector]
    public bool canMove = true;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        rotation.y = transform.eulerAngles.y;
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate move direction based on axes
            Vector3 forward = transform.TransformDirection(Vector3.forward);
            Vector3 right = transform.TransformDirection(Vector3.right);
            float curSpeedX = canMove ? speed * Input.GetAxis("Vertical") : 0;
            float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;
            moveDirection = (forward * curSpeedX) + (right * curSpeedY);

            if (Input.GetButton("Jump") && canMove)
            {
                moveDirection.y = jumpSpeed;
            }
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);

        // Player and Camera rotation
        if (canMove)
        {
            rotation.y += Input.GetAxis("Mouse X") * lookSpeed;
            rotation.x += -Input.GetAxis("Mouse Y") * lookSpeed;
            rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
            playerCamera.transform.localRotation = Quaternion.Euler(rotation.x, 0, 0);
            transform.eulerAngles = new Vector2(0, rotation.y);
        }
    }
}
  • SC_CharacterController 脚本附加到 "Player" 对象(您会注意到它还添加了另一个名为“字符控制器”的组件。将其中心值更改为 (0, 1, 0))
  • 将主摄像机分配给 SC_CharacterController 中的玩家摄像机变量

玩家控制器现已准备就绪:

第2步:添加摇头效果

摇头效果是在脚本的帮助下完成的,并通过在玩家移动时上下移动相机来工作。

  • 创建一个新脚本,将其命名为 SC_HeadBobber,并将以下代码粘贴到其中:

SC_HeadBobber.cs

using UnityEngine;

public class SC_HeadBobber : MonoBehaviour
{
    public float walkingBobbingSpeed = 14f;
    public float bobbingAmount = 0.05f;
    public SC_CharacterController controller;

    float defaultPosY = 0;
    float timer = 0;

    // Start is called before the first frame update
    void Start()
    {
        defaultPosY = transform.localPosition.y;
    }

    // Update is called once per frame
    void Update()
    {
        if(Mathf.Abs(controller.moveDirection.x) > 0.1f || Mathf.Abs(controller.moveDirection.z) > 0.1f)
        {
            //Player is moving
            timer += Time.deltaTime * walkingBobbingSpeed;
            transform.localPosition = new Vector3(transform.localPosition.x, defaultPosY + Mathf.Sin(timer) * bobbingAmount, transform.localPosition.z);
        }
        else
        {
            //Idle
            timer = 0;
            transform.localPosition = new Vector3(transform.localPosition.x, Mathf.Lerp(transform.localPosition.y, defaultPosY, Time.deltaTime * walkingBobbingSpeed), transform.localPosition.z);
        }
    }
}
  • 将 SC_HeadBobber 脚本附加到主摄像头
  • 将 SC_CharacterController 脚本分配给 "Controller" 变量

最后,按 Play 进行测试,相机摆动应该在玩家移动时激活。