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

How do you move a part relative to the camera?

Asked by
wackem 50
8 years ago

Like the part is rotated in the same direction as the camera, and it moves away from it in the direction it is looking

0
I do believe further explanation is required. User#11440 120 — 8y

1 answer

Log in to vote
0
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago

You could position your part according to the Camera's CFrame property. A CFrame is value which represents a point and rotation in 3D space. The Camera's CFrame thus represents the position and rotation of the camera (not to be confused with its Camera.Focus property which represents the point the camera is looking at. Unfortunately, this CFrame is not rotated along with the camera, so we cannot use it to have the part face it).

You can retrieve the camera through the game.Workspace's CurrentCamera property but only in a localscript, since the camera is different for every player and for this same reason does not need to be replicated to the server.

E.g:

local camera = game.Workspace.CurrentCamera

local part = game.Workspace:FindFirstChild("Part")


while true do
    --[[
        'Multiplying' two CFrames together is like transforming the second
        CFrame from Object coordinates, coordinates relative to a point,
        to World coordinates -- absolute coordinates, i.e: coordinates
        relative to the Workspace's origin (0,0,0).
    ]]--

    part.CFrame = camera.CFrame * CFrame.new(Vector3.new(0, 0, -10))
    -- Here we set the part's position to be ten studs away from the camera's
    -- position. 

    wait(0.01)
end


Ad

Answer this question