Hi,
I want to create a 2D box which will kill the player if you run into in but you can Jump onto it without dying. Similar to the sprites in the Super Mario games.
I don’t know where to start looking to solve this issue. Help will be appreciated.
Thanx in advance
3 Answers
3
So you basically need to know the relative position of the player to the thing it’s hitting in your OnCollisionEnter2D routine. Simply check if the Y coordinate of your player + the height of your player is within x (let’s say 5) pixels of the Y coordinate of your enemy - if it is then it’s above it.
Another choice is the see the angle between the .up of the enemy and the vector between the enemy and the player:
var vectorToPlayer = player.transform.position - enemy.transform.position;
if(Vector3.Angle(vectorToPlayer, enemy.transform.up) < 50) {
//Safe
} else {
//Kill the player
}
Other option is using the plane colider on the sides with code for killing the player, and a plane collider on top wich he can walk on…
i think that would work…
Couldn’t you just use A tiny raycast coming from the side of the player, and a tiny one coming from the bottom of the player. If the forward ray hits the platform collider he dies, if the downward ray hits the collider hes safe? Though the above answer using a plane on the sides would work, but that requires adding more game objects.