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

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

Answer this question