Help having part follow a character but stay in the center of the view

Ok, so this might be hard to explain so try to bare with me What I am doing, is trying to make a local script that has a part follow a character, keeping them in the center of the view because it is a camera. The images below will show the 2 things I would like to go for but don’t understand how to use the math to make the part move how I want it to, I would appreciate help on either/both of them.image 1st thing; starting position
image 1st thing; ending position
In this situation, the green represents the axis for the block and that front continues to look at the noob while only being able to look up and down (to adjust when the player jumps) rather then being able to turn left and right. It will maintain the same angle no matter how much to either side it slides, always calculating how far to move to look at the player at the same angle left-right wise (but the Y will still continue to move)
image 2nd thing; start position
image 2nd thing; end position
In this example, the brick can rotate left and right as well as up and down and still slides along the original axis, but this time, instead of maintaining an angle perpendicular to its original axis, it maintains the same z axis as the player.
If you have any questions about what I mean feel free to ask but I just am not really that good with CFrame and math so any help is appreciated.

So you’re trying to make like a camera dolly system where the position can only move on one axis while keeping the character in the center of the view? Is that correct?

1 Like

Yes, this is precisely what I am trying to do.

For the positioning part, you’ll have to define some sort of axis. This can be tricky for directions that aren’t directly on the XYZ axes, so I’d suggest picking one of those. In the original post you mentioned the z-axis, so I’ll use that as an example. All you have to do is use the character’s z position as the camera’s z position, the x and y are up to you. That might look something like this:

local cameraPosition = Vector3.new(5, 5, character.HumanoidRootPart.Position.Z)

Then you need to orient the camera at the character, which is easy using CFrame.lookAt.

local cameraCFrame = CFrame.lookAt(cameraPosition, character.HumanoidRootPart.Position)

This will create a CFrame at the defined camera position facing the character. Now you just have to apply the CFrame to the camera, whether it’s on the render step or just on a loop.

but this position script couldnt work because that “5,5” in the vector 3 doesnt actually follow the rightvector values of the cframe, instead it will just go upon world position instead of moving in a predefined line.

If you want to use a custom axis other than a world axis, you can take advantage of object and world space. I slapped together something quick to demonstrate how you could use any axis using this method:

local cameraAxis = CFrame.Angles(0, math.rad(45), 0)

game:GetService("RunService").RenderStepped:Connect(function()
	local character = game.Players.LocalPlayer.Character
	if character and character.PrimaryPart then
		local cameraPosition = cameraAxis:PointToWorldSpace(Vector3.new(5, 5, cameraAxis:PointToObjectSpace(character.PrimaryPart.Position).Z))
		local cameraCFrame = CFrame.lookAt(cameraPosition, character.HumanoidRootPart.Position)
		workspace.CurrentCamera.CFrame = cameraCFrame
	end
end)

The two 5s in the Vector3 are just my test numbers that define an offset from the world center so that the camera won’t be in the ground. Make these whatever you like.

2 Likes

Worked exactly as intended, thank you very much!

1 Like