Camera-following flashlight

I’m trying to make a flashlight that follows your camera on the Y axis, simply.

I don’t mean attaching a light to the client’s camera. I mean a flashlight tool, that points the light to where your camera is looking.

Another thing to mention, I want it to only follow the Y axis, so the camera looking up or down.

I already have the data to know how to do this:

print(workspace.CurrentCamera.CFrame.LookVector.Y)
--Prints 1 if you look up, -1 if you look down, and numbers in-between.

But I’m not sure how to make the flashlight point to the direction of that.

If you have any suggestions or solutions, please comment them down below.

(Put simply, I want the flashlight to point to the direction of my camera’s Y axis, so either up or down.)

4 Likes

Try this I added some comments as I made a script like this along time ago.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local flashlight = script.Parent -- Assuming the flashlight is a child of the tool
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.new(0, 4000, 0) -- Adjust torque as needed

local function updateFlashlightOrientation()
    local lookY = workspace.CurrentCamera.CFrame.LookVector.Y
    local newYRotation = math.deg(math.asin(lookY)) -- Convert Y-axis value to degrees

    bodyGyro.CFrame = CFrame.new(flashlight.Position) * CFrame.Angles(math.rad(newYRotation), 0, 0)
end

flashlight.Activated:Connect(function()
    if not bodyGyro.Parent then
        bodyGyro.Parent = flashlight
        game:GetService("RunService").Heartbeat:Connect(updateFlashlightOrientation)
    end
end)

flashlight.Deactivated:Connect(function()
    if bodyGyro.Parent then
        bodyGyro.Parent = nil
    end
end)

1 Like

And I put this script in ServerScriptService?

1 Like

Make this the child script of the flashlight tool.

It didn’t work. There was no movement whatsoever on my arm.

Sorry, not much I can do I apologize for my failing.

Nevermind, I made this and it works just how I want it to:

--The "axis" being the camera.CFrame.LookVector.Y
script.Parent.Event.OnServerEvent:Connect(function(player,axis)
	
	if player.Character and player.Character:FindFirstChild(script.Parent.Name) then
		player.Character.Torso["Right Shoulder"].C0 = CFrame.new(player.Character.Torso["Right Shoulder"].C0.Position) * CFrame.Angles(0,math.rad(90),math.rad(axis*70))
	else
		player.Character.Torso["Right Shoulder"].C0 = CFrame.new(player.Character.Torso["Right Shoulder"].C0.Position) * CFrame.Angles(0,math.rad(90),0)
	end
	
end)

Im going to be posting this solution onto the Community Tutorials page, because I haven’t seen any solutions like mine.

1 Like

Awsome! im glad I could be some help!

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