Rocket Launcher explosion

Hi, for my current project I need to create a rocket launcher like explosion, the obective is that when it hits, it would make the explosion, but I’m having some problems since it doesn’t react as fast as it should.

using System.Collections;

public class RocketScript : MonoBehaviour {

	public GameObject explosionPrefab;
	public float velocity = 7f;
	public float damage = 35f;
	public float radius = 3f;

	IEnumerator Start () {
		yield return new WaitForSeconds (15f);
		Destroy(gameObject);
	}

	void FixedUpdate () {
		rigidbody.velocity = transform.forward * velocity;
	}

	void OnTriggerEnter () {
		Explode();
	}

	void Explode () {
		Instantiate(explosionPrefab, transform.position, Quaternion.identity);
		Collider[] hitColliders = Physics.OverlapSphere(transform.position ,radius);
		foreach (Collider hitCollider in hitColliders) {
			if (hitCollider.tag == "Player") {
				if (!Physics.Linecast(transform.position, hitCollider.transform.position)) {
					hitCollider.SendMessageUpwards("ApplyRocketDamage", damage, SendMessageOptions.DontRequireReceiver);
				}
			}
		}
		
		gameObject.renderer.enabled = false;
	}
}

That is my curent script, the explosionPrefab is just a sphere (for testing purposes) with the scale of 3,3,3 what I notice is that the sphere is instantiated more to the wrong side of the wall than to the hit point, for example if it hits a thin wall, the sphere would go more to the other side of the wall than to the side of where the explosion should happen, so this makes that " if (!Physics.Linecast(transform.position, hitCollider.transform.position))" never happens, because the Linecast starts from the other side of the wall, intercepting the LineCast. What can I do to fix this?

update: Since the collision detection seemed to be somewhat difficult to get it right, I decided to completily remove the collider, and instead use a raycast to detect “collision”, my fist try, with an usual raycast was working better than usual, since it was spawning where I wanted, however, sometimes the rycast didn’t detect the wall and it would go straight through it, now I tried a spherecast, seems to be handling it pretty well. I hope it works

Thanks in advance.

The problem is that you’re using transform.position as the Instantiate point for your prefab and for the Linecast:

Instantiate(explosionPrefab, transform.position, Quaternion.identity);

and

!Physics.Linecast(transform.position, hitCollider.transform.position)

When these are called, the transform.position of your Rocket may be through the wall already, which is why your prefab gets put on the wrong side of the wall, and why the Linecast starts on the other side of the wall.

The best way to fix this is to turn off IsTrigger on your Collider component use OnCollisionEnter instead of OnTriggerEnter. The benefit of this is that a Collision object is passed into OnCollisionEnter, which holds information about the collision, including the hit point, which is what we want in this case. So just replace OnTriggerEnter() and Explode() with:

void OnCollisionEnter (Collision info) {
   //info.contacts[0].point is a Vector3 of the first collision point.
   Instantiate(explosionPrefab, info.contacts[0].point, Quaternion.identity);
   Collider[] hitColliders = Physics.OverlapSphere(info.contacts[0].point, radius);
   foreach (Collider hitCollider in hitColliders) {
	 if (hitCollider.tag == "Player") {
	  if (!Physics.Linecast(info.contacts[0].point, hitCollider.transform.position)) {
		  hitCollider.SendMessageUpwards("ApplyRocketDamage", damage, SendMessageOptions.DontRequireReceiver);
	  }
	 }
   }

   gameObject.renderer.enabled = false;
}

And also don’t forget to uncheck the isTrigger tickbox on your Rocket collider.
For when you change the collision prefab from a sphere to something else, you can also use the normal of the contact point to make it face the right direction, otherwise it might just face straight up and look weird:

Instantiate(explosionPrefab, info.contacts[0].point, Quaternion.Euler(info.contacts[0].normal));