Have projectile explode at mouse position?

Hey everyone. I’m making a top down shooter. I have a fireball projectile that starts as a small projectile, but when it hits anything that isn’t another projectile, it explodes. The problem with this, though, is that I want the projectile to explode at the Mouse Position when it is first fired, as long as no other colliders block it from reaching that point. I can’t seem to get it working right without instantiating another trigger at the mouse position, but if I do this, then different small projectiles could interact with each others destination triggers.

I have tried making the small projectile explode when it’s transform.position is the same as the instantiated Destination game object’s but it will never work because the position can have really specific floating point values that will never match up across frames.

Any ideas as to how I can make this work? Here’s my relevant code:

    public GameObject spellDestination;

    Vector3 screenMousePos;
    Vector3 worldMousePos;    

void Start()
    {
        screenMousePos = Input.mousePosition; //this is the position of the mouse on the screen, measured in resolution pixels
        worldMousePos = Camera.main.ScreenToWorldPoint(screenMousePos); //this converts the position of the mouse on the screen to an actual world transform position
        worldMousePos.z = 0;
        Instantiate(spellDestination, worldMousePos, Quaternion.identity); 

        Invoke("Explode", weakSpellLifeTime); //destroy the projectile after a certain amount of time
    }


void FixedUpdate()
    {
        transform.Translate(0, Time.deltaTime * weakSpellSpeed, 0); //constantly move it forward at a fixed speed

        if (transform.position == spellDestination.transform.position)
        {
            isAlreadyExploding = true;
            Explode();
        }
    }

Ok, so it looks like you are getting the mouse position when the projectile is instantiated, but depending on how far away your camera is from where you want the world mouse position to be and ultimately where you want the projectile to explode if no collisions happen, the position might not be precise because you are not passing the z parameter (world units from the camera) to the ScreenToWorldPoint function. You apply the z position to the returned vector after the function has done it’s job. You should get the distance the camera is from the plane which the projectile explodes, if you know that the X and Y plane is always located at 0 on the z and you know the camera is always at -10 on the z then pass the function 10 on the z:

worldMousePos = Camera.main.ScreenToWorldPoint(screenMousePos.x, screenMousePos.y, 10);

If the distance from the camera is always changing, get the distance you should get the distance like so:

float distance = Mathf.Abs(planeZPos - camera.transform.position.z);

and then pass distance as the final parameter to the function. Now get rid of instantiating the spellDestination trigger and just use lerp for example:

Vector3 startPos;
float timeCount = 0.0f;

void Start(){

startPos = transform.position;
}

void Update(){

transform.position = Vector3.Lerp(startPos, worldMousePos, timeCount);
timeCount = timeCount + Time.deltaTime;

if(timeCount >= 1.0f){
Explode();
}

}