Camera in a tool ; Follow the part and rotate the player

Hello !

I’m currently making binoculars for my game. Everything work well, instead of a very important point ; The camera subject. I had to set the camera subject to a part that is in front of my binoculars, so the player don’t see binoculars in first person, but get a view on the front as I can put a gui after that.

The thing I want to do with that camera is that the camera should be fixed straight toward the part, and make the player rotate with it with the camera when you move your mouse. I don’t want to see up and down, nor right and left… I just want the camera to be fixed on this part, and make my player follow the mouse.

Here is the problem ;

https://streamable.com/3s8x8f

As i’m sure that you don’t really see what I clearly mean, here is an example. When i’m in first person, the tool follows player rotation. So I want it like that, but with the camera set on the part on the front.

https://streamable.com/5c0khv

And the code ;

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local binocular = script.Parent.Zeiss
local Camera = workspace.Camera

local ZoomerInFOV = 40
local DefaultFov = workspace.Camera.FieldOfView

-- Animations ; --------------------------------------

local anim1 = h:LoadAnimation(script.Parent.Animations:WaitForChild("A1"))
local anim2 = h:LoadAnimation(script.Parent.Animations:WaitForChild("A2"))
local anim3 = h:LoadAnimation(script.Parent.Animations:WaitForChild("A3"))
local anim4 = h:LoadAnimation(script.Parent.Animations:WaitForChild("A4"))
local anim6 = h:LoadAnimation(script.Parent.Animations:WaitForChild("A6"))

------------------------------------------------------
local state = true
local tool = script.Parent

tool.Equipped:Connect(function()
	binocular.Transparency = 1
	anim6:Play()
	wait(1.24)
	anim6:Stop()
	binocular.Transparency = 0
	anim1:Play()
end)

tool.Activated:Connect(function()
	
	
	
	if state == true then
		
		anim1:Stop()
		anim2:Play()
		wait(0.18)
		anim2:Stop()
		anim3:Play()
								
		Camera.FieldOfView = ZoomerInFOV
		
		workspace.CurrentCamera.CameraSubject = script.Parent.Part
		workspace.CurrentCamera.CameraType = Enum.CameraType.Track
		
		state = false		
	else
		
		anim3:Stop()
		anim4:Play()
		wait(0.18)
		anim4:Stop()
		anim1:Play()
	
		
		Camera.FieldOfView = DefaultFov
		
		state = true
		
	end
	
	
	
end)

tool.Unequipped:Connect(function()
	anim1:Stop()
	anim2:Stop()
	anim3:Stop()
	anim4:Stop()
	anim6:Stop()
	
	Camera.FieldOfView = DefaultFov
	
end)

make a update every frame and check if the player is using the binoculars and if so, set the cameras cframe to the parts cframe. make sure to also set the cameras cameratype to scriptable

Honestly, I think you could do this:

-- Code when binoculars are equipped:
player.Character.Humanoid.CameraOffset = Vector3.new(0,0,40) -- If I remember correctly this puts the camera infront of the player. If this puts the camera to the side of the player or above or below the player try experimenting with stuff like Vector3.new(0,40,0) or Vector3.new(40,0,0)
-- Code when binoculars are unequipped:
player.Character.Humanoid.CameraOffset = Vector3.new(0,0,0)

This is a really short and sweet snippet of code I made that should work.

4 Likes

Yes, it does work ! I thought about it, but didn’t knew if it was possible or not. Thank you very much !