Boundary set with Camera.WorldToViewportPoint changes on Executable

Another struggle that probably has an easy answer. I setup my screen boundary using Camera.WorldToViewportPoint as the lecture taught, and it worked find inside the tool. Perfect. However …

When I generate an executable on the desktop, two things happen:

  1. The screen size is wider than my 9:16 ratio, so there is blue backround padding in each side. That’s ok.
  2. The car now goes through my boundary, and only stops at the end of the screen (the player is the red car). See the screenshot below.

So what what am I missing?

Hi,

If you want to keep that window size, you’ll have to calculate the offset via code. @SteveBee had the same issue and shared a solution in their thread. Did it fix it for you, too?

Well, I’ll give that a try, though it seems a whole lot more complicated than the course indicates. You also mentioned, “In the Build Settings > Player Settings, you would have to set a resolution with that aspect ratio”, and that is something I tried. But I didn’t see an option to set anything other than a “Default Resolution”, which didn’t solve the problem.

You thing I noticed that is not covered a lot in the course, is these deployment questions. I had similar issues in earlier chapters (a little easier in those). In this chapter there isn’t even a discussion at all on how to deploy, and what options to set. And by the looks of it, the code shared won’t really work properly,

Still love the course. But just hoping that’s something that can be added. In any case, I’ll try to work on these workarounds, and post back here if they work.

If you want to set your window to the correct aspect ratio/resolution, you could take a look at my instruction in this thread on Udemy.

The problem is that the viewport of the camera usually refers to the entire game window. We use ViewportToWorldPoint (not WorldToViewportPoint!) to get the edges of the game window/viewport in World Units. If the game window does not have the correct aspect ratio, the returned values are “wrong”.

Thanks, I’ll take a look at my next break from work.

I got it working for now just using plain game units, and I configured the limits with Serializable fields. Is there a reason why the solution using ViewportToWorldPoint is better?

With ViewportToWorldPoint, you can have Unity calculate the edges of your screen and do not have to manually figure values out. Whether that’s a better approach, is a matter of personal preference.

Thanks, that’s a good answer.

One more thing. I looked at your earlier thread, and it was helpful. However, you mentioned “disable the Display Resolution Dialog, you get the desired resolution”, and that option is not there. I compared your screen shot with mine, so I know I’m in the right place, but that option is gone. I assumed it changed in a newer versions of Unity (I’m on v2019.3.0f6). Do you know what the equivalent option is now?

It was removed in one of the newer Unity versions a couple of weeks ago. If you don’t have that option in your version, skip that step in my instruction.

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

Privacy & Terms