Ok… so making a game with grenades and ragdolls. when the grenade explodes, the ragdoll is not instantiated in the right position and the ragdoll multiplies. Any advice?
using System.Collections;
using UnityEngine;
public class Grenade : MonoBehaviour
{
public float delay = 5f;
public float radius = 5f;
public float force = 1000f;
public float damage = 50000f;
public float grenadeAmount = 3f;
public GameObject explosionEffect;
public AudioSource esfx;
float countdown;
bool hasExploded = false;
// Start is called before the first frame update
void Start()
{
countdown = delay;
}
// Update is called once per frame
void Update()
{
countdown -= Time.deltaTime;
if (countdown <= 0f && !hasExploded)
{
Explode();
hasExploded = true;
}
}
void Explode()
{
esfx.Play();
GameObject EFX = Instantiate(explosionEffect, transform.position, transform.rotation);
Destroy(EFX, 3f);
Collider[] collidersToDestroy = Physics.OverlapSphere(transform.position, radius);
foreach (Collider nearbyObject in collidersToDestroy)
{
Target dest = nearbyObject.GetComponent<Target>();
if (dest != null)
{
dest.Die();
}
foreach (Collider nearbyObject1 in collidersToDestroy)
{
Ragdoll dest1 = nearbyObject1.GetComponent<Ragdoll>();
if (dest1 != null)
{
dest1.Die();
}
}
Collider[] collidersToMove = Physics.OverlapSphere(transform.position, radius);
foreach (Collider nearbyObject1 in collidersToMove)
{
Rigidbody rb = nearbyObject1.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(force, transform.position, radius);
EndPart();
StartCoroutine(EndPart());
}
}
IEnumerator EndPart()
{
yield return new WaitForSeconds(1.5f);
Destroy(gameObject);
}
}
}
}