How to change the distance between the camera and camera subject?

I need to change the distance between the camera and camera subject on a non-player camera; I’m guessing by using math to calculate where the camera’s CFrame should be. This is a relatively simple request, but the other Devforum posts I’ve seen aren’t what I need, and I’m too stupid to do this myself. My brain just shuts off when math and programming combine.
Context: How Could I Make A Model Fit Inside A Viewportframe? - #8 by BanTech

You could try with a negative lookvector.

camera.CFrame + camera.CFrame.LookVector * -Number

Let me know if it works properly, I’m not home rn.

1 Like

What property would I be setting? I tried setting the LookVector property, it gave me an error. I tried setting the CFrame property, and nothing happened. Sorry for not being able to figure out this myself. This is my first day messing with CFrame and manipulating cameras.

What’s the error? Show me the output results.

LookVector cannot be assigned to

Perhaps like this

Camera.CFrame = Camera.CFrame + (Camera.CFrame.LookVector * negative value)

Yeah, I just thought about that a second ago. Nothing happens. If this extra info helps, I’m using a camera for a ViewportFrame.

Oh wait, you want a viewport frame camera? My my, this could be helpful.

Reminding you that ViewportFrames render the view again, which is pretty resources requiring.

Oh, by the way, this camera is rendering nothing but the stuff in the ViewportFrame. I’m not trying to make a security camera system. I don’t know if that changes anything, but I thought it’d be useful info, just in case.

Mmh, could you give a more concrete example then? I’d like to find a solution for you without giving you non needed information for hours.

I’m making a custom backpack GUI that renders the tools in the slots. I want the camera to adjust it’s position depending on the tool size. I already have some of the code to do this, but I need to figure out how to set the distance between the camera and the tool.
image

Oh, you could use GetExtentsSize() I think.
local PrimaryPart = Model.PrimaryPart

local Size = Model:GetExtentsSize() -- Get the extents size of the model

local LongestAxisSize = math.max(Size.X, Size.Y, Size.Z) -- Get the largest number whether it be the X, Y, or Z number.

local Distance = LongestAxisSize * 2 -- The higher the second number, the farther away the camera will be from the part.

-- Set the CFrame of the camera (Position, lookAt).
-- Position is a Vector3 value that decides where your camera will be at. I first set the base position then add the offset in the front direction of the primary part, then I multiply it by however much is needed.
-- lookAt is a Vector3 value that has your camera look at the Vector3 position.
Camera.CFrame = CFrame.new(PrimaryPart.Position + (PrimaryPart.CFrame.LookVector * LongestAxisSize * 2), PrimaryPart.Position)
1 Like

This actually helped me as well, I never thought of using it for my own viewport needs, so I owe you a thank you, ahah @s1muIate

Does it work for you?

1 Like

Nah, I owe you way more. This backpack GUI wouldn’t have been possible without you. I don’t know if it works for me yet. I’m still writing code to make it work with my system. I also think I might have seen your reply late, so that’s why it’s taking so long.

1 Like

It feels so good to see the backpack GUI finally looking nice. Thank you so, so much. You’re seriously a live saver. I don’t think anyone else on the Devforum would put up with how bad I am at requesting help correctly. Oh, and after you update the camera, you need to set the ViewportFrame’s CurrentCamera property for it to update.

1 Like

Thank you for the kind words, and you’re welcome no problem.

For once I’d rather see a person in need actually get help instead of receiving 0 replies.

1 Like

Can I add you on Discord? I want to link you the Devforum post of my backpack GUI resource when it releases. I need to repay you somehow, and I think that this will be a good resource once it’s finished. Now that I think about it, this probably counts as spam under the Devforum guidelines, so let’s take this to DMs.

1 Like