- Home /
How do I hurt enemies by jumping on them without getting killed?
I have half of the code already written. I am just not sure if the player will get hurt since I have the enemies hurt my player through collisions.
The code here is for the enemy. If i switch this code around will the program allow the player to attack the enemy without getting hurt? or will they both get hurt?
void OnCollisionEnter2D(Collision2D frog)
{
if (frog.gameObject == PTarget)
{
playerRange = true;
}
}
void OnCollisionExit2D(Collision2D frog)
{
if (frog.gameObject == PTarget)
{
playerRange = false;
}
}
// Update is called once per frame
void Update()
{
attackTime();
}
void attack()
{
timer = 0f;
if (PlayerHealth.currentHealth > 0)
{
PlayerHealth.TakenDmg(amount);
}
}
There's probably a cute way to do this with Platform Effectors.
Answer by Vollmondum · Apr 05, 2019 at 06:58 AM
There should be either 4 colliders on each side, or raycasting from 8 points. Left/right/top collider corresponds for you being hurt, bottom one - kick enemies. Top collider also checks if you're hitting a ceiling. Same for raycasting, just each raycast should be out of imaginary colliders boundaries.
This should definitely do the trick. $$anonymous$$aybe alternatively you can have 2 colliders, one larger one that covers the top and sides of the player, and a smaller one that covers the bottom. Essentially the same I think, but with fewer colliders.
So i should attach a bottom collider at my players feet and add in.
void OnCollisionEnter2D(Collision2D bottom)
{
if (bottom.gameObject == frogbody)
{
enemyRange = true;
}
}
i wrote that at the top of my head, but it should be something similar to that?
enemyDamage, right. bottom.gameObject.tag == enemyTag
Your answer
Follow this Question
Related Questions
How to stop enemy within certain distance of player? 2 Answers
Problems with enemy attack script! 0 Answers
enemy attack the player 0 Answers
How to correctly do an enemy ranged attack script? 0 Answers