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

How would you change the camera's CoordinateFrame when you click on a part?

Asked by 10 years ago

I have tried the following inside a localscript, but it doesn't work. If anyone explains an answer to this problem, I would highly appreciate it! Thank you!

local cam = Workspace.Camera
function click(player)
    cam.CoordinateFrame=CFrame.new(29, 0.99, 9)
    wait(1)
end
script.Parent.ClickDetector.MouseClick:connect(click)

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

I am getting fed up with the terrible answers people give.

Wazap's answer is correct. Here is the explanation that he should have given when he took the time to post.

There are three reasons why your solution, dpark19285, doesn't work:

1) workspace.Camera is not actually anything meaningful. It refers to the camera of the server, I believe (which no one sees). You need to use workspace.CurrentCamera (which is not a child but actually a property of workspace see the wiki page)

2) CurrentCamera is only available to LocalScript's, so a normal script sitting in a part cannot actually do this. You would instead use a LocalScript in the StarterGui which listens for a click by the PlayerMouse on the particular part. (The equality he checks for nil is superfluous by the way)

3) Setting the CoordinateFrame may or may not work how you expect with the default CameraType. Setting it to "Scriptable" will let you completely control it, but the player will lose all control of their camera whatsoever. If you just want them to have their camera readjusted to point at something, setting the camera type may or may not be necessary; if you want their view to be locked onto some object, you should use "Scriptable".

Here is something that should work when used in a LocalScript:

local mouse = game.Players.LocalPlayer:GetMouse();

local cam = Workspace.CurrentCamera;

function click(part)
    cam.CameraType = "Scriptable"; -- Potentially optional
    cam.CoordinateFrame = CFrame.new(29, 0.99, 9);
    wait(1)
end

local clickedPart = ...;
-- game.Workspace.Button or whatever it is, some reference to it
-- for clickedPart

mouse.Button1Down:connect(function()
    if mouse.Target == clickedPart then
        click(mouse.Target)
    end
end);

0
YES! Thank you very much for the explanation! dpark19285 375 — 10y
Ad
Log in to vote
0
Answered by
wazap 100
10 years ago

In StarterGui

local mouse = game.Players.LocalPlayer:GetMouse()
local cam = Workspace.CurrentCamera
function click(part)
    cam.CameraType="Scriptable"
    cam.CoordinateFrame=CFrame.new(29, 0.99, 9)
    wait(1)
end
mouse.Button1Down:connect(function() if mouse.Target and mouse.Target == PathToObjectWithClickDetector then click(mouse.Target) end end)

Answer this question