Search Unity

Question Limit all game-objects to the size of a quad

Discussion in 'Getting Started' started by ARunitystudi, Sep 7, 2021.

  1. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    Hello, I have a Canvas with the Render mode "Screen Space - Overlay". In this canvas, I specified a Quad for the area on the screen, in which I want my models to be rendered (currently they can be moved outside this area and I want to prevent this). This Quad scales correctly to different screen sizes and resolutions as desired.

    Now I want to get the coordinates of the four corners of this Quad in worldspace to be able to limit the positions of all gameobjects in my scene.
    I tried to work with extents of a mesh filter, but the corners always output positions between -5 and 5 regardless of e.g. the screen-resolution or size, when I do it this way. So, when I limit all my models to this values (-5 to 5), nothing happens as they are much smaller than this (but also cut by the screen bounds).

    So, what is the correct way to get the coordinates of the corners of my Quad in worldspace?

    Currently, I am doing it the following way:
    Code (CSharp):
    1. MeshFilter meshFilter = screenQuad.GetComponent<MeshFilter>();
    2. Vector3 extends = meshFilter.mesh.bounds.extents;
    3.  
    4. Vector3 bottomLeftPosition = new Vector3(-extends.x, -extends.y, 0);
    5. Vector3 bottomLeftWorldSpacePosition = screenQuad.transform.TransformPoint(bottomLeftPosition);
    6.  
    7. Vector3 bottomRightPosition = new Vector3(extends.x, -extends.y, 0);
    8. Vector3 bottomRightWorldSpacePosition = screenQuad.transform.TransformPoint(bottomRightPosition);
    9.  
    10. Vector3 topLeftPosition = new Vector3(-extends.x, extends.y, 0);
    11. Vector3 topLeftWorldSpacePosition = screenQuad.transform.TransformPoint(topLeftPosition);
    12.  
    13. Vector3 topRightPosition = new Vector3(extends.x, extends.y, 0);
    14. Vector3 topRightWorldSpacePosition = screenQuad.transform.TransformPoint(topRightPosition);
     
  2. ZingZangGames

    ZingZangGames

    Joined:
    Dec 11, 2020
    Posts:
    73
    You could also try render textures that scale with the screen (and also support all other screen aspects than the standard 16:9)