- Home /
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);
}
Answer by fafase · Sep 20, 2012 at 04:42 PM
Weird, youh ave most of it except the easy part:
if(dist < 1.6){
curHealth -= damage;
}
Well, here is a quite simple way but maybe not the best:
var health:int =100;
function Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))InvokeRepeating("Damage",0.01f,1.0f);
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))CancelInvoke();
}
function Damage(){
health-=10;
print(health);
}
InvokeRepeating calls the first argument after the second argument of time. In my case, I call Damage after 0.01s (0.00s messes up the function, sounds like a bug). The third parameter is the frequency of the call, every second. When releasing the button I cancel the invoke.
And if you want to have the code style in the comment, you need to press space bar 4 times.
Answer by Griffo · Sep 20, 2012 at 05:00 PM
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;
}
Answer by AlucardJay · Sep 21, 2012 at 10:15 PM
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;
}
}
Blink :
This is what Ive got but Im getting tons of errors so I guess Its wrong, sorry for the formatting it only happens to me.
var fullHealth : int = 100; var curHealth : int = 100; var enemy : Transform; var counter : float = 0.0; var counter$$anonymous$$ax : float = 5.0;
function Update () {
if (counter > counter$$anonymous$$ax){ attack(); 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
if(enemy){ var dist = Vector3.Distance(enemy.position, transform.position); print ("Distance to enemy: " + dist); }
function attack () {
if(dist < 1.6){ fullHealth -= 10; print ("enemy attack"); }
}
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); }
var fullHealth : int = 100;
var curHealth : int = 100;
var enemy : Transform;
var counter : float = 0.0;
var counter$$anonymous$$ax : float = 5.0;
var dist : float = 1000.0;
function Update ()
{
if (counter > counter$$anonymous$$ax)
{
attack();
counter = 0.0;
}
// increment timer over time
counter += Time.deltaTime;
if(enemy)
{
dist = Vector3.Distance(enemy.position, transform.position);
print ("Distance to enemy: " + dist);
}
if (curHealth >= fullHealth)
{
curHealth = fullHealth;
}
if(fullHealth <= 0)
{
fullHealth = 0;
Debug.Log("You are Dead");
}
}
function attack ()
{
if ( dist < 1.6 )
{
fullHealth -= 10;
print ("enemy attack");
}
}
function OnGUI()
{
GUI.Label (Rect (25, 40, 100, 20), "Health = " + fullHealth );
}
Your answer
Follow this Question
Related Questions
Deduct health on collision 2 Answers
Restart with GUIText 2 Answers
Adding a counter? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Need Help About Health,Damage 1 Answer