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

How do I make a movable camera?

Asked by 8 years ago

So I've been trying to make a camera that is controllable by the arrow keys (kinda like League of Legends). But in my endeavor to create this glorious camera I have ran into a problem, I don't know how to check when the key isn't being pressed anymore, so I don't know when to break. ( I also plan on copying the function "onRightKeyPress" for Left, up, & Down.)

local cameraPiece = workspace.CameraPart
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = cameraPiece
    camera.CoordinateFrame = CFrame.new(cameraPiece.Position)
                           * CFrame.Angles(-45, 0, 0)
                           * CFrame.new(0, 5, 0)       

function onRightKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.Right then
        while inputObject.KeyCode == Enum.KeyCode.Right do
            print("Right was pressed")
            workspace.CameraPart.CFrame = workspace.CameraPart.CFrame+Vector3.new(50,0,0)
            camera.CoordinateFrame = CFrame.new(cameraPiece.Position)
                                   * CFrame.Angles(-45, 0, 0)
            wait(0.1)
        end
    end
end

game:GetService("UserInputService").InputBegan:connect(onRightKeyPress)

I have also tryed the IsKeyDown function, but it says it isn't in my library

thanks to anyone who can help

1 answer

Log in to vote
0
Answered by 8 years ago

I've never used UserInputService before - I've always used key press functions from the Mouse object (yes it does more than Mouse movement detection)

Nonetheless, I've figured out a solution by setting up an InputEnded event - here's my modifications to your code (I'm not sure if the camera will work or not, but I did get the input working - you might make some tweaks to the wait(.1):

local cameraPiece = workspace.CameraPart
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = cameraPiece
    camera.CoordinateFrame = CFrame.new(cameraPiece.Position)
                           * CFrame.Angles(-45, 0, 0)
                           * CFrame.new(0, 5, 0) 


local inputEnded=false
function onRightKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.Right and inputEnded==false then
        while inputObject.KeyCode == Enum.KeyCode.Right and inputEnded==false do
            print("Right was pressed")
           workspace.CameraPart.CFrame = workspace.CameraPart.CFrame+Vector3.new(50,0,0)
            camera.CoordinateFrame = CFrame.new(cameraPiece.Position)                     * CFrame.Angles(-45, 0, 0)
            wait(0.1)
       end
    end
end

game:GetService("UserInputService").InputBegan:connect(onRightKeyPress)
game:GetService("UserInputService").InputEnded:connect(function()
    inputEnded=true -- breaks your while loop
    wait(0.1)
    inputEnded = false -- reset, and ready to go again...
end)
0
Thanks, and I'll look into the keypress functions aswell. dlange95 15 — 8y
Ad

Answer this question