AI- trouble making NPC attack effect NPCs differently than player

I’m pretty new to Unity, so please bare with me. I have an invulnerable monster that attacks the player and allies. So I use FindGameObjectsWithTag to chase the player group.

like so:

var localEnemies : GameObject[];
localEnemies = GameObject.FindGameObjectsWithTag("opponents"); 

I think this is pretty standard and works without problem. Just to test the code if NPCs are hit they are destroyed and if the player is hit a game over scene is loaded. How can I get the player object recognized outside the tag used for tracking position? Here is the code I’m working with. -

function OnCollisionEnter(collision : Collision)
{
	var thing : GameObject = collision.gameObject;
	if(thing.tag == "opponents" || thing.tag == "trap")
	{
		if(thing.tag == "opponent" && thing == GameObject.Find("Player"))
		{
			status = false;
			animation.Play("attackMajor");
			Instantiate(small_flame, thing.transform.position, thing.transform.rotation);
			Application.LoadLevel("GameOver");
		}
		else if(thing.tag == "opponents" && thing != player)
		{
			status = false;
			animation.Play("attackMajor");
			Instantiate(small_flame, thing.transform.position, thing.transform.rotation);
			yield WaitForSeconds(3);
			Destroy(thing);
		}
		else if(thing.tag == "trap")
		{
			status = false;
			animation.Play("rage");
			yield WaitForSeconds(3);
			Instantiate(small_flame, thing.transform.position, thing.transform.rotation);
			Destroy(thing);
		}

	 	speed = 1;
	 	status = true;
	}
}

Any help would be really appreciated!

oh geez I'm such a dork there is a typo in the tag of the first if statement "opponent" should be "opponents"! AAARRGH :)

1 Answer

1

Use the name of you player. It is probably name “Player”.

if(thing.name == "Player")

Other way, would be to have a reference to your player. Since it is the only one it is not so annoying. Then you compare the references.

public GameObject player; // Drag player 

then in the method:

if(thing == player)

This one is actually faster since you compare a 4 bytes address while the other one is a string comparison that needs to go through each char.