Camera :ToObjectSpace Flickering

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to achieve a freecam styled camera.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that when doing so, it works, but flickers.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried using lookVector, rightVector etc, I want to keep my code as compressed as possible and that isn’t really what I’m looking for. I think It could be changing the angle, since cframe carries rotation too. I’ve tried adding .Position, but to no luck.

https://gyazo.com/6fbc3330d9b1c5c36baab2d71b999a54
Ignore the mouse stuff, that’s for when this issue is fixed.

local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local UIS = game:GetService("UserInputService")

camera.CameraType = Enum.CameraType.Scriptable
UIS.MouseIconEnabled = false

local x, z = 0, 0
UIS.InputBegan:Connect(function(input, gp)
    if not gp then
        if input.KeyCode == Enum.KeyCode.W then
            z -= 1
        elseif input.KeyCode == Enum.KeyCode.S then
            z += 1
        end
        
        if input.KeyCode == Enum.KeyCode.A then
            x -= 1
        elseif input.KeyCode == Enum.KeyCode.D then
            x += 1
        end
    end
end)

UIS.InputEnded:Connect(function(input, gp)
    if not gp then
        if input.KeyCode == Enum.KeyCode.W then
            z += 1
        elseif input.KeyCode == Enum.KeyCode.S then
            z -= 1
        end
        
        if input.KeyCode == Enum.KeyCode.A then
            x += 1
        elseif input.KeyCode == Enum.KeyCode.D then
            x -= 1
        end
    end
end)

--[[local xAngle, yAngle = 0, 0
UIS.InputChanged:Connect(function(input, gp)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        xAngle = xAngle - input.Delta.x * 0.4
        yAngle = math.clamp(yAngle - input.Delta.y * 0.4, -90, 90)
    end
end)]] -- Ignore

game:GetService("RunService").RenderStepped:Connect(function()
    UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
    
    local delta = UIS:GetMouseDelta()
    camera.CFrame = camera.CFrame:ToObjectSpace(CFrame.new(x, 0, z))
end)

I think you meant ToWorldSpace. Or just do camera.CFrame *= CFrame.new(x, 0, z)

1 Like

I didn’t mean ToWorldSpace but *= seemed to have done the job.

ToWorldSpace and the * operator are equivalent :slight_smile: