#Unexpected camera rotation changes

1 messages · Page 1 of 1 (latest)

balmy hatch
#

hello i need help with my 51 lines of camera system, so when i toggle the Alternative move (by pressing F for the first time) it do change the Position but also the "Rotation" causing disproportion between default and alternative mode. You can test the script below:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 55

-- Settings
local default_offset = Vector3.new(0, 20, -20)
local alternative_offset = Vector3.new(0, 20, 0)
local offset = default_offset

local function onCharacterAdded(character)
local root = character:WaitForChild("HumanoidRootPart")
local renderConnection

renderConnection = RunService.RenderStepped:Connect(function()
    if not root or not root.Parent then
        renderConnection:Disconnect()
        return end
    
    local cameraPos = root.Position + offset
    camera.CFrame = CFrame.lookAt(cameraPos, root.Position)
end)

character.AncestryChanged:Connect(function(_, parent)
    if not parent then
        if renderConnection then
            renderConnection:Disconnect()
            renderConnection = nil
        end
    end
end)

end

local Alternative_mode = false
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.F then
Alternative_mode = not Alternative_mode
if Alternative_mode then
offset = alternative_offset
else
offset = default_offset
end
end
end)

player.CharacterAdded:Connect(onCharacterAdded)

vital pilotBOT
#

studio** You are now Level 1! **studio

uneven vessel
#
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 55

-- Settings
local default_offset = Vector3.new(0, 20, -20)
local alternative_offset = Vector3.new(0, 20, 0)
local offset = defaultoffset

local function onCharacterAdded(character)
    local root = character:WaitForChild("HumanoidRootPart")
    local renderConnection

    renderConnection = RunService.RenderStepped:Connect(function()
        if not root or not root.Parent then
            renderConnection:Disconnect()
            return end

        local cameraPos = root.Position + offset
        camera.CFrame = CFrame.lookAt(cameraPos, root.Position)
    end)

    character.AncestryChanged:Connect(function(, parent)
        if not parent then
            if renderConnection then
                renderConnection:Disconnect()
                renderConnection = nil
            end
        end
    end)
end

local Alternative_mode = false 
UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.F then
        Alternative_mode = not Alternative_mode
        if Alternative_mode then
            offset = alternative_offset
        else
            offset = default_offset
        end
    end
end)

player.CharacterAdded:Connect(onCharacterAdded)
#

hold on just putting it on format

balmy hatch
#

bro just removed some words 💀

uneven vessel
#

i just said im putting it in format

#

for me to read it

#

😭

#

💔

balmy hatch
#

oh

#

i though you copied it and tested in studio

#

sorry man

balmy hatch
uneven vessel
#

and/or what is the first argument?

balmy hatch
#

oh what i literarry copied the whole code by CTRL + A

uneven vessel
#

also i think you forgot to add the underscore in the offset, or is this just a snippet?

balmy hatch
#

let me resend

#

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 55

-- Settings
local default_offset = Vector3.new(0, 20, -20)
local alternative_offset = Vector3.new(0, 20, 0)
local offset = default_offset

local function onCharacterAdded(character)
local root = character:WaitForChild("HumanoidRootPart")
local renderConnection

renderConnection = RunService.RenderStepped:Connect(function()
    if not root or not root.Parent then
        renderConnection:Disconnect()
        return end

    local cameraPos = root.Position + offset
    camera.CFrame = CFrame.lookAt(cameraPos, root.Position)
end)

character.AncestryChanged:Connect(function(_, parent)
    if not parent then
        if renderConnection then
            renderConnection:Disconnect()
            renderConnection = nil
        end
    end
end)

end

local Alternative_mode = false
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.F then
Alternative_mode = not Alternative_mode
if Alternative_mode then
offset = alternative_offset
else
offset = default_offset
end
end
end)

player.CharacterAdded:Connect(onCharacterAdded)

#

this is the ful code

#

should be 51 lines

uneven vessel
#

oh nvm im the one whos tripping

#

there was two underscores that turned it into

#

this type of text

balmy hatch
#

wait i think i can't copy the "_" from the script. You know whats wrong right?

uneven vessel
#

is this whats supposed to happen?

balmy hatch
#

but when you toggle the alt mode, you will notice that the camera will be rotated

vital pilotBOT
#

studio** You are now Level 2! **studio

balmy hatch
#

causing disproportion as i said

