I want to make camera follow player mouse.
Hey there! Could you tell us what you’d like to achieve more clarity and what you already tried to make this? Make sure to follow the roblox dev forum template, so it’s easier for us to help you.
I want the look vector of the camera to be the same as mouse look vector
I believe there is already a post on this, not sure if this’ what you’d like to achieve?
Bind to Heartbeat event a function that sets the camera CFrame to CFrame.LookAt(), the position will remain, camera.CFrame.position, but the second parameter will be Mouse.Hit.Position
local RS = game:GetService("RunService")
local cam = game.Workspace.CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()
RS.Heartbeat:Connect(
function()
cam.CFrame = CFrame.LookAt(
cam.CFrame.Position,
mouse.Hit.Position
)
end
)
PD: Heartbeat is the option to follow, as RenderStepped and the bindToRenderStep() position on render scheme are before the part physics updates and animations complete this rendering frame
Through the use of basic CFrame functions, you can easily achieve this. You can use CFrame.lookAt(eye, target)
which constructs a new CFrame positioned at eye
looking at target
. The eye
in this case will be the camera’s Position and the target
being the mouse’s hit position. Since you want the camera to be constantly looking at where is positioned in 3D space, you need to update it every RenderStepped
(before a frame renders).
Basic usage:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local mouse = Players.LocalPlayer:GetMouse()
RunService.RenderStepped:Connect(function()
-- camera doesn't have a position property but you can get it via CFrame.Position or Vector3.new(camera.CFrame.X, camera.CFrame.Y, camera.CFrame.Z)
camera.CFrame = CFrame.lookAt(camera.CFrame.Position, mouse.Hit.Position)
end)
The code above will work but will not get you the most optimal results. What we can do is getting the direction endPosition - startPosition
and unitize it, then multiply it by a number which will be the speed.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local mouse = Players.LocalPlayer:GetMouse()
local direction
local speed = 7
RunService.RenderStepped:Connect(function(deltaTime)
direction = (mouse.Hit.Position - camera.CFrame.Position).Unit * 7
camera.CFrame = CFrame.lookAt(camera.CFrame.Position, direction)
end)
If you only want the camera to move on the X and Z axis of the mouse and since direction
is a vector, you can just multiply it by Vector3.new(1, 0, 1)
and then add the camera’s position to the direction. That way, the Y axis of direction
will be at the camera’s height which is what we want here.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local mouse = Players.LocalPlayer:GetMouse()
local direction
local speed = 7
RunService.RenderStepped:Connect(function(deltaTime)
direction = ((mouse.Hit.Position - camera.CFrame.Position) * Vector3.new(1, 0, 1)).Unit * speed
-- camera doesn't have a position property but you can get it via CFrame.Position or Vector3.new(camera.CFrame.X, camera.CFrame.Y, camera.CFrame.Z)
camera.CFrame = CFrame.lookAt(camera.CFrame.Position, camera.CFrame.Position + direction)
end)