Location of RigidBody2D on collision

How do I find the exact location of an object on collision. The contacts shows you the point where the two objects touched, but at that same moment of collision I need the position of the colliding object instead, not the point where they touched.

The collision data object also stores a reference to the other collider. So you should be able to do something like collision.collider.transform.position.

Anything stopping you from doing this?

  private void OnTriggerEnter2D(Collider2D col) {
        Debug.Log(col.gameObject.transform.position);
    }

This will return the objects position on TriggerEnter. You can do OnCollisionEnter as well.

Okay, so I’ve achieved this result with the following setup.

  • Ball GameObject with a “knob” Sprite
  • CircleCollider2D and
    RidgidBody2D(continous), Trail
    Renderer

Attach “FireBall” script with following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireBall : MonoBehaviour {

    Rigidbody2D rb;
    [SerializeField]
    GameObject hitPoint;

    // Use this for initialization
    void Start() {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update() {

        if (Input.GetKey("d")) {
            rb.AddForce(new Vector2(100, 200));
        }
        if (Input.GetKey("a")) {
            rb.AddForce(new Vector2(-100, 200));
        }

    }

    private void OnCollisionEnter2D(Collision2D col) {
        if (col.gameObject.tag == "wall") {
          
            GameObject obj = Instantiate(hitPoint, transform.position, Quaternion.identity);
     
        }
    }
}
  • GameObject with BoxCollider2D and tag
    of “wall” > stretched to enclose the
    ball
  • Prefab serialized into the “FireBall”
    script as variable “hitPoint”

With the above setup you can achieve this result.

97710-ballcorrectcollision.jpg

The ball leaves a copy of the prefab at the point where you have noted as your point of interest.

If this has helped, feel free to accept as the answer :wink: