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!