Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a function fire when the camera moves?

I have a viewportframe that has an arrow mesh indicating wind direction (Vector3) relative to the camera. I use renderstep to update it smoothly, but I want to make the code fire only when the camera is actually moving, so it doesn't run when it's not needed.

Is there a way to know when a camera starts or stops moving?

I tried looking for UserInputService, the camera Object, without results.

1 answer

Guess I found out a way. Sorry for the post.

You can use UserInputService.InputBegan and InputEnded to check if the player is holding the mousebutton2 (right one):

local UIS = game:GetService("UserInputService")
local rightclick = false
UIS.InputBegan:connect(function(input,process)
	if not process and input.UserInputType == Enum.UserInputType.MouseButton2 then
		rightclick = true
	end
end)
UIS.InputEnded:connect(function(input,process)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		rightclick = false
	end
end)

After this you use UserInputService.InputChanged to check if the player moved the mouse and then, if he is holding the mousebutton2, do the code:

UIS.InputChanged:Connect(function(input,process)
	if rightclick == true and input.UserInputType == Enum.UserInputType.MouseMovement then
		--code here
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.