Destroying Enemies/Being Killed By Them

Hi,

I tried searching the forums and google but I haven’t been able to find my answer.

I am creating an adventure game and I am stuck on one part of my game. I have already coded when the enemy touches my character, the character is destroyed by using the destroy(GameObejct). My question now is that I want my character to be able to jump on top of the enemy and destroy it but still have it when the enemy touches you, you die and lose a life.

I’m not really that great at coding and this is my first game so a little guidance is appreciated.

Thank you

Hi,

You could make that simply by creating an trigger on the head/top of the enemy

Enemy>
Box (Tick is Trigger and place it on the head of the enemy then attach the script here) /!\ Put the Box under your enemy hierarchy !

var Enemy: GameObject;    // Attach the Enemy who will be destroyed

   function OnTriggerEnter (other : Collider) {

  if (other.gameObject.tag == "Player") {

        Destroy(Enemy.gameObject);
    }

}

Don’t forget to untick Mesh renderer, if you want to hide the trigger :slight_smile:

Hello, thanks for the quick reply.
I have done exactly what you have said but I have a slight issue…

When I jump on the top, the box on the top is destroyed but the enemy below it is not. My character goes through the box to destroy it and ends up touching the enemy which then kills by player.

Just for a test I am using cubes created within Unity

double trigger isnt the best solution.

Rather, On Collision, I would check the X and Y in relation to the enemy.

If the enemy Y is below Player Y, then its probably landing on top of it. You will probably want to check that the X of both are close to each other.

This managed to fix the issue by using 2 colliders and making the head a child

function OnTriggerEnter (other : Collider) {

if (other.name == "Player") {
	Destroy(transform.parent.gameObject);
    }
}

why does every use javascript you can’t even use it in the new unity can you?

unity uses csharp only now that’s what i think