- Home /
Health subtracts when hit by bullit
Hey i'm currently trying to make a 2D platformer, and i've reached the stage where i want to make the enemies work out, so what i'm currently strangling with is to make a script that subtracts my 1 of my ¨hearths¨ ( I have made a GUI-texture and placed out some hearths, and i've also made a enemy that shoots towards my character)
Here's my healthcontroll script so far :
var health1 : Texture2D; //one life left
var health2 : Texture2D; //two life left
var health3 : Texture2D; //full heatlh
static var LIVES = 3;
function Update (){
switch(LIVES)
{
case 3:
guiTexture.texture = health3;
break;
case 2:
guiTexture.texture = health2;
break;
case 1:
guiTexture.texture = health1;
break;
case 0:
//game over script here
break;
}
}
So i know i'll have to make script that will subtract my ¨LIVES¨, does anyone have a example of such a script? would love to get this scripting done :)
Why have you shown us the part of the script that is working perfectly? It's kind of irrelevant. How do your bullets work? Do they follow a predeter$$anonymous$$ed path, or are they physics based?
Answer by Alaro · Mar 15, 2012 at 01:37 AM
The script was just to illustrate how far i've gotten, and the bullets are physic-based ¨spheres¨.
Answer by GutoThomas · Mar 15, 2012 at 02:04 AM
you can call a function in this script to detect any collision by doing:
function OnCollisionEnter(collider: Collision) {
LIVES--;
}
or if you want to tag your Sphere to detect just when it hits the player you can attach a tag called "Sphere" in the sphere and make an if statement inside OnCollisionEnter function to check the tag of the collider, like:
function OnCollisionEnter(collider: Collision) {
if(collider.transform.tag == "Sphere") {
LIVES--;
}
}
Your answer
Follow this Question
Related Questions
2D flash health and damage system 0 Answers
health regeneration after death 1 Answer
Enemy Health Bar UI 1 Answer
How Do I Make A Health Bar 6 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers