Making camera move with mouse

So i want from that perspective when i move my mouse the camera also moves slightly with me what do i need to do for it to get it working?

local cam = workspace.CurrentCamera

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


local cas = game:GetService("ContextActionService")


--
game:GetService("RunService").RenderStepped:Connect(function()
	
	cam.CameraType = Enum.CameraType.Scriptable
	
	cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(0, 20, 15), hrp.Position), 0.1)
end)

7 Likes

Hmm I think you could try using lerp to get like .25 or .5 between the character and the mouse. ill see if I can get something working!

3 Likes

Thank you already mate for trying to help me :smiley:

1 Like

Maybe you can rotate the Camera’s CFrame with CFrame * CFrame.Angles() by the mouse.X and mouse.Y

2 Likes

Im very new to scripting haha can you explain it for me better or make an example?

1 Like

Lets go! it looks really good lemmie know if you dont understand something I did.

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()

plr.CharacterAdded:Wait()

--local cas = game:GetService("ContextActionService")

game:GetService("RunService").RenderStepped:Connect(function()

	cam.CameraType = Enum.CameraType.Scriptable
	
	-----Positions
	
	local hrp = plr.Character:FindFirstChild("HumanoidRootPart")
	
	if not hrp then
		return
	end
	
	local mousepos = Mouse.Hit.Position
	local CharacterPosition = hrp.Position
	--------------------------------------
	
	-- You can replace this manually if ya like
	local MaxLength = 2
	
	-- there are a couple thins I should explain here:
	-- .Unit takes a Vector3 and turns it to the length of 1 ( Normalized vector ), .Magnitude is the length of the vector
	-- I wanted to cap the vector so i got the Normalized vector and multiplied it by a Clamped length ( limited )
	-- I then used this vector to lerp the direction from the character to the mouse ( ur camera Lookat position )
	
	local ChartoMouse = (mousepos - CharacterPosition).unit * math.clamp( (mousepos - CharacterPosition).Magnitude , 0, MaxLength)
	
	local Lookat = CharacterPosition:Lerp(CharacterPosition+ChartoMouse,0.75)

	cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(0, 20, 15),Lookat),0.1)
	
	-- It looks pretty good :D
	
end)
7 Likes

I just tested it:

but it didn’t work well

1 Like

So the thing is ive added it but it didn’t work for me. Do i need to add it with the old script or delete the old script and implement the one you send

1 Like

Well keep your old one just in case I put this new one in Starter player scripts.

3 Likes

Ah yes i see now but the thing is now it rotates a bit but i want to only to move on the X and Y axis you know? let me search for an example
https://gyazo.com/d46126ff0dd34ef49d3a8cff6d9acf54

1 Like

Oh so the camera wont go closer to the camera or further? I think I can make that possible id probably just fix the mouse position vector to keep the players z axis

3 Likes
-- I Changed a few things ( i added this next line underneath local CharacterPosition )
mousepos = Vector3.new(mousepos.X,CharacterPosition.Y,CharacterPosition.Z)

-- I made max length 15
-- Lookat lerp % is 0.2
-- Lerp % is 0.2 for the camera
3 Likes

Wait sorry that was a bad example. Maybe this one is a better example i just want it to move a little bit you know camera dont have to stay on the same posistion but no rotation

1 Like

Ohhh I see im making some food but I should be able to get this done in a bit!

3 Likes

Thank you so much man! If you need anything from me say it like a couple of bux or robux idk

1 Like

Couple changes I don’t remember what I changed though so here’s the entire script:

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()

plr.CharacterAdded:Wait()

--local cas = game:GetService("ContextActionService")

game:GetService("RunService").RenderStepped:Connect(function()

	cam.CameraType = Enum.CameraType.Scriptable

	-----Positions

	local hrp = plr.Character:FindFirstChild("HumanoidRootPart")

	if not hrp then
		return
	end

	local mousepos = Mouse.Hit.Position
	local CharacterPosition = hrp.Position

	mousepos = Vector3.new(mousepos.X,CharacterPosition.Y,CharacterPosition.Z)
	--------------------------------------
	local Unit = (mousepos-CharacterPosition).Unit
	local distance = math.clamp((mousepos-CharacterPosition).Magnitude,0,10) -- 10 is max side to side
	
	local ChartoMouse = CharacterPosition:Lerp(CharacterPosition+Unit*distance,0.25)

	cam.CFrame = cam.CFrame:Lerp(CFrame.new(ChartoMouse + Vector3.new(0, 20, 15),ChartoMouse),0.2)

	-- It looks pretty good :D

end)
2 Likes

It should all work now I forgot to add a characterposition thing lol

2 Likes

Ah sickk!! How can i add the Y axis? that is the only thing that is missing haha

1 Like

mousepos = Vector3.new(mousepos.X,CharacterPosition.Y,CharacterPosition.Z)
^^^
This line is whats locking the y and z axis
you can change Character Position for mouspos and it will unlock the axis

3 Likes

Yep that worked only i can move it a little bit do i need to add another line that will create the side to side with magnitude etc?