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

How do I make a movable camera?

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

✓ Accepted answer

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)

1 comment

0

Thanks, and I'll look into the keypress functions aswell.

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