0
\$\begingroup\$

I have a stationary sprite with the tag "Player" with a Rigidbody2D (dynamic body type and discrete collision detection) and circle collider 2D.

I also have a moving UI image; also with a Rigidbody2D (dynamic body type and discrete collision detection) and circle collider 2D.

I want to detect when the moving UI Image collides with the sprite, however I'm not getting the print statement inside of the OnCollisionEnter2D() function below whenever I see them collide on the screen.

public class MeteorController : MonoBehaviour
{
    float xCoor;
    float yCoor;

    float newXCoor;
    float newYCoor;
    Vector3 newPos;

    float speed = 1.25f;

    // Start is called before the first frame update
    void Start()
    {
        xCoor = transform.localPosition.x;
        yCoor = transform.localPosition.y;

        newXCoor = xCoor * -1;
        newYCoor = yCoor * -1;
        newPos = new Vector3(newXCoor, newYCoor, 0);
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(newPos * Time.deltaTime * speed);
    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Player")
        {
            SceneManager.LoadScene("PlanetSpawningTest");
            print("PLAYER");
        }
        else if (coll.gameObject.tag == "Planet")
        {
            print("PLANET");
        }
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Remember, physics doesn't happen in screen space, it happens in world space. Look in your scene view and zoom out far enough to show both the sprite and UI image at the same time. They're not in the same place in the world, even if they're drawn in the same place. The UI just gets overlaid using its own independent mapping after your other objects have been drawn. So yes, this collision is possible to detect, but to collide with physics you need to map them in the same space. Or you can do your own checks, based on screenspace or accounting for the two mappings. \$\endgroup\$
    – DMGregory
    Feb 10, 2020 at 4:54
  • \$\begingroup\$ What steps do I need to take to map the two objects in the same space? \$\endgroup\$
    – Jnguyen22
    Feb 10, 2020 at 4:58
  • 1
    \$\begingroup\$ I think a much better plan is to not do this. Show us the effect you want to create and we may be able to suggest a simpler solution than trying to make these two different worlds collide. \$\endgroup\$
    – DMGregory
    Feb 10, 2020 at 5:01

2 Answers 2

0
\$\begingroup\$

I don't recommend doing it this way but just for the sake of your question.

Render the canvas to the world space. To detect the collision between the UI image and Sprite, based off of what you see on the game view rather than scene view.

Unity Inspector canvas

However, this will displace all the objects in your canvas so it wont be shown on the game window anymore and you will have to re position the UI objects. So for this you can create a separate canvas just for the UI Images that have collision and render that canvas to world space.

\$\endgroup\$
0
\$\begingroup\$

There's probably a more elegant solution, but the way I would do this is to attach a script to the UI that needs to be collided with.

This script will calculate the screen-space to world space coordinates of the UI element. Then it will create a box collider with the dimensions of the UI element in world space, making its Z component very long so it overlaps your world. Then you can test collisions between this box collider and your player.

\$\endgroup\$
1
  • \$\begingroup\$ If you're using 2D box colliders you don't need / can't set a Z component. There are only X and Y, and Z is ignored for the sake of collision (it's usually only used for determining which item has priority for point-in-shape tests, to pick the frontmost) \$\endgroup\$
    – DMGregory
    Feb 10, 2020 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .