Search Unity

Unity2D Collision - Get Left/Right side of collision

Discussion in 'Scripting' started by Marceta, Nov 26, 2014.

  1. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    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.


     
    Last edited: Nov 28, 2014
  2. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    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.
    Code (CSharp):
    1.  
    2.     void OnCollisionEnter(Collision collisionInfo){ //you can change this to OnCollisionStay or OnTriggerStay
    3.         var direction = transform.InverseTransformPoint (collisionInfo.transform.position); //this helps us find which direction the object collided from
    4.  
    5.         if (direction.x > 0f) { //Change the axis to fit your needs
    6.                 print ("The object collided with the right side of the ball!");
    7.         } else if (direction.x < 0f) {
    8.                 print ("The object collided with the left side of the ball!");
    9.         }
    10.   }
     
    Thalon4 and 8beat like this.
  3. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    Last edited: Nov 28, 2014
  4. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    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.
     
  5. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    I forgot to say i'm using OnCollisionEnter2D
     
  6. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    Problem Solved
    When some object hit my 2d circle collider, i wanted to know at what point/angle it got hit.
    This method will return angle of collision.
    Code (CSharp):
    1. public float GetCollisionAngle(Transform hitobjectTransform, CircleCollider2D collider, Vector2 contactPoint)
    2.     {
    3.         Vector2 collidertWorldPosition = new Vector2(hitobjectTransform.position.x, hitobjectTransform.position.y);
    4.         Vector3 pointB = contactPoint - collidertWorldPosition;
    5.  
    6.         float theta = Mathf.Atan2(pointB.x, pointB.y);
    7.         float angle = (360 - ((theta * 180) / Mathf.PI)) % 360;
    8.         return angle;
    9.     }
    This is the result i wanted:



    Yes it's really simple, but when brain freeze it return only null xD​
     
    Last edited: Nov 28, 2014
  7. jjconroy

    jjconroy

    Joined:
    Aug 25, 2016
    Posts:
    1
    I know this is rather old now, but what are the 3 things I need to pass to call this correctly?
     
    Last edited: Aug 31, 2016
  8. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    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.
     
  9. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    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
     
  10. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    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.
     
  11. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    can u write a example code please
     
  12. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Code (CSharp):
    1. if (target.position.x < transform.position.x)
    2. {
    3.     //Target is to the left
    4. }
    5. else
    6. {
    7.     //Target is to the right
    8. }
     
  13. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    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;

    /// <summary>
    /// Attack with ranged weapon
    /// </summary>
    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;




    /// <summary>
    /// Awake this instance.
    /// </summary>
    void Awake()
    {
    anim = GetComponentInParent<Animator>();
    cooldownCounter = cooldown;
    Debug.Assert(arrowPrefab && firePoint, "Wrong initial parameters");
    //gameObject.GetComponent<CircleCollider2D>().offset = new Vector2(newX, newY);
    }

    /// <summary>
    /// Update this instance.
    /// </summary>
    void FixedUpdate()
    {

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

    }

    /// <summary>
    /// Attack the specified target if cooldown expired
    /// </summary>
    /// <param name="target">Target.</param>
    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");
    }

    }*/
    /// <summary>
    /// Make ranged attack
    /// </summary>
    /// <param name="target">Target.</param>
    public void Fire(Transform target)
    {
    if (target != null) {
    // Create arrow
    GameObject arrow = Instantiate (arrowPrefab, firePoint.position, firePoint.rotation);
    IBullet bullet = arrow.GetComponent<IBullet> ();
    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");
    }
    }
    }
    }
     
    Last edited: Aug 5, 2017