यूनिटी में कैमरे में हेड बॉबिंग इफ़ेक्ट जोड़ना

फर्स्ट-पर्सन शूटर गेम में हेड बॉबिंग प्रभाव का व्यापक रूप से उपयोग किया जाता है और यह खिलाड़ी के विसर्जन को बढ़ाने में महत्वपूर्ण भूमिका निभाता है।

इस ट्यूटोरियल में, मैं दिखाऊंगा कि Unity में हेड-बॉबिंग प्रभाव कैसे बनाया जाए।

चरण 1: प्लेयर कंट्रोलर सेट करें

सबसे पहले, हमें एक प्लेयर कंट्रोलर बनाना होगा:

  • एक नया गेम ऑब्जेक्ट बनाएं (गेम ऑब्जेक्ट -> खाली बनाएं) और इसे नाम दें "Player"
  • एक नया कैप्सूल बनाएं (गेम ऑब्जेक्ट -> 3डी ऑब्जेक्ट -> कैप्सूल) और इसे "Player" ऑब्जेक्ट के अंदर ले जाएं
  • कैप्सूल से कैप्सूल कोलाइडर घटक निकालें और उसकी स्थिति को (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 दबाएँ, प्लेयर के मूवमेंट पर कैमरा बॉबिंग सक्रिय होना चाहिए।

स्रोत
HeadBobbing.unitypackage5.09 KB
लिंक
Unity