Home > other >  Why my bomb impact radius and explosion effect instantiate in diffrent possition than it should?
Why my bomb impact radius and explosion effect instantiate in diffrent possition than it should?

Time:10-02

Im working on bomb system in Unity 3D. I have uploaded some explosion effects from Unity Asset Store and I want implement them into my project. I want to plant bomb with key "K" then wait 3sec to detonate it with explosion effect and give some damage to nearby objects. The problem is that explosion appears in such different position as it should. In my opinion this is Editor problem , the code looks fine. I will give you some screenshoots(https://drive.google.com/file/d/19Yzymch9RdTa-E6RkbvvyfzMWjMJHo52/view?usp=sharing)and my bomb script :

public class BoombScript : MonoBehaviour
{
    public GameObject boombEffect;

[SerializeField]
private float radius;
[SerializeField]
private float force;
[SerializeField]
private int explosiveDamage;

public void Explode()
{
    Instantiate(boombEffect, transform.position, Quaternion.identity);

    Debug.Log("Transform"   transform);
    Debug.Log("Position"   transform.position);

    Collider[] colliders = Physics.OverlapSphere(transform.position, radius);

    foreach(Collider rangedObject in colliders)
    {
        GateScript Gate = rangedObject.GetComponent<GateScript>();
        Rigidbody rb = rangedObject.GetComponent<Rigidbody>();

        if(Gate != null)
        {
            Gate.GateDestroy(explosiveDamage);
        }

        if(rb != null)
        {
            rb.AddExplosionForce(force, transform.position, radius);
        }
    }
}

public IEnumerator WaitForExplode()
{
    yield return new WaitForSeconds(3f);
    Explode();
    
}

}

CodePudding user response:

In my opinion this is Editor problem , the code looks fine.

In general I would always doubt such a statement.

I can only guess but if I understand you correctly you rather want to use the position of your object the moment when you start the Coroutine, not the one after 3 seconds:

// instead of using the current "reansform.position" rather
// use the pre-stored position passed in via parameter
public void Explode(Vector3 position)
{
    Instantiate(boombEffect, position, Quaternion.identity);

    Debug.Log("Transform"   transform);
    Debug.Log("Position"   position);

    Collider[] colliders = Physics.OverlapSphere(position, radius);

    foreach(Collider rangedObject in colliders)
    {
        GateScript Gate = rangedObject.GetComponent<GateScript>();
        Rigidbody rb = rangedObject.GetComponent<Rigidbody>();

        if(Gate != null)
        {
            Gate.GateDestroy(explosiveDamage);
        }

        if(rb != null)
        {
            rb.AddExplosionForce(force, position, radius);
        }
    }
}

// If really needed you could still also have an overload 
// e.g. if at some places you actually call this method from the outside 
// immediately
public void Explode()
{
    Explode(transform.position);
}

public IEnumerator WaitForExplode()
{
    // Store the position when the routine is started
    var position = transform.position;

    yield return new WaitForSeconds(3f);

    // Instead of using the current position rather pass in
    // the previously stored one of when the routine was started
    Explode(position);
}
  • Related