Thêm hiệu ứng lắc đầu vào máy ảnh trong Unity

Hiệu ứng lắc lư đầu được sử dụng rộng rãi trong các trò chơi Bắn súng góc nhìn thứ nhất và đóng vai trò quan trọng trong việc tăng sự hòa nhập của người chơi.

Trong hướng dẫn này, tôi sẽ hướng dẫn cách tạo hiệu ứng lắc đầu trong Unity.

Bước 1: Thiết lập Trình điều khiển trình phát

Đầu tiên, chúng ta cần tạo bộ điều khiển trình phát:

  • Tạo một Game Object mới (Game Object -> Create Empty) và đặt tên cho nó "Player"
  • Tạo một Capsule mới (Đối tượng trò chơi -> Đối tượng 3D -> Capsule) và di chuyển nó vào trong Đối tượng "Player"
  • Loại bỏ thành phần Capsule Collider khỏi Capsule và thay đổi vị trí của nó thành (0, 1, 0)
  • Di chuyển Camera chính bên trong Đối tượng "Player" và thay đổi vị trí của nó thành (0, 1.64, 0)
  • Tạo một tập lệnh mới, đặt tên là "SC_CharacterController" và dán mã bên dưới vào trong tập lệnh đó:

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);
        }
    }
}
  • Đính kèm tập lệnh SC_CharacterController vào Đối tượng "Player" (Bạn sẽ nhận thấy rằng nó cũng đã thêm một thành phần khác gọi là Bộ điều khiển ký tự. Thay đổi giá trị trung tâm của nó thành (0, 1, 0))
  • Gán Camera chính cho biến Player Camera trong SC_CharacterController

Bộ điều khiển Player hiện đã sẵn sàng:

Bước 2: Thêm hiệu ứng lắc lư đầu

Hiệu ứng lắc lư đầu được thực hiện với sự trợ giúp của tập lệnh và hoạt động bằng cách di chuyển Camera lên xuống khi người chơi di chuyển.

  • Tạo một tập lệnh mới, đặt tên là SC_HeadBobber và dán mã bên dưới vào trong tập lệnh đó:

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);
        }
    }
}
  • Đính kèm tập lệnh SC_HeadBobber vào Camera chính
  • Gán tập lệnh SC_CharacterController cho biến "Controller"

Cuối cùng, nhấn Play để kiểm tra, Camera nhấp nháy sẽ được kích hoạt khi người chơi chuyển động.

Nguồn
📁HeadBobbing.unitypackage5.09 KB
Bài viết được đề xuất
Thêm Cúi người vào Trình phát FPS trong Unity
Hướng dẫn sử dụng đèn pin cho Unity
Unity Cách tạo điều khiển cảm ứng trên thiết bị di động
Bộ điều khiển ô tô cho Unity
Bộ điều khiển máy bay cho Unity
Hệ thống đối thoại vì sự thống nhất
Cần điều khiển nhập cảm ứng di động trong Unity