How to keep object centered within a set screen space

I have a character that’s located on the left side of the screen. I want the camera to constantly focus in on this character, so regardless of zoom value, the character will always be centered. At the moment when I zoom, it becomes awkward and offset
ezgif.com-video-to-gif - 2023-07-18T224348.401

RunService:BindToRenderStep("AvatarCamera", Enum.RenderPriority.Camera.Value - 1, function()
	Camera.CFrame = workspace.AvatarEditor.Camera.CFrame
		+ Vector3.new(0, Height.Value, 0)
		+ Vector3.new(Zoom.Value, 0, 0)
end)

Setup

  • Red part is the character spawn
  • Black part is the camera location

As you can see, the character is offset to the left here too, to account for the UI on the right side of screen

1 Like
Camera.CFrame = CFrame.new(workspace.AvatarEditor.Camera.Position,CharacterRootPart.Position) + Vector3.new(0, Height.Value, 0) + Vector3.new(Zoom.Value, 0, 0)

The second argument of CFrame is lookAt, which is the Vector3 position the CFrame will be facing to. CharacterRootPart is just an example name, you can make the CFrame look at the red part instead.

I’ve tried this and the issue is that’ll centre the character in the camera. Half the screen is occupied by UI tho. I need a way so the camera only takes into account the portion of screen that isn’t being occupied by UI

take a look at that

Ooh, I thought that you were using a viewport frame, oops. I think that maybe you can make it so the camera moves back and forth at an angle?

I think with this method you’ll have to find the sweet spot so the character will stay in the center of the portion of the screen. You can try making a CFrame that is facing the position of the character and changing its position based on the zoom level. Then take only the CFrame’s position and apply it to the camera.

Trying this, but 1. it seems to severaly distort the image for some reason and 2. I can’t figure out what size and position should be. If it’s supposed to be the size of the area on the screen, then I’ve tried that, but no use

Resize.Position = UDim2.fromScale(0, 0)
Resize.Size = UDim2.fromScale(0.45, 1)

This is the size of open area
character is still centre of screen and severely squashed

Because you resized the X down to 45% but left Y at 100% so of course it looks squashed because that’s exactly what you asked it to do. If you wanted it to keep the aspect ratio then you need X and Y scale to be the same, like UDim2.fromScale(0.45, 0.45).

I managed to figure it out after a lot of trial and error eventually :+1:

Not sure where else to post this, but I recently came across the resize module you’re using, and everything works fine except for the fact that it breaks on smaller screens. No idea how to fix this.

(SunRays also break, but that I can live without.)


EDIT: Setting Camera.FieldOfViewMode to MaxAxis and adding this line to Module._getOffset() seems to fix this for the most part:

x = math.clamp(x, -0.663, 0.663)

Note that using MaxAxis can result in unwanted changes to the FieldOfView if the user is able to resize their screen, so it’s best to reset it when the module is disabled.
(Set Camera.FieldOfView back to 70 and Camera.FieldOfViewMode back to Vertical)

3 Likes