- Home /
AI Damage if touched
How can you make an AI character cause damage to the player if it touches it.
If needed my health script is:
var healthdead : Texture2D; //dead var health1 : Texture2D; //one hit left var health2 : Texture2D; //two hits left var health3 : Texture2D; //three hits left var health4 : Texture2D; //four hits left var health5 : Texture2D; //five hits left var health6 : Texture2D; //six hits left var health7 : Texture2D; //seven hits left var health8 : Texture2D; //eight hits left var health9 : Texture2D; //nine hits left var health10 : Texture2D; //ten hits left var health11 : Texture2D; //eleven hits left var health12 : Texture2D; //twelve hits left var health13 : Texture2D; //thirteen hits left var health14 : Texture2D; //fourteen hits left var health15 : Texture2D; //fifteen hits left var health16 : Texture2D; //sixteen hits left var health17 : Texture2D; //seventeen hits left var health18 : Texture2D; //eighteen hits left var health19 : Texture2D; //nineteen hits left var health20 : Texture2D; //twenty hits left var health21 : Texture2D; //twenty one hits left var health22 : Texture2D; //twenty two hits left var health23 : Texture2D; //twenty three hits left var health24 : Texture2D; //full health
static var LIVES = 24;
function Update () { switch(LIVES) { case 24: guiTexture.texture = health24; break;
case 23:
guiTexture.texture = health23;
break;
case 22:
guiTexture.texture = health22;
break;
case 21:
guiTexture.texture = health21;
break;
case 20:
guiTexture.texture = health20;
break;
case 19:
guiTexture.texture = health19;
break;
case 18:
guiTexture.texture = health18;
break;
case 17:
guiTexture.texture = health17;
break;
case 16:
guiTexture.texture = health16;
break;
case 15:
guiTexture.texture = health15;
break;
case 14:
guiTexture.texture = health14;
break;
case 13:
guiTexture.texture = health13;
break;
case 12:
guiTexture.texture = health12;
break;
case 11:
guiTexture.texture = health11;
break;
case 10:
guiTexture.texture = health10;
break;
case 9:
guiTexture.texture = health9;
break;
case 8:
guiTexture.texture = health8;
break;
case 7:
guiTexture.texture = health7;
break;
case 6:
guiTexture.texture = health6;
break;
case 5:
guiTexture.texture = health5;
break;
case 4:
guiTexture.texture = health4;
break;
case 3:
guiTexture.texture = health3;
break;
case 2:
guiTexture.texture = health2;
break;
case 1:
guiTexture.texture = health1;
break;
case 0:
//gameover script here
break;
}
}
ohh man... just a tip... For those texture variables, you might just want to make a array of textures. then just do
guiTexture.texture = health[LIVES];
Answer by aldonaletto · Nov 01, 2011 at 12:11 AM
You could use a trigger object childed to the enemy; when the player enters this trigger, you can reduce its health in the player script, since OnTrigger events are sent to both, the trigger and the object that entered it.
Child an object of suitable shape (cube, sphere, capsule etc.) to the enemy, adjust its dimensions, disable the Mesh Renderer to make it invisible, set Is Trigger true and tag this object as "Enemy" (for instance). In the player's script, add the code below:
function OnTriggerEnter(col: Collider){ if (col.tag == "Enemy"){ // verify if it's the enemy trigger HealthScript.LIVES--; // supposing the script above is HealthScript.js } }
The thing is tho which i forgot to mention is that the ai are spawned by an AI$$anonymous$$anager and AISpawner, so i dont have any access to the AI themselves
Well, if the enemies are CharacterControllers or have rigidbodies, you can do the inverse thing: add the trigger to the player. The function will work the same, provided that the enemies all have a common tag like "Enemy" or equivalent (use the correct tag in this script).
Its still not working. I have put the script in and it doesn't work it says:
function OnTriggerEnter(col: Collider){ if (col.tag == "EnemyTentacles"){ // verify if it's the enemy trigger HealthBar.LIVES--; // supposing the script above is HealthScript.js } } EnemyTentacles is the name of the enemy I have also created that capsule around the player and called it damage i tryed that in the script to and it didn't work either
EnemyTentacles is the enemy name or tag? If it's the name, compare col.name ins$$anonymous$$d. You can also place a debug line to help understand what is actually happening:
function OnTriggerEnter(col: Collider){
print("Hit by "+col.name); // tell what hit the player
if (col.name == "EnemyTentacles"){
HealthBar.LIVES--;
}
}
Your answer
Follow this Question
Related Questions
Attack Script problem please help 1 Answer
Player Damage to enemy damage melee system 0 Answers
AI keeps attacking objects that are not player. 1 Answer
Ai that applies damage in collision? 1 Answer