1
\$\begingroup\$

I want to create a game where the main character can pass through all the zombies that are chasing him but he can also jump up and bounce on their heads to kill them (like in old mario)...

The problems I am running into are:

  1. When I am in the same position as the zombie and jump to kill him the trigger collider triggers the Destroy(Zombie) method. I only want them to be killed when you "Stomp" on them.
  2. How do I make the character able to pass through the zombies without falling through the floor?

Here is my code for the trigger event that is attached to the "Stomper" box collider on my character.

     void OnTriggerEnter2D (Collider2D other) {
    if (other.tag == "EnemyHead") {


        //Destroy
        Rigidbody2D playerRB = transform.parent.gameObject.GetComponent<Rigidbody2D> ();
        playerRB.velocity = new Vector2 (0, 0);
        playerRB.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

        Destroy (other.transform.parent.gameObject);

    }
}

Let me know if you need anything else. I am trying to explain this as well as possible...

\$\endgroup\$
2
  • \$\begingroup\$ About 2: You can create collision layers, so you can setup collision between zombie and ground and disable collision between players body and zombie. \$\endgroup\$ Sep 7, 2016 at 6:40
  • \$\begingroup\$ @PawełMarecki thanks, that works for that. But how do I get my trigger child collider trigger the event to destroy the zombie? \$\endgroup\$
    – Wp3Dev
    Sep 7, 2016 at 6:51

1 Answer 1

2
\$\begingroup\$

As mentioned in the comments you can add the zombies to another layer and ignore it for collision with the player.

For your stomp mechanic: A quick solution would be to check the velocity of the player. When the player is 'stomping' you can define that the player needs to be on the downward part of the jump. So you check if your y component is less than 0, the destroy and apply the new velocity.

\$\endgroup\$
2
  • \$\begingroup\$ Awesome worked that problem out but now I have a new problem... After two stomps the next one I stomp nothing happens unit I land on the ground and then jump again then it works fine. \$\endgroup\$
    – Wp3Dev
    Sep 7, 2016 at 7:07
  • 1
    \$\begingroup\$ This should have nothing to do with this solution. I reccomend you to accept / close this question if it fullfills your need and post a new one with relevant code snippets \$\endgroup\$
    – floAr
    Sep 7, 2016 at 7:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .