Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best way to place sprite (which collides) on the top of screen

Discussion in '2D' started by pastaHead2, Feb 4, 2015.

  1. pastaHead2

    pastaHead2

    Joined:
    Dec 24, 2014
    Posts:
    7
    Hello!

    My 2D game is being developed for different resolutions (iphones/ipads).

    Somewhere I have the code to handle multiple resolutions(btw is it a good approach?):

    int defaultWidth = 1536;
    int defaultHeight = 2048;
    float cameraScale = (float) (Screen.height * defaultWidth / Screen.width / 200.0);
    Camera.main.orthographicSize = cameraScale;

    I have a canvas, which render mode is "Screen Space - Camera" and the Render Camera is Main Camera.

    Now I want to place the sprite on the top on screen (which collides with my character), well, I only see the 2 ways to do it:

    1) Place gameobject not inside canvas, then create component box collider within this object and change the position in code (depends on resolution)

    2) Place the UI image to canvas and set anchor point to top of it. I do not need any code any more, but now I can't add component box collider to this object, I can't make this work, because I get "The collider did not create any collision shapes as they all failed verification. etc" message

    I don't know what is a best approach to handle situations like this, how I have to "think" to handle these kind of troubles, what is a general rule.

    Thanks.