CustomBullet | FEP

PHOTO EMBED

Wed May 04 2022 17:51:17 GMT+0000 (Coordinated Universal Time)

Saved by @BiscuitTinx #c#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomBullet : MonoBehaviour
{
    // assignables
    public Rigidbody rb;
    public GameObject explosion;
    public LayerMask whatIsEnemies;

    // stats
    [Range(0f, 1f)]
    public float bounciness;
    public bool useGravity;

    public bool playerBullet;

    // damage 
    public int explosionDamage;
    public float explosionRange;
    public float explosionForce;

    // lifetime
    public int maxCollisions;
    public float maxLifetime;
    public bool explodeOnTouch = true;

    int collisions;
    PhysicMaterial physics_mat;

    private void Start()
    {
        Setup();
        Destroy(gameObject, 1);
    }

    private void Update()
    {
        // when to explode 
        if (collisions > maxCollisions)
        {
            Explode();
        }

        // count down lifetime 
        maxLifetime -= Time.deltaTime;
        if (maxLifetime <= 0)
        {
            Explode();
        }
    }

    private void Explode()
    {
        // instantiate explosion
        if (explosion != null)
        {
            Instantiate(explosion, transform.position, Quaternion.identity);
        }

        // check for enemies 
        Collider[] enemies = Physics.OverlapSphere(transform.position, explosionRange, whatIsEnemies);
        for (int i = 0; i < enemies.Length; i++)
        {
            // get component of enemy and call Take Damage 

            // Example!
            // enemies[i].GetComponent<ShootingAi().TakeDamage(explosionDamage);

            // add explosion force (if enemy has a rigidbody)
            if (enemies[i].GetComponent<Rigidbody>())
            {
                enemies[i].GetComponent<Rigidbody>().AddExplosionForce(explosionForce, transform.position, explosionRange);
            }
        }

        // add a little delay, just to make sure everything works fine 
        Invoke("Delay", 0.05f);
    }

    private void Delay()
    {
        Destroy(gameObject);
    }

    private void OnCollisionEnter(Collision c)
    {
        // don't count collisions with other bullets
        if (c.collider.CompareTag("Bullet"))
        {
            return;
        }

        // count up collisions 
        collisions++;

        // explode if bullet hits an enemy directly and explodeOnTouch is activated
        if (c.collider.CompareTag("Enemy") && explodeOnTouch)
        {
            Explode();
        }
    }

    private void Setup()
    {
        // create a new physic materal 
        physics_mat = new PhysicMaterial();
        physics_mat.bounciness = bounciness;
        physics_mat.frictionCombine = PhysicMaterialCombine.Minimum;
        physics_mat.bounceCombine = PhysicMaterialCombine.Maximum;
        // assign material to collider
        GetComponent<SphereCollider>().material = physics_mat;

        // set gravity 
        rb.useGravity = useGravity;
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, explosionRange);
    }

    void OnTriggerEnter(Collider c)
    {
        c.GetComponent<Health>()?.TakeDamage(explosionDamage, playerBullet);

        //AI enemy = c.GetComponent<AI>();
        //if (enemy != null)
        //{
        //    enemy.TakeDamage(explosionDamage);
        //}

        Destroy(gameObject);
    }
}
content_copyCOPY