Hello Everybody! I currently have a Build Mode System I am working on and basically the camera currently has few controls which include moving it back and fourth. And rotating the Camera using the mouse. Both work fine, but when both are being used at the same time, when the input ends it seems to move the camera the opposite direction of the camera (Please note their is a input ended function that does handle when input ends) The code is down below, KEEP IN MIND IT ISN’T THE FULL SCRIPT, thanks!
-- Variables
local WPressed, SPressed = false, false
local APressed, DPressed = false, false
local CFrameValue = CFrame.new()
local CameraAngle = 0
local Camera = workspace.CurrentCamera
--- RenderStepped Function
RS.RenderStepped:Connect(function()
GetClosestE()
if ClosestPart ~= nil then
if IsBuilding.Value == true and BuildManager.BuildMode("CheckBarrier", ClosestPart.Parent, Camera.CFrame * CFrameValue) == true then
Camera.CFrame = Camera.CFrame * CFrameValue * CFrame.Angles(0, CameraAngle, 0)
end
end
end)
-- Input Began
UIS.InputBegan:Connect(function(Input, Gameproccessed)
if not Gameproccessed and IsBuilding.Value == true then
if Input.KeyCode == Enum.KeyCode.W and WPressed == false or Input.KeyCode == Enum.KeyCode.Up and WPressed == false then
WPressed = true
CFrameValue = CFrameValue + Camera.CFrame.RightVector * -0.15
elseif Input.KeyCode == Enum.KeyCode.A and APressed == false or Input.KeyCode == Enum.KeyCode.Left and APressed == false then
APressed = true
CFrameValue = CFrameValue + Camera.CFrame.LookVector * -0.15
elseif Input.KeyCode == Enum.KeyCode.S and SPressed == false or Input.KeyCode == Enum.KeyCode.Down and SPressed == false then
SPressed = true
CFrameValue = CFrameValue + Camera.CFrame.RightVector * 0.15
elseif Input.KeyCode == Enum.KeyCode.D and DPressed == false or Input.KeyCode == Enum.KeyCode.Right and DPressed == false then
DPressed = true
CFrameValue = CFrameValue + Camera.CFrame.LookVector * 0.15
end
end
end)
--- Input Ended
UIS.InputEnded:Connect(function(Input, Gameproccessed)
if not Gameproccessed and IsBuilding.Value == true then
if Input.KeyCode == Enum.KeyCode.W and WPressed == true or Input.KeyCode == Enum.KeyCode.Up and WPressed == true then
WPressed = false
CFrameValue = CFrameValue - Camera.CFrame.RightVector * -0.15
elseif Input.KeyCode == Enum.KeyCode.A and APressed == true or Input.KeyCode == Enum.KeyCode.Left and APressed == true then
APressed = false
CFrameValue = CFrameValue - Camera.CFrame.LookVector * -0.15
elseif Input.KeyCode == Enum.KeyCode.S and SPressed == true or Input.KeyCode == Enum.KeyCode.Down and SPressed == true then
SPressed = false
CFrameValue = CFrameValue - Camera.CFrame.RightVector * 0.15
elseif Input.KeyCode == Enum.KeyCode.D and DPressed == true or Input.KeyCode == Enum.KeyCode.Right and DPressed == true then
DPressed = false
CFrameValue = CFrameValue - Camera.CFrame.LookVector * 0.15
end
end
end)
--- Mouse Script
local MouseMove
Mouse.Button2Down:Connect(function()
if IsBuilding.Value == true then
MouseMove = Mouse.Move:Connect(function()
UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
CameraAngle = UIS:GetMouseDelta().X/4700
end)
local Connection
Connection = Mouse.Button2Up:Connect(function()
MouseMove:Disconnect()
CameraAngle = 0
UIS.MouseBehavior = Enum.MouseBehavior.Default
Connection:Disconnect()
end)
end
end)
I’m not referring to my first response regarding your memory leak issue (which was a huge problem), I’m referring to my second response about how you need to be calculating the destination CFrame within the event loop and not just when the key is first pressed (such that it dynamically updates as the camera is rotated).
I’m referring to these calculations, they need to be performed as frequently as possible (as the camera’s CFrame changes), so the camera’s initial RightVector (when the key is first pressed) is going to be different to the camera’s end RightVector (when the key is released) providing the camera has rotated between the two actions.