- Home /
Creating a Asteroid health script
I know this is very simple script, but considering my progress, I only downloaded unity A week ago, and with that began learning javascript
any way. what i want this to do is if the object this script is attached to is hit by an object with the tag "PlayerGun" Health is decreased. and if the health hits 0, or below 0, then the object the script is attached to is destroyed.
Also when the object is destroyed, it spawns 1 of the 5 transform variables I have below at random where the object the script is attatched to was, and If it happens to be babyAsteroid, then it spawns 3 of them. The babyAsteroid variable should have the likability of being spawned 50%. I put in what my code is below, but it doesnt work, and some parts I dont even know where to begin.
//asteroid script =3
var health = 10.0;
var babyAsteroid:Transform;
var Money:Transform;
var addammo:Transform;
var shield:Transform;
var random:Transform;
function OnCollisionEnter( hit : Collider ){
if(hit.gameObject.tag == "PlayerGun") {
health -=1;
}
}
function Awake(){
if(health < 0){
Destroy(transform)
//then spawn one of the variables that are transforms. or if its babyAsteroid. 3 of them.
}
}
Hi EliDavis, when you post your code it looks much more clear if you mark it as code ;) it is the button with 0s and 1s
I'm getting an error message
Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.
So what should I change up? function OnCollisionEnter( hit : Collider ){ // Debug.Log("OnCollisionEnter:"); if(hit.gameObject.tag == "PlayerGun") { Debug.Log("Hit"); health --; } }
I'm getting an error message
Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.
So what should I change up? function OnCollisionEnter( hit : Collider ){ // Debug.Log("OnCollisionEnter:"); if(hit.gameObject.tag == "PlayerGun") { Debug.Log("Hit"); health --; } }
Answer by Luke Briggs · May 21, 2011 at 11:50 PM
I think this is what your looking for :)
var onDieObjects:GameObject[]=new GameObject[5];
var health:float=10f;
function reduceHealth(by:float){ health-=by; //hasn't always got to be 1 this way!
if(health<=0){
Destroy(gameObject);//destroy the object this scripts stuck to.
var numberOfObjects=onDieObjects.length;
var rand:int=Random.Range(0,numberOfObjects-1);//choose a random object
if(rand==0){//baby rock! make it 3 times by doing a lil loop.
//because this uses the same position/rotation theyll end up ontop of each other.
for(var i=0;i<3;i++){
Instantiate(onDieObjects[0],transform.position,transform.rotation);
}
}else{
Instantiate(onDieObjects[rand],transform.position,transform.rotation);
}
}
}
function OnCollisionEnter( hit:Collision ){
if(hit.gameObject.tag == "PlayerGun") {
reduceHealth(1); //hits a 1.
}
}
The above has been tested out and should do what your looking for. In the inspector you'll see an on die objects option with a drop down. Click that and then select the objects you'd like to generate, with the baby rock one in first :)
Hope that helps!
Answer by GesterX · May 21, 2011 at 06:51 PM
Your code is fine except you should do the health checking part in the Update function not the Awake function. The awake function is called just once - when the object is created. The Update function is called every frame. So, because you want to always be checking the health of the gameobject use the Update function. As for your spawning a baby asteroid (nice idea) you could do something along these lines:
function Update(){
//if health is less than or equals 0
if(health <= 0){
//Generate a random number of 1 or 2
var rand = Random.Range(1,3);
if (rand == 1)
{
//spawn a baby asteroid
Instantiate(babyAsteroid, transform.position, transform.rotation);
}
//destroy the gamObject
Destroy(gameObject)
}
}
That should get you started.
Posted whilst I was typing. Grrrr. Anyway, just combine the two.
Answer by Muzz5 · May 21, 2011 at 06:54 PM
//asteroid script =3
var health = 10.0;
var babyAsteroid:Transform;
var Money:Transform;
var addammo:Transform;
var shield:Transform;
var random:Transform;
function OnCollisionEnter( hit : Collider ){
if(hit.gameObject.tag == "PlayerGun") {
health --;
// -- is shorthand for -1. ++ is for +1.
}
}
function Update(){
// update detects every frame
if(health < 0){
Destroy(GameObject) ;
// need to add semi-colons at the end of each line (unless it has a '{')
Instantiate (Money, transform.position, transform.rotation);
// Use a similar thing to make the others.
}
}
I haven't tested this, so there may be a few small errors. Try and sort them from the compiler.
You need to move the Destroy call to the end. Else the GO will be destroyed and the Instantiate won't be called.
Answer by Luke Briggs · May 21, 2011 at 11:55 PM
P.s. The wierd code formatting of my last message isn't my fault, plus it won't let me edit it again without erroring :P
Answer by Luke Briggs · May 22, 2011 at 09:56 AM
If you'd like any explanation of how that's working please just let me know :)