I have to scripts. One is The player’s Health and the other one is Enemy script for the enemy. So basicly the enemy follows the player and collides, then the health has to be subtracted.
Health :
var h00 : Texture2D;
var h10 : Texture2D;
var h20 : Texture2D;
var h30 : Texture2D;
var h40 : Texture2D;
var h50 : Texture2D;
var h60 : Texture2D;
var h70 : Texture2D;
var h80 : Texture2D;
var h90 : Texture2D;
var h100 : Texture2D;
static var HEALTH = 100;
function Update()
{
var g_Health = gameObject.Find("g_Health");
if(HEALTH > 90)
{
g_Health.guiTexture.texture = h100;
return;
}
else if (HEALTH > 80)
{
g_Health.guiTexture.texture = h90;
return;
}
else if (HEALTH > 70)
{
g_Health.guiTexture.texture = h80;
return;
}
else if (HEALTH > 60)
{
g_Health.guiTexture.texture = h70;
return;
}
else if (HEALTH > 50)
{
g_Health.guiTexture.texture = h60;
return;
}
else if (HEALTH > 40)
{
g_Health.guiTexture.texture = h50;
return;
}
else if (HEALTH > 30)
{
g_Health.guiTexture.texture = h40;
return;
}
else if (HEALTH > 20)
{
g_Health.guiTexture.texture = h30;
return;
}
else if (HEALTH > 10)
{
g_Health.guiTexture.texture = h20;
return;
}
else if (HEALTH > 5)
{
g_Health.guiTexture.texture = h10;
return;
}
else if (HEALTH <= 0)
{
g_Health.guiTexture.texture = h00;
Application.LoadLevel(1);
return;
}
}
Enemy :
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
renderer.material.color = Color.red;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
Anyone please help me with this, I want the enemy to subtract health from the player when the enemy collides with him.
- Felipe