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.