- Home /
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.
If you want a nice easy way to do explosions use the Detonator Explosions pack from the asset store, its free and you just apply one of the prefabs and call explode on it from OnCollisionEnter. When your importing it DON'T fix the materials or textures when unity asks you or the explosions look messed up
Answer by RedSandStudios · Feb 19, 2014 at 04:33 PM
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));
Okay I'll try that in a bit once I figure out another problem I'm having right now edit: hmm it seems to be working most of the times, thanks!
$$anonymous$$y apologies, it's just it seemed very similar to a problem that I was having when I was implementing rockets in my game, and this was how I solved it. What do you mean by it fails quite a lot? Does it not collide/not put the sphere prefab in the right position?
it still spawns after the wall a little bit, causing same problem as the trigger, I updated my question, and it seemed to work better, anyway I just gave up on rockets since this is for a simple networking project, I just wanted to learn networking better, so this problem isn't that bad at this time for me
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
OnCollisionEnter problem (RocketJump script) 1 Answer
Multiple Cars not working 1 Answer
Rocket Explosion not applying damage when hitting ground 1 Answer
Cast Source Instantiating Error 1 Answer