Handling Immediate Loot Collection Upon Enemy Death

I have a problem with my current Unity setup. I want to spawn a loot prefab a few seconds after an enemy dies. However, the issue I’m facing is that the player character hits the enemy, and at the moment of the enemy’s death, the player immediately “collects” the loot because their attack intersects with the loot’s collider.

Here’s the current code for spawning the loot:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Invector.vCharacterController;
using Invector.vCharacterController.AI;

public class enemy_dead : MonoBehaviour
{
    public vControlAIMelee enemy;
    public bool Dead = false;
    public GameObject lootPrefab;
    public float destroyDelay;
    private bool lootSpawned = false;

    void Start()
    {

    }

    void FixedUpdate()
    {
        if (enemy.isDead == true && !Dead)
        {
            Dead = true;
        }

        if (Dead && !lootSpawned)
        {
            Instantiate(lootPrefab, transform.position, transform.rotation);
            lootSpawned = true;
        }

        if (Dead)
        {
            Destroy(gameObject, destroyDelay);
        }
    }
}

How can I ensure that the player does not immediately collect the loot upon the enemy’s death? What is the best way to handle the timing and interaction to avoid this issue?

Thank you for your help!

Most basic would be Invoke or coroutine

1 Like

You can throw the loot a few feet into the air and only allow the player to collect it once it has landed.

1 Like

Thank you to all who replied, I fixed it like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Invector.vCharacterController;
using Invector.vCharacterController.AI;

public class enemy_dead : MonoBehaviour
{
    public vControlAIMelee enemy;
    public GameObject lootPrefab;
    public float destroyDelay = 0.1f; // Задержка перед уничтожением объекта врага
    public float lootSpawnDelay = 2.0f; // Задержка перед активацией лута
    private bool lootSpawned = false;
    private bool Dead = false;

    void Start()
    {
        // При необходимости инициализация
    }

    void FixedUpdate()
    {
        if (enemy.isDead && !Dead)
        {
            Dead = true;
            StartCoroutine(HandleDeath());
        }
    }

    private IEnumerator HandleDeath()
    {
        // Дожидаемся окончания задержки перед активацией лута
        yield return new WaitForSeconds(lootSpawnDelay);

        if (!lootSpawned)
        {
            // Создаем префаб лута на месте врага
            GameObject loot = Instantiate(lootPrefab, transform.position, transform.rotation);

            // Активируем коллайдер лута после задержки
            BoxCollider lootCollider = loot.GetComponent<BoxCollider>();
            if (lootCollider != null)
            {
                lootCollider.enabled = true;
            }

            lootSpawned = true;
        }

        // Уничтожаем объект врага с задержкой
        Destroy(gameObject, destroyDelay);

    }
}