Canvas Boundary and Enemy Visibility and Player Movement

When running the game in the Editor and set to 9:16 Aspect Ratio everything looks and behaves fine. If I change it to Free Aspect or build it to run outside of the editor two problems happen:

  1. Enemies that spawn outside of the canvas/background are visible.
  2. The player can move off of the canvas/background.

Did I miss something somewhere to constrain the game to the canvas?

I have something that works to address problem #2.

In the method where I am setting the boundaries for the player, I get the RectTransform of the Canvas and then get the scaled width and height and use that to calculate the min and max x and y.

    private void SetupMoveBoundaries()
        Camera gameCamera = Camera.main;

        SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();

        xPad = (spriteRenderer.bounds.max.x - spriteRenderer.bounds.min.x) / 2;
        yPad = (spriteRenderer.bounds.max.y - spriteRenderer.bounds.min.y) / 2;

        Canvas gameCanvas = FindObjectOfType<Canvas>().rootCanvas;
        RectTransform canvasRectTransform = gameCanvas.GetComponent<RectTransform>();

        float panelWidth = canvasRectTransform.rect.width * canvasRectTransform.localScale.x;
        float panelHeight = canvasRectTransform.rect.height * canvasRectTransform.localScale.y;

        xMin = -panelWidth/2 + xPad;
        xMax = panelWidth/2 - xPad;
        yMin = -panelHeight/2 + yPad;
        yMax = panelHeight/2 - yPad;

I have the same Problem.

This helped with the Player, thanks a lot!

But I can’t figure out how to solve Problem 1, except for setting the waypoints all above the playable Screen. Also my Particles are flying in the blue ares (which is really distracting).

I’ve tried to build a canvas around the camera, but the the Shapes of the Images keep resizing and re-positioning themselves, no matter what and my understanding of coding doesn’t help me either at this point. It get’s really frustratng. Sitting around two days at this problem and nothing seems to work.

Does anyone has any suggestion?

I tried in the build for WebGL to specify the exact dimensions of the playable space to show in the player and that worked up until the point where I clicked on the full screen button and then I was back to the same problem.

I was thinking of trying to add opaque (maybe solid black) images to the UI that would be sized to cover the left and right border areas and be anchored to the left and right sides of the canvas, similar to how I have anchored the score and health to the corner of the canvas, and make sure they are on a Z coordinate in front of where the sprites are so that they cover up anything that is off canvas.

I have a feeling that this is probably not the ideal solution. Don’t know what it does to performance. Are all of the out of bounds sprite still getting rendered even though they are not visible? I briefly did some reading about Occlusion Culling to not render things that are not visible to the camera but having explored that.

I’ve tried the picture method and it worked just fine. Used a 8x8px Sprite that I scaled up to the needed dimensions.

But. I also found this Asset and after some testing it solves both of the problems. :slight_smile: Just add “Force Camera Ratio” as a component to your camera and set the aspect ratio that you want to have. In our case X= 9, Y = 16.

Worked fine for me. The player can’t move outside the area and the non playable areas are blacked out. \o/


I tried that Auto Letterbox asset and boy that was simple and easy - add component to camera, set the X=9 and Y=16, and done. Works great!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms