- Home /
How to I make a explode on Raycast?
I am making a helicopter game in unity and I want it so that the ship will explode once it's health is down to zero. I have done some testing and my code doesn't work and the explosion prefab just keeps playing. Here is my code...
#pragma strict
var Health = 100;
var ExplosionPrefab : GameObject;
var ExplosionSound = "";
var ShipPrefab : GameObject;
var hit : RaycastHit;
var foundhit : boolean = false;
function Start ()
{
}
function Update ()
{
if(Health < 0);
{
Instantiate(ExplosionPrefab, transform.position, transform.rotation);
}
if(Physics.Raycast(transform.position, transform.forward))
{
Health = Health - 1;
}
}
If there is something I am missing please tell me, this is a class project and it is due and the end of the week. That would be great if I could get some help.
You'll need to add something to deal with what should happen when Health is below 0, right now it just keeps going and going, maybe something as simple as
if(Health = 0) (no semicolon)
GameObject explosion = Instantiate(...)
Destroy(explosion,2);
Destroy(helicopter,2);
that's pseudo code
I have the health thing figured out but I need some script to detect a raycast hit and deduct 1 health from the ship.
Answer by SeeSharp · Apr 23, 2014 at 03:54 AM
Hi,
To fully and correctly answer your question, I might need some more information about the explosion prefab(scripts attached, mostly). Add this to the 'Start()' method on your prefab's script: Destroy(gameObject, 5)
, and according to the Unity Documentation, this script should destroy any gameobject that it's attached to within 5 seconds of being activated.
Best of luck with your project and your class ,
SeeSharp.
Your answer
Follow this Question
Related Questions
Click to Change Public GameObject Variable 1 Answer
Search and Destroy 1 Answer
Adding Explosion Sound Effect 1 Answer
Explosion-Simple.js script of FPS Tutorial 1 Answer
Explosion using an animated sprite? 1 Answer