Oyun Kütüphanesi

Zıplama Sistemi (Space Tuşu)

Fizik & Zıplama Unity C# 2
ZıplamaSistemiSpaceTuşu .cs
C#
using UnityEngine;

public class PlayerJump : MonoBehaviour
{
    [Header("Zıplama Ayarları")]
    public float jumpHeight = 1.5f;
    public float gravity = -9.81f;

    private CharacterController controller;
    private Vector3 velocity;
    private bool isGrounded;

    // Yer kontrolü için zemin katmanı (Inspector'dan ayarla)
    public LayerMask groundMask;
    public Transform groundCheck;
    public float groundDistance = 0.4f;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        // Yere temas mı?
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
            velocity.y = -2f;

        // Space ile zıpla
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            // v = sqrt(h * -2 * g) formülü
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        // Yerçekimi
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}

Açıklama

Yere temas kontrolü ile Space tuşuna basıldığında zıplayan, çift zıplamayı engelleyen güvenli zıplama scripti.

Etiketler

Zıplama Space Jump CharacterController

Nasıl Kullanılır?

1. Oyuncunun ayakları hizasına boş bir 'GroundCheck' GameObject oluştur.

2. 'Ground' adında bir Layer oluştur ve zemin nesnelerine ata.

3. Inspector'da 'Ground Check' ve 'Ground Mask' alanlarını doldur.

4. Space tuşuna basınca zıplar, havadayken Space çalışmaz.

Unity 2022+ ve Unity 6 ile uyumludur.

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