x


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;
	}
}
more ▼

asked Oct 31 '11 at 10:39 PM

Jarrod_Walker gravatar image

Jarrod_Walker
16 5 5 7

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];

Nov 01 '11 at 03:53 AM GoSuNeem
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

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
    }
}
more ▼

answered Nov 01 '11 at 12:11 AM

aldonaletto gravatar image

aldonaletto
41.6k 16 42 197

The thing is tho which i forgot to mention is that the ai are spawned by an AIManager and AISpawner, so i dont have any access to the AI themselves

Nov 01 '11 at 12:24 AM Jarrod_Walker

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).

Nov 01 '11 at 03:03 AM aldonaletto

Thanks mate

Nov 01 '11 at 05:57 AM Jarrod_Walker

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

Nov 01 '11 at 07:31 AM Jarrod_Walker

EnemyTentacles is the enemy name or tag? If it's the name, compare col.name instead. 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--;
    }
}
Nov 01 '11 at 09:57 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x960
x249

asked: Oct 31 '11 at 10:39 PM

Seen: 824 times

Last Updated: Nov 02 '11 at 06:46 AM