Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Dfzoz 489 Moderation Voter
3 years ago
Edited 3 years ago

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

Log in to vote
2
Answered by
Dfzoz 489 Moderation Voter
3 years ago
Edited 3 years ago

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):

01local UIS = game:GetService("UserInputService")
02local rightclick = false
03UIS.InputBegan:connect(function(input,process)
04    if not process and input.UserInputType == Enum.UserInputType.MouseButton2 then
05        rightclick = true
06    end
07end)
08UIS.InputEnded:connect(function(input,process)
09    if input.UserInputType == Enum.UserInputType.MouseButton2 then
10        rightclick = false
11    end
12end)

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:

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

Answer this question