- Home /
2D Platformer, Jump on the enemies to kill them.
I'm creating a 2D platformer in Unity. I'm trying to get the player to be able to jump on the enemies head in order to destroy them. In order to test if the player is jumping on them or not, I'm checking whether the player's y position is higher than the enemies' y position via this script. The issue I've ran into is that with multiple enemies in the scene, it seems to only track the lowest enemy, so the player is able to kill any enemy that is high up by simply colliding with them. Is there a better way to handle this?
void OnTriggerEnter(Collider c) {
GameObject player = GameObject.FindWithTag ("Player");
GameObject enemy = GameObject.FindWithTag ("Enemy");
if (c.tag == "Player") {
if (player.transform.position.y <= enemy.transform.position.y + 2) {
c.GetComponent<Entity> ().TakeDamage (10);
}
if (player.transform.position.y >= enemy.transform.position.y) {
Destroy(this.gameObject);
}
}
Please only post a question once. All questions go through moderation, which may take from $$anonymous$$utes to a couple of hours. When you earn 15 karma points, your questions will not longer go through moderation.
I didn't know I posted the question more than once? But in any case, its been 6 days with no answer. Is the question still in moderation..?
Quick question, are you placing this script in the player or the enemy?
Your answer
Follow this Question
Related Questions
2D Platformer AI Jump? 2 Answers
Jump on Enemy heads to destroy enemy? 1 Answer
Enemy follows the only player in rotation 2 Answers
Scripts Destroys all Objects its Attached to! 2 Answers
How do I stop jumping in mid air? 3 Answers