- Home /
How to get the point of collision when collision takes place inside the collider
Hello! I want to register collision when it happens inside BoxCollider2D and instantiate hit effect at that point, but when I try to determine the point using collision.GetContact(0).point I receive the point which is actually outside of the box collider.
void OnCollisionStay2D(Collision2D collision) {
Instantiate(hitEffect, collision.GetContact(0).point, Quaternion.identity);
Destroy(gameObject);
}
I got the result:
Appreciate your help in advance, guys!
UPD: I think I can add a condition and if the bullet is inside the container instantiate hit effect at the firepoint pisition. But it seems to be a duct tape code and I'm not sure this will work.
Answer by DeadZombieee · Jan 25, 2020 at 09:55 AM
Solved this by adding kinda duct tape code :) I added a trigger collider to my FirePoint object and if it's inside of any collider I set isInsideCollider variable to true. And then, in the bullet script I added a condition:
void Start()
{
if (firePoint.IsInsideCollider())
{
Destroy(gameObject);
Instantiate(hitEffect, firePoint.transform.position, Quaternion.identity);
}
else
{
CreateBullet();
}
}
So basically I create hit effect right away. Now it looks like this:
Your answer

Follow this Question
Related Questions
Linecast detects gameObject underneath the collided one 1 Answer
My character runs into the terrain 0 Answers
How do I add collider to tilemaps? 2 Answers
2D colliders not colliding 2 Answers
Problem with 2D Collider Trigger 1 Answer