At the start of my scene I want to keep a chasing character on the left edge of the camera with a little more than half of him showing. But the results are different on my laptop vs a friend’s laptop.
Here’s what it looks like on my screen (14” MacBook Pro 3024×1964)
You can see that on my friend’s laptop the guy with the mohawk (the chaser) is further off-screen.
I logged the distance between the center of our screens to the left edge and they came back at 7.500001f and 7.502171f. So I’m not sure why the chaser is positioned that much differently.
Should I be adjusting the Orthographic size of the main camera based on the player’s resolution? I’m not sure how I can get consistent positioning across different screens.
I know it’s a minor difference, but it’s important that the player sees a little more than half of the character that’s chasing them.
High-Level Setup
I’ve rigged up a setup where the camera follows a center object (circle) as it auto-moves to the right. That center object is connected to another object (square) with a distance joint, and the square object is connected to a character with a slider joint.
Basically, as the circle joint moves to the right, the square follows at a fixed distance that I can change as needed. And in turn, the character stays aligned with the square’s X position and moves with it.
To calculate that starting position of the chaser I do the following:
Get the distance from the center of the screen to the left edge
Move the square to that position so the character is half off-screen
Shift the square position slightly to the right to show a bit more of the character
– I just took the character’s width, divided by 8 to get a slice and used that number as the offset
– I was hoping that by doing this it would be consistent across different screens since we’re always shifting the chaser to the right by 1/8 of his width.
This may have to do with aspect ratio. Your screen ratio is 1.54 (77:50 ??) while your friends’ is 1.6 (16:10).
Why are you basing this distance from the center of the screen anyway? The left border is at X=0 so if you simply place the object with X=0 plus your offset, that’ll work regardless of aspect.
Never transcribe code. Always post the code. We can’t tell if you made a simple mistake from a description of the code.
There’s a lot of different things necessary, different between UI and the actual main camera views, and different if they are orthographic or perspective.
Here are the official docs on how to make your UI handle various resolutions and aspect ratios cleanly:
Here are some notes on UI Anchoring, Scaling, CanvasScaler, etc:
Usually you need to choose a suitable ScaleMode and MatchMode in the Canvas Scaler and stick with it 100%. Generally if you change those settings you will often need to redo your UI entirely.
I also use this CanvasScalerOrientationDriver utility to make sharing UI for Landscape / Portrait easier. Read what it does carefully.
Generally cameras are tied height-wise to the .orthographicSize, or the .fieldOfView
To tie it to the width, use ratios with the Screen.width and Screen.height, or alternately whatever pixel viewport you are rendering to. Remember those are integers so dividing them properly requires casting at least one of them to a float
I’ll definitely remember not to transcribe code going forward. Thank you for the tip. I’m not at the right machine right now so I can’t share the code at the moment.
Im basing the distance from the center of the screen because I’m using a Distance Joint. 0 for the distance would be right on top of the object it’s attached to (circle in the center of the screen). So 7.5 pushes the distance out from the center to the edge of the camera.
I will definitely read through the rest of our post when I get time. But this issue isn’t related to the UI, or at least I don’t think it is since it’s not anything canvas related. I could be misunderstanding, though. I’ll read through your info more thoroughly this evening.
Here is the code I’m using to set the chaser to his first position. I should have mentioned that another reason why I used the distance joint is because the chaser will go through a few phases where he gets closer and further from the center of the screen.
// Get the center of the screen in viewport space
Vector3 centerViewport = new(0.5f, 0.5f, 0f);
// Get the left edge of the screen in viewport space
Vector3 leftEdgeViewport = new(0f, 0.5f, 0f);
// Convert to world space
Vector3 centerWorld = Camera.main.ViewportToWorldPoint(centerViewport);
Vector3 leftEdgeWorld = Camera.main.ViewportToWorldPoint(leftEdgeViewport);
// Calculate distance
float distanceToEdge = Vector3.Distance(centerWorld, leftEdgeWorld);
float phaseOneDistanceOffset = chaser.GetComponent<Renderer>().bounds.size.x / 8;
// Set the distance joint's initial distance from the center of the screen
phaseOneDistance = distanceToEdge - phaseOneDistanceOffset;
Posting code isn’t super-useful… debug it and find if it is generating the correct values you want it to. Press PLAY and then pause and print out all the values until you find out where the computation is going wrong.
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.
I see you using inputs to Camera.ViewportToWorldPoint() that have a zero z component. I’m not sure if you care in this case, but beware of what that might mean to the resulting values coming back: