Unity2D Collision - Get Left/Right side of collision

I’m trying to get left/right side of collision when some object collide with my unity 2d circle collider, not matter how circle object is rotated, it should always retun witch side of circle is collided. For example if x is smaller than circle.with/2 then it hit left side, else it hit right side.Any ideas how i could do this, i tried a lot of stuff but couldn’t get it working.

Problem Solved

You could use the OnTrigger, or OnCollision function and from there, use transform.InverseTransformPoint to find whether the collision happened left or right of the object.

I just tested this and it worked when I had one sphere drop on another one.

    void OnCollisionEnter(Collision collisionInfo){ //you can change this to OnCollisionStay or OnTriggerStay
        var direction = transform.InverseTransformPoint (collisionInfo.transform.position); //this helps us find which direction the object collided from

        if (direction.x > 0f) { //Change the axis to fit your needs
                print ("The object collided with the right side of the ball!");
        } else if (direction.x < 0f) {
                print ("The object collided with the left side of the ball!");
        }
  }
2 Likes

I did same thing, but sometimes it just return one side i don’t know why.
You can see what i mean on this video i uploaded, you can see ball’s collision side and what Debug.Log is showing.
I have also found this solution: java - Find angle of a point from center of circle - Stack Overflow
But also can’t get it working…Feeling frustrated with such an easy problem, but still unsolved.

I noticed in the video when you shot the ball to the right of the black dot enemy, it gave you a “Left” Debug.Log, and when you shot to the left of the enemy it gave you a “Right” Debug.Log.

I think all you need to do is just switch the left and the rights, because you may have had your perspective different from mine. I had mine looking Front, and I believe your perspective is Back.

(If you don’t know what Im talking about, it’s the X/Y/Z Axis on the top right corner of the scene window.)

Unfortunately my solution won’t give you precise measurement, I believe.

I forgot to say i’m using OnCollisionEnter2D

Problem Solved
When some object hit my 2d circle collider, i wanted to know at what point/angle it got hit.
Image Explanation
This method will return angle of collision.

public float GetCollisionAngle(Transform hitobjectTransform, CircleCollider2D collider, Vector2 contactPoint)
    {
        Vector2 collidertWorldPosition = new Vector2(hitobjectTransform.position.x, hitobjectTransform.position.y);
        Vector3 pointB = contactPoint - collidertWorldPosition;

        float theta = Mathf.Atan2(pointB.x, pointB.y);
        float angle = (360 - ((theta * 180) / Mathf.PI)) % 360;
        return angle;
    }

This is the result i wanted:

Yes it’s really simple, but when brain freeze it return only null xD

I know this is rather old now, but what are the 3 things I need to pass to call this correctly?

If i remember correctly i used it inside OnCollisionEnter Method:

public void OnCollisionEnter2D(Collision2D collision)
Than just pass the object that has collided (collision.transform.gameobject), this object collider, collision point also from collision.

Can’t find that project right now, it somewhere in backup storage. I was working on game similiar to zuma deluxe.

the same thing but i have a collider with object and its function is to throw arrows and i have separate aniamtion to turn right and left now enemy will come from its place if enemy comes near the object it has to turn left throw arrows and enemy passes half way then the object turn right…what i have to do for this am fully confused

The easiest way to determine direction in 2D is comparing the x pos.

if target x is greater than my x the target is to the right of me.
if target x is less than my x the target is to the left of me.

can u write a example code please

if (target.position.x < transform.position.x)
{
    //Target is to the left
}
else
{
    //Target is to the right
}

it’s working but it never back to idle it is continuosly… am struggling with this…
this s my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///


/// Attack with ranged weapon
///

public class AttackRanged : MonoBehaviour, IAttack
{
// Damage amount
public int damage = 1;
// Cooldown between attacks
public float cooldown = 1f;
// Prefab for arrows
public GameObject arrowPrefab;
// From this position arrows will fired
public Transform firePoint;

// Animation controller for this AI
private Animator anim;
// Counter for cooldown calculation
private float cooldownCounter;

///


/// Awake this instance.
///

void Awake()
{
anim = GetComponentInParent();
cooldownCounter = cooldown;
Debug.Assert(arrowPrefab && firePoint, “Wrong initial parameters”);
//gameObject.GetComponent().offset = new Vector2(newX, newY);
}

///


/// Update this instance.
///

void FixedUpdate()
{

if (cooldownCounter < cooldown)
{
cooldownCounter += Time.fixedDeltaTime;
}

}

///


/// Attack the specified target if cooldown expired
///

/// Target.
public void Attack(Transform target)
{
if (cooldownCounter >= cooldown)
{
cooldownCounter = 0f;
Fire(target);
}
}
/*void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.name == “BowmanL1”)
{
Debug.Log (“aaaa”);
anim.SetTrigger(“shootright”);
}

}*/
///


/// Make ranged attack
///

/// Target.
public void Fire(Transform target)
{
if (target != null) {
// Create arrow
GameObject arrow = Instantiate (arrowPrefab, firePoint.position, firePoint.rotation);
IBullet bullet = arrow.GetComponent ();
bullet.SetDamage (damage);
bullet.Fire (target);
if (target.position.x < transform.position.x) {
anim.SetBool (“shootleft”, true);
Debug.Log (“left”);
} else if (target.position.x > transform.position.x) {
anim.SetBool (“shootright”, true);
Debug.Log (“right”);
}
else
{
anim.SetTrigger (“idle”);
Debug.Log (“idle”);
}
}
}
}