Camera Always Faces Origin

Hello Developers,

So I have a script where the camera is positioned to a node to face something and a function to add like a camera following the mouse effect, the problem is that the camera always faces the origin no matter where I put the node it always faces the origin Ill provide examples below

Expected view:

Outcome:

Clip of camera forcing to look at the origin:

Relevant Code:

	-- Scene Camera Nodes 
	local LoadingScreen_Nodes: Folder = workspace:WaitForChild("LoadingScene_Nodes")
	local RandomSceneNode: BasePart = LoadingScreen_Nodes:GetChildren()[math.random(1, #LoadingScreen_Nodes:GetChildren())]
	local Mouse: Mouse = LPlayer:GetMouse()
	
	-- Prepare Camera for Scene
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = RandomSceneNode.CFrame
	
	-- Camera Follow at Mouse 
	CameraConnection = RunService.RenderStepped:Connect(function(deltaTime)		
		-- Get Look Position and Set 
		local Direction = (Mouse.Hit.Position - Camera.CFrame.Position).Unit * 50	
		Camera.CFrame = Camera.CFrame:Lerp(CFrame.new(Camera.CFrame.Position,Direction),.1)
	end)
2 Likes

Are you sure that this is the only code that is moving the camera? From the video it looked like there might be something else moving the camera.

It looks like you are setting the position of the camera directly where your “node” is at.

I would specify an origin position and then get the CFrame from that. Then if you still want to follow the mouse then you can apply a transform to that CFrame.

For example, I made a similar camera during the pandemic.

local MouseLocation = (-UserInputService:GetMouseLocation() + VIEWPORT_CENTER)/8	
local ScreenPart = MMC.Screens[MMC.Screen].Part
local Origin: CFrame = CFrame.lookAt(
	ScreenPart.Position + (ScreenPart.CFrame.LookVector * 13),
	ScreenPart.Position
)
Camera.CFrame = Camera.CFrame:Lerp(
	Origin * CFrame.new(0,0,0,rad(MouseLocation.Y),rad(MouseLocation.X),0,10),
	clamp(DeltaTime * 5,0,1)
)

The second piece of code is run every frame.
As you can see I am only rotating the original.

I also need to recreate the origin every time I change screen.

2 Likes

image

I thought a drawing may help.

| a → camera cframe | b → node cframe | c → b - a | u → unit c * n |

CFrame.new() with a second argument is the old way to define CFrame.lookAt(). If Direction, respectively u is the ‘look at’ position, it’s always originating from the origin of the coordinate system.

So we need camera’s position + direction. But because that comes without limits, we would have to clamp the angles.

If you don’t mind, I found a slightly easier approach to achieve the same effect, based on the screen mouse location.

local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local LocalPlayer = game:GetService("Players").LocalPlayer
local Camera = workspace.CurrentCamera
local Mouse = LocalPlayer:GetMouse()

local LoadingScene_Nodes: Folder = workspace.LoadingScene_Nodes
local RandomSceneNode: BasePart = LoadingScene_Nodes:GetChildren()[
	math.random(1, #LoadingScene_Nodes:GetChildren())
]

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CameraSubject = nil
Camera.CFrame = RandomSceneNode.CFrame

RunService:BindToRenderStep("SceneCamera", Enum.RenderPriority.Camera.Value +1, function(dt)
	local MouseLocation = UIS:GetMouseLocation()
	local NewCFrame = RandomSceneNode.CFrame * CFrame.fromEulerAnglesXYZ(
		math.rad(((MouseLocation.Y - Camera.ViewportSize.Y *.5)/Camera.ViewportSize.Y) * -20),
		math.rad(((MouseLocation.X - Camera.ViewportSize.X *.5)/Camera.ViewportSize.X) * -15),
		0
	)
	Camera.CFrame = Camera.CFrame:Lerp(NewCFrame, .1)
end)
2 Likes

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