Oyun Kütüphanesi

Çömelme Sistemi (Crouch - Ctrl)

Etkileşim & Input Unity C# 2
ÇömelmeSistemiCrouch-Ctrl .cs
C#
using UnityEngine;
public class CrouchSystem : MonoBehaviour
{
    public float standHeight = 2f;
    public float crouchHeight = 1f;
    public float crouchSpeed = 5f;
    public Transform cameraTransform;
    public float standCamY = 1.6f;
    public float crouchCamY = 0.9f;

    private CharacterController cc;
    private bool isCrouching;

    void Start()
    {
        cc = GetComponent<CharacterController>();
        cc.height = standHeight;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftControl))
            isCrouching = !isCrouching;

        float targetH = isCrouching ? crouchHeight : standHeight;
        float targetCamY = isCrouching ? crouchCamY : standCamY;

        cc.height = Mathf.Lerp(cc.height, targetH, Time.deltaTime * crouchSpeed);
        Vector3 camPos = cameraTransform.localPosition;
        camPos.y = Mathf.Lerp(camPos.y, targetCamY, Time.deltaTime * crouchSpeed);
        cameraTransform.localPosition = camPos;
    }
}

Açıklama

Sol Ctrl ile karakterin çömelip ayağa kalktığı, CharacterController yüksekliğini dinamik olarak değiştiren sistem.

Etiketler

Crouch Çömelme CharacterController Ctrl

Nasıl Kullanılır?

1. CharacterController'lı Player'a ekle.

2. cameraTransform = Player'ın çocuğu olan kamera.

3. Sol Ctrl'ye basınca çömelir, tekrar basınca kalkar.

4. Kamera yumuşakça alçalır/yükselir.

5. Çömelme hızı için crouchSpeed'i artır/azalt.

Unity 2022+ ve Unity 6 ile uyumludur.

MonoBehaviour tabanlı scriptleri Assets klasörüne .cs olarak kaydedin.