# How Can I Make The Collision Detection In a Correct Way?

Hello everyone,

The thing what i am going to talk about is little bit different. I am currently working on a 2D platformer action game in which characters have no health. Players try to destroy each other by trying to push each other out of the platform with guns or other explosives. Let’s suppose i have two players in the game screen and when player 1 fires his weapon in a certain distance, the bullet goes and hits his opponent and bullet pushes his opponent a little bit back with this code below;

``````    void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "bulletP1")
{
if ((this.transform.position.x - coll.collider.transform.position.x) < 0)
{
print("hit right");
// transform.Translate(Vector3.left * speed * Time.deltaTime);
}
else if ((this.transform.position.x - coll.collider.transform.position.x) > 0)
{
print("hit left");
// transform.Translate(Vector3.right * speed* Time.deltaTime);
}
}
}
``````

But if Player 1 fires his weapon when his weapon is inside of Player 2(You can see the example picture in the attachment 1), it is not possible to find out for Player 2’s collider to which side is hit correctly because the bullet is created inside of the collider and It causes Player 2 to push in wrong direction because of wrong side collision.

How can i push the players to the correct directions even if the weapon is fired inside of the collision?

One way would be to compare with the player’s position instead of the collision location.

To do that you could add a component to your bullets which can hold the “Owning Player” reference or something, which you would assign when the player instantiates the bullet, and then on collision you can get it using GetComponent for the comparison.

Another way would be to use the bullet’s forward direction or velocity as the force direction, or the gun’s forward direction.

Thanks for your answer. I will try the ways you suggest. I really appreciated. I will be waiting for other suggestions.

Edit:
I just tried the first way, it works pretty good. I am going to try to find a bug. If i cannot find a bug i will use this way.

``````        if (coll.gameObject.tag == "bulletP1")
{

if(GameObject.Find("Player1").transform.position.x < this.gameObject.transform.position.x)
{
}else
{
}

}
``````

Just in general, you want to be avoiding using Find functions whenever possible. They’re highly inefficient as they crawl thru the whole hierarchy looking for the object name. It’s much better to already have a reference to the correct gameobject.

For example:

``````public class Bullet : MonoBehaviour
{
public Player owningPlayer;
public float impactForce = 1000;
}
``````

Then your player class can look something like this:

``````public class Player : MonoBehaviour
{
public GameObject bulletPrefab; // prefab that has the Bullet component

private Rigidbody2D rigid;

private void Awake()
{
rigid = GetComponent<Rigidbody2D>();
}

public void Shoot()
{
// spawn the bullet and do shoot stuff
Bullet bullet = Instantiate(bulletPrefab).GetComponent<Bullet>();
// assign this player as the owner
bullet.owningPlayer = this;
}

public void OnCollisionEnter2D(Collision2D coll)
{
// try to get a bullet component from the collision object
Bullet bullet = coll.gameObject.GetComponent<Bullet>();

// if the object is a bullet, and it doesn't belong to this player
if (bullet != null && bullet.owningPlayer != this)
{
// get the sign of the x difference between players(-1 or 1)
float direction = Mathf.Sign(bullet.owningPlayer.transform.position.x - transform.position.x);