Taking a hit

I am trying to get my enemy to reduce the amount of health the player has when it comes within a certain distance. I have wrote this script that tells me how far away I am from the enemy, and if I get too close the console says “enemy attack”. The script also gives me a little GUI of the health that I am wanting to change also when the enemy is too close to the player. I have got so far but Im not sure how to reduce the amount of health the player has at line 12, can anyone give me some pointers?

This is what I have so far but It is not working can you help me out?

var fullHealth : int = 100;
var curHealth : int = 100;

function OnCollisionEnter(collision: Collision) {

if(collision.gameObject.tag == "Enemy");
  fullHealth -= 10;
  print ("hit");
}

function Update () {

if(curHealth >= fullHealth){
    curHealth = fullHealth;
}

  if(fullHealth <= 0){
    fullHealth = 0;
    Debug.Log("You are Dead");
}
}

function OnGUI() {
    GUI.Label (Rect (25, 40, 100, 20), "Health = "+fullHealth);
}

Weird, youh ave most of it except the easy part:

if(dist < 1.6){
    curHealth -= damage;
}

Try this, Not tested.

Make a square colour texture about 64 x 64 a drag it into the healthTexture in the inspector.

#pragma strict

var healthTexture : Texture2D;			// Texture to use for the health graphics
var curHealth : int = 100;
var enemy : Transform;
private var adjust : int;
private var gapTest = true;

function Update () {

  if(enemy){
  var dist = Vector3.Distance(enemy.position, transform.position);
    print ("Distance to enemy: " + dist);
}

  if(gapTest && dist < 1.6){
  print ("enemy attack");
 curHealth = curHealth - 1;
 GapSetHealth();
}



  if(curHealth >= fullHealth){
    curHealth = fullHealth;
}

  if(curHealth <= 0){
    curHealth = 0;
    Debug.Log("You are Dead");
  }
}

function OnGUI () {

adjust = curHealth * 3;

// Place the GUI at the top of the screen
GUI.BeginGroup(Rect(Screen.width / 2 - 150,Screen.height - Screen.height + 10,adjust,15));
GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
GUI.EndGroup();
}

function GapSetHealth(){

    gapTest = false;
    yield WaitForSeconds (0.02); // Gap size for the time to deduct health
    gapTest = true;
}

original post

You need to decide how often (how many seconds between each attack) that the enemy is attacking, then use a counter and add to it every Update. When the counter reaches attack time, deduct health and reset the counter. =]

var attackTimer : float = 0.0;
var attackNow : float = 0.1; // 1 every 1/10 of a sec, or 10 every sec.

function Update()
{
	if ( attackTimer > attackNow )
	{
		// deduct health
		// reset Timer
		attackTimer = 0.0;
	}
	attackTimer += Time.deltaTime;
}

in response to : Ive looked at a few different ways of making a timer but there not making any sense to me, could you help me out?

I personally have seen 2 types of timer/counter. The first is as in my comment, with a ‘counter’ and a maximum value.

var counter : float = 0.0;
var counterMax : float = 5.0;

function Update()
{
    if ( counter > counterMax )
    {
       // do stuff
       // reset Timer
       counter = 0.0;
    }
	// increment timer over time
    counter += Time.deltaTime;
	
	// increment counter with an inter value (like counting frames)
	// counter ++; // like this, counter can be an integer
}

the other method is using Time.time itself, but it still need an increment value to work.

var counter : float = 0.0;
var counterDelay : float = 5.0;

function Update()
{
    if ( Time.time > counter )
    {
       // do stuff
       // reset Timer
       counter = Time.time + counterDelay;
    }
}