Oyun Kütüphanesi

Rigidbody ile Mermi (Bullet) Sistemi

Fizik & Zıplama Unity C# 2
RigidbodyileMermiBulletSistemi .cs
C#
using UnityEngine;
public class Gun : MonoBehaviour
{
    public GameObject bulletPrefab;
    public Transform muzzle;
    public float bulletSpeed = 30f;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
            Shoot();
    }

    void Shoot()
    {
        GameObject b = Instantiate(bulletPrefab, muzzle.position, muzzle.rotation);
        Rigidbody rb = b.GetComponent<Rigidbody>();
        rb.AddForce(muzzle.forward * bulletSpeed, ForceMode.VelocityChange);
        Destroy(b, 3f); // 3 saniye sonra sil
    }
}

Açıklama

Silahtan Instantiate edilen mermi prefab'ına AddForce uygulayan temel silah mekanizması.

Etiketler

Rigidbody Mermi Silah Instantiate

Nasıl Kullanılır?

1. Mermi için küçük bir Sphere prefab yap, Rigidbody ekle.

2. Silah objesine bu scripti ekle.

3. Muzzle = namlunun ucu (boş bir Transform).

4. Bullet Prefab alanını Inspector'dan ata.

5. Sol tık ile ateş eder.

Unity 2022+ ve Unity 6 ile uyumludur.

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