How to rotate my custom camera?

I guess I really don’t understand CFrames and how they interact with Vector3s, but I literally can’t get my camera to just rotate to the left by 90 degrees. It always winds up tilted or just going the opposite direction. This is a top-down, over the shoulder camera. i.e:

Any help is appreciated!

local cam = workspace.CurrentCamera
local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")

cam.CameraType = Enum.CameraType.Scriptable
cam.Name = "PlayerCam"

local cameraPart = Instance.new("Part")
cameraPart.Transparency = 1
cameraPart.CanCollide = false
cameraPart.Parent = workspace
cameraPart.CFrame = CFrame.new(hrp.Position + Vector3.new(15,20,0), hrp.Position)

local bp = Instance.new("BodyPosition")
bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bp.Parent = cameraPart

game:GetService("RunService").RenderStepped:Connect(function()
	bp.Position = hrp.Position + Vector3.new(15,20,0)
    -- My failed Attempt at Rotating the camera
	cam.CFrame = cameraPart.CFrame * CFrame.Angles(0, math.rad(90), 0)
end)

It looks like cameraPart is free to rotate on it’s own. You might want to control it’s rotation if you are using it’s cframe for the camera.

Another approach you could try is to use the cameraPart solely for it’s position. You can use cam.CFrame = CFrame.LookAt(cameraPart.Position, hrp.Position) to create a cframe at the cameraPart, that is looking at the hrp.

I’m really not sure how to control the rotation of the cameraPart. I can’t rotate it on my own because it’s not anchored, and I can’t anchor it because then the camera won’t move. Unless I’m completely misunderstanding how this camera system is working.

This almost works, but it only rotates the camera to the left when the character model turns to the left (but it is a nice effect to add to the camera, so much appreciated for that). I need the camera, by default, to be rotated in the world space by about 90 degrees.

And I actually think that this is the problem. The orientation of the cameraPart is just wrong. And I’m not sure how to update that in relation to how I have it set up now. Ideally, I’d just rotate the cameraPart and everything else would fall in line with it since it’s attached to the cameraPart. I just can’t figure out how to rotate it while keeping the over-the-shoulder effect.

cameraPart.CFrame = CFrame.new(hrp.Position + Vector3.new(15,20,0), hrp.Position)
1 Like

Looks like I really did overcomplicate it. The issue was that the cameraPart was oriented the wrong direction, so I just changed the axis that I used in the Vector3 for it’s CFrame, and that fixed my problem (with the added look effect from @VitalWinter).

Before:

cameraPart.CFrame = CFrame.new(hrp.Position + Vector3.new(15,20,0), hrp.Position)
local bp = Instance.new("BodyPosition")
bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bp.Parent = cameraPart

game:GetService("RunService").RenderStepped:Connect(function()
	bp.Position = hrp.Position + Vector3.new(15,20,0)
	cam.CFrame = CFrame.lookAt(cameraPart.Position, hrp.Position)
end)

After:

cameraPart.CFrame = CFrame.new(hrp.Position + Vector3.new(0,20,-15), hrp.Position)
local bp = Instance.new("BodyPosition")
bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bp.Parent = cameraPart

game:GetService("RunService").RenderStepped:Connect(function()
	bp.Position = hrp.Position + Vector3.new(0,20,-15)
	cam.CFrame = CFrame.lookAt(cameraPart.Position, hrp.Position)
end)
1 Like

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