- Home /
Sidescroller health bar collision script with regen?
I am at University in Leeds making a side scroller game for kids. Here is my health script i know it works but i need to make it take off health on a collision with my barrels in my scene. The barrels have a mesh colliders and the character has a character controller attached as well as other scripts for other things. My health bar is updated in the GUI when i test it but i can't get the collision to work. Also how to regenerate the health once my re spawn script takes him back to the start. Many thanks for having a look at this!
var h00 : Texture2D; var h25 : Texture2D; var h50 : Texture2D; var h75 : Texture2D; var h100 : Texture2D;
static var HEALTH = 100;
function Update() { var g_Health = gameObject.Find("g_Health");
if(HEALTH > 75)
{
g_Health.guiTexture.texture = h100;
return;
}
else if (HEALTH > 50)
{
g_Health.guiTexture.texture = h75;
return;
}
else if (HEALTH > 25)
{
g_Health.guiTexture.texture = h50;
return;
}
else if (HEALTH > 10)
{
g_Health.guiTexture.texture = h25;
return;
}
else if (HEALTH <= 0)
{
g_Health.guiTexture.texture = h00;
return;
}
}
Have you tested the collision? does it work? do you have an error? what does your healthbar when it when HEALTH = 5? When you take him back to start simply set HEALTH = 100. if you want to give him live again
I don't know how to add the collision to this script and to script the take health on collision. So i haven't tested it only tested with print in the console window to see if the script works but don;t know how to implement it in the script. thanks for your time
var h00 : Texture2D; var h25 : Texture2D; var h50 : Texture2D; var h75 : Texture2D; var h100 : Texture2D;
static var HEALTH = 100;
function Update() { var g_Health = gameObject.Find("g_Health");
if(HEALTH > 75)
{
g_Health.guiTexture.texture = h100;
return;
}
else if (HEALTH > 50)
{
g_Health.guiTexture.texture = h75;
return;
}
else if (HEALTH > 25)
{
g_Health.guiTexture.texture = h50;
return;
}
else if (HEALTH > 10)
{
g_Health.guiTexture.texture = h25;
return;
}
else if (HEALTH <= 0)
{
g_Health.guiTexture.texture = h00;
return;
}
}
function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.collider.gameObject.tag == "barrel") { take health??? } }
Collision added like this? how would i add the take damage scripting into the colission?
Answer by Techsuport · Aug 19, 2011 at 02:48 AM
//what i did was draw the bar with
var maxHealth:float;
var health:float;
var healthRegen:float;
var pic:Texture2D;//fills the bar
var barRect:Rect=new Rect(0,0,.1,.5);//height and width are multuplyed be screen space
//remember not to leave things at 0
function OnGUI(){
GUI.Label(Rect(barRect.x,barRect.y,barRect.width*screen.width,barRect.hieght*screen.height),pic);//
}
function FixedUpdate(){
heath+=healthRegen*Time.deltaTime;//makes health regenerate at healthRegen per-second ;)
}, //what i did was draw the bar with
var maxHealth:float;
var health:float;
var healthRegen:float;
var pic:Texture2D;//fills the bar
var barRect:Rect=new Rect(0,0,.1,.5);//height and width are multuplyed be screen space
//remember not to leave things at 0
function OnGUI(){
GUI.Label(Rect(barRect.x,barRect.y,barRect.width*screen.width,barRect.hieght*screen.height),pic);//
}
function FixedUpdate(){
heath+=healthRegen*Time.deltaTime;//makes health regenerate at healthRegen per-second ;)
}