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

Camera movement question. How can i do it?

Asked by 5 years ago

I want to make something that when a play touches a part or clicks a button , their camera moves to an exact position, and they can't move it, and when they press a button their camera reverts to normal.

0
You're lucky you got help because requesting is not allowed, Ady LordTechet 53 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
wait() --Waits for player to load

function MoveCamera(cframe, button)
    local prevCFrame = game:GetService("Workspace").Camera.CFrame
    local camType = game:GetService("Workspace").Camera.CameraType

    game:GetService("Workspace").Camera.CameraType = Enum.CameraType.Scriptable --Disables player moving camera

    game:GetService("Workspace").Camera.CFrame = cframe --Set camera position

    buttonEvent = button.MouseButton1Click:connect(function() --Change 'MouseButton1Click' to 'MouseClick' if using ClickDetector
        game:GetService("Workspace").Camera.CameraType = camType
        game:GetService("Workspace").Camera.CFrame = prevCFrame
        pcall(function() --Incase of error
            buttonEvent:Disconnect() --Disconnects event from function so it only runs once
        end)
    end)
end

--e.g. MoveCamera(CFrame.new(0, 10, 0), GuiButton)

edit:

local prevCFrame = game:GetService("Workspace").Camera.CFrame
local camType = game:GetService("Workspace").Camera.CameraType

Stores the players camera data.

game:GetService("Workspace").Camera.CameraType = Enum.CameraType.Scriptable

Removes the ability for the player to move the camera.

game:GetService("Workspace").Camera.CFrame = cframe

Sets the camera cframe to the one used in the function.

game:GetService("Workspace").Camera.CameraType = camType
game:GetService("Workspace").Camera.CFrame = prevCFrame

Sets the camera data back to before it was changed, fixes the camera.

buttonEvent:Disconnect()

Disconnects the event so it cannot run again.

Hopefully the edit helps.

0
pls explain not in comments User#24403 69 — 5y
Ad

Answer this question