Answer the question
In order to leave comments, you need to log in
How to do top, right, left damage in a 2D game?
The enemy is killed if you jump on top of him, initially the problem was that the damage during the jump was inflicted on both the enemy and the player, fixed it, but now there is another problem, when the enemy moves from the left side, then no damage is done, but if it is applied from the right (2 hearts) . what is needed so that the damage passes on the right side
. Here is the script for receiving damage, assigned to the enemy and the player:
public class CollisionDamage : MonoBehaviour
{
public int damage = 10;
public string collisionTag;
GameObject enemy;
GameObject player;
private void OnCollisionEnter2D(Collision2D coll)
{
Health healthE = enemy.GetComponent<Health>();
Health healthP = player.GetComponent<Health>();
if (coll.gameObject.CompareTag(collisionTag)) //tag enemy
{
foreach (ContactPoint2D point2D in coll.contacts)
{
if (point2D.normal.y >= 0.5f)
{
//Destroy(enemy.gameObject);
healthE.takeDamage(damage);
}
if (point2D.normal.x >= 0.5f)
{
healthP.takeDamage(damage);
}
}
}
}
private void Start()
{
enemy = GameObject.FindWithTag("Enemy");
player = GameObject.FindWithTag("Player"); //GameObject.FindGameObjectWithTag("Player");
}
}
Answer the question
In order to leave comments, you need to log in
if (point2D.normal.x >= 0.5f)
{
healthP.takeDamage(damage);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question