- Home /
Question by
pleasurehouse · Dec 28, 2020 at 03:57 PM ·
collisionspritecontrollertilemap
how to get off the enemy from the player top?
There are times when my player stays on top of my enemy and vice versa. I used the following code to get off my enemy from my player and vice versa. But sometimes the sprite falls in a corner between the ground and the wall, becouse Unity does not detect collisions well there and the sprite crosses the ground and fall untill infinite. (Unity has is a big problem with the tilemap corners).
Can you tell me a better way to get my enemy off my player? Thank you so much.
void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.name = "Tilemap")
{
boxCollider.isTrigger = false;
}
if (collision.compareTag("Enemy"))
{
Vector3 n = collision.GetContact(index).normal;
if (Mathf.CeilToInt(n.y) == 1)
{
boxCollider.isTrigger = true;
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
boxCollider.isTrigger = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
boxCollider.isTrigger = false;
}
private void OnTriggerExit2D(Collider2D collision)
{
boxCollider.isTrigger = false;
}
Comment