Help with camera move to mouse pos

Hey guys recently I tried making my camera move to the mouse position x
I have no clue how to keep the camera on the x location though
can you script that for me?


(camera need to move to player mouse x position)

6 Likes

Local Script:

local mouse = game.Players.LocalPlayer:GetMouse()
local camera = game.Workspace.MyCamera

camera.CameraType = Enum.CameraType.Custom
wait(.1)
camera.CameraType = Enum.CameraType.Scriptable

camera.CFrame = mouse.Hit.Position – this is a CFrame

–Additionally if you want to return camera to player then:
camera.CameraType = Enum.CameraType.Custom

3 Likes

I don’t want my camera position to be the hit position

3 Likes
local mouse = game.Players.LocalPlayer:GetMouse()
print(mouse.X)
3 Likes

local mouse = game.Players.LocalPlayer:GetMouse()
local camera = game.Workspace.MyCamera

camera.CameraType = Enum.CameraType.Custom
wait(.1)
camera.CameraType = Enum.CameraType.Scriptable

while true do
wait(0.1)
local mouseCFrame = CFrame.new(mouse.X, mouse.Y, --Some number, mouse is in 2D so
it cannot have a Z position )
camera.CFrame = mouseCFrame
end

–Additionally if you want to return camera to player then:
camera.CameraType = Enum.CameraType.Custom

3 Likes
local camera = workspace.CurrentCamera
UserInputService.InputBegan:Connect(function(input, gp)
    if gp then return end

    if input.UserInputType == Enum.UserInputType.MouseMovement then
        local unitRay = camera:ViewportPointToRay(input.Position.X, input.Position.Y, 0)
        local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * 100)

        if not raycastResult then
            return
        end
        camera.CFrame = raycastResult.Normal * 50
    end
end)
2 Likes

won’t work for some reason, no errors tho

2 Likes

May you show your script so far? Also, what exactly do you mean by your ‘mouse position’? It could be the position on the screen relative to the top left or the middle, or it could be the hit cframe on the ground?

3 Likes

I just want my camera to follow my mouse position

2 Likes

What exactly do you mean by your ‘mouse position’?

Do you want the camera to slightly track the mouse on the screen?
Do you want it to continously move depending on the mouse positon relative to the middle of the screen?

Are you able to show a video of a game which uses the feature you want to make for your own game?

2 Likes

I want my mouse to move free and I want the camera to follow it

2 Likes


I would like it to be like that

2 Likes

This should be a LocalScript in StarterGui

Mouse.X will always return a number between 0-1000, so subtracting that number by 500 will give us a number between -500 and 500.

We can take that number and multiply it by a decimal to make it smaller (to get the amount of studs we move by).

So the closer the mouse is to the center of the screen, the slower it moves and the farther it is from the center, the faster it moves.

Then, we can move the position of the part by that many studs, and finally lerp the camera CFrame to the CameraPart position.

local cam = workspace.CurrentCamera

local CameraPart = workspace:WaitForChild("MyCamera") -- the part thats your camera.
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

repeat cam.CameraType = Enum.CameraType.Scriptable wait() until cam.CameraType == Enum.CameraType.Scriptable

CameraPart.Transparency = 1


game:GetService("RunService").RenderStepped:Connect(function()
	cam.CameraType = Enum.CameraType.Scriptable
	local speed = 0.001 -- make this bigger to make it move faster
	local studsToMove = (mouse.X-500)*speed -- formula for how much studs to move

	CameraPart.Position += Vector3.new(studsToMove,0,0)	
	cam.CFrame = cam.CFrame:Lerp(CameraPart.CFrame,0.1)
end)
4 Likes

Thank you so much man you really helped me out

2 Likes

Do in LocalScript:
(some of it did not turn into code)

local players = game:GetService(“Players”)

local player = players.LocalPlayer

local mouse = player:GetMouse()

local lastMouseX, lastMouseY = mouse.X, mouse.Y

mouse.Move:Connect(function()

if math.abs(mouse.X - lastMouseX) >             math.abs(mouse.Y - lastMouseY) then

	if mouse.X - lastMouseX > 0 then
		print("Mouse is moving right!")

                    camera.CFrame = camera.CFrame * CFrame.new(5,0,0)

	else
		print("Mouse is moving left!")

                    camera.CFrame = camera.CFrame * CFrame.new(-5,0,0)

	end
else
	if mouse.Y - lastMouseY > 0 then
		print("Mouse is moving down!")

                    camera.CFrame = camera.CFrame * CFrame.new(0,-5,0)

	else
		print("Mouse is moving up!")

                    camera.CFrame = camera.CFrame * CFrame.new(0,5,0)

	end
end
lastMouseX, lastMouseY = mouse.X, mouse.Y

end)

1 Like

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