#

i'm just a beginner scripter btw🙏 your help will really help me

uneven vessel
#
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 55

-- Settings
local default_offset = Vector3.new(0, 20, -20)
local alternative_offset = Vector3.new(0, 20, 0)
local offset = default_offset

local Alternative_mode = false 

local function onCharacterAdded(character)
    local root = character:WaitForChild("HumanoidRootPart")
    local renderConnection

    renderConnection = RunService.RenderStepped:Connect(function()
        if not root or not root.Parent then
            renderConnection:Disconnect()
            return
        end

        local cameraPos = root.Position + offset
        local lookTarget = root.Position

        if Alternative_mode then -- check if the alternative mode is on,
            local forward = root.CFrame.LookVector -- get the forward vector of the player
            lookTarget = root.Position + forward -- set the look target to the player's forward vector
        end
        
        -- after all of that just.. do it normally.
        camera.CFrame = CFrame.lookAt(cameraPos, lookTarget)
        print("cam rotation: ", camera.CFrame.LookVector)
    end)

    character.AncestryChanged:Connect(function(_, parent)
        if not parent then
            if renderConnection then
                renderConnection:Disconnect()
                renderConnection = nil
            end
        end
    end)
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.F then
        Alternative_mode = not Alternative_mode
        if Alternative_mode then
            offset = alternative_offset
        else
            offset = default_offset
        end
    end
end)

player.CharacterAdded:Connect(onCharacterAdded)
#

@balmy hatch

#

hiya!

#

The issue was because of the LookVector

#

^^

balmy hatch
#

wow thanks @uneven vessel i will test it out!

#

you might notice that at the 14th second, he toggled the second camera mode and thats what i want

uneven vessel
#

i see

#

so its just slightly rotated?

balmy hatch
#

In my case when I toggle the alternative mode. It seems like the camera rotated counterclockwise 90 degrees based on the default mode

uneven vessel
# balmy hatch Idk how to answer but it should be like in the video (sorry for the late reply)
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 55

-- Settings
local default_offset = Vector3.new(0, 20, -20)
local alternative_offset = Vector3.new(0, 20, -6)
local offset = default_offset

local function onCharacterAdded(character)
    local root = character:WaitForChild("HumanoidRootPart")
    local renderConnection

    renderConnection = RunService.RenderStepped:Connect(function()
        if not root or not root.Parent then
            renderConnection:Disconnect()
            return
        end

        local cameraPos = root.Position + offset
        local lookCFrame = CFrame.lookAt(cameraPos, root.Position)

        camera.CFrame = lookCFrame --* CFrame.Angles(math.rad(-10), 0, 0) --[[this is optional btw lol]]
    end)

    character.AncestryChanged:Connect(function(_, parent)
        if not parent and renderConnection then
            renderConnection:Disconnect()
            renderConnection = nil
        end
    end)
end

local Alternative_mode = false 
UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.F then
        Alternative_mode = not Alternative_mode
        if Alternative_mode then
            offset = alternative_offset
        else
            offset = default_offset
        end
    end
end)

player.CharacterAdded:Connect(onCharacterAdded)

the issue was actually.. quite simple...

#

to fix

#

..

#

💔

#

and i overcomplicated it

balmy hatch
#

WHAT DID YOU DO!?!?

uneven vessel
#

but overall

#

i just changed
alternative_offset = Vector3.new(0, 20,0)
to

#

alternative_offset = Vector3.new(0, 20, -6)

balmy hatch
#

Okay, I'll test it out!

uneven vessel
#

Hope this fixes your issue! ^^

balmy hatch
uneven vessel
#

Also just to explain,

When you set it to
Vector3.new(0, 20, 0)
your camera was positioned directly above the character, same X and Z as the target.
That makes the direction vector from camera -> root

#

(root.Position - (root.Position + Vector3.new(0, 20, 0))) = Vector3.new(0, -20, 0)
That’s perfectly vertical.

#

CFrame.lookAt() can’t build a stable rotational frame from a direction like (0, -1, 0) because there’s no clear “right” or “forward” direction.

#

by just introducing (0, 20, -1) it changes it, making it not perfectly horizontal

#

or even (0, 20 -0.01) wont make it perfectly horizontal

balmy hatch
#

Okay. Thanks for the help and the explaination mate! @uneven vessel

uneven vessel
#

No problem!