Help with CFrame

Hey!

I do not know too much of CFrames, so, probably, this question will be simple.

How can I get the CFrame of this flashlight based on player’s camera CFrame?
imagem_2022-12-28_184310089

1 Like

Do you want to know where to put the flashlight relative to the players camera?

Yes! I writed “based” but I do not know if this is the correct term, sorry, English is not my native language :sweat_smile:

Everything is defined relative to World Space initially. Every Space has a left right and up direction (it can be any 3 directions) and changing these creates spaces that are rotated relative to eachother, they can also be offset in position. To place your flashlight you need World Space Coordinates, since thats the space all objects are placed in, in the Workspace. If you have a position relative to your camera CFrame, you need to convert using the :PointToWorldSpace() method of CFrame:

cameraWorldCFrame = currentCamera.CFrame:PointToWorldSpace(cameraRelativePosition)

In general, this function tells you where a relative point falls in world space given its position relative to the CFrame on the left side (currentCamera.CFrame in this case). There is also :VectorToWorldSpace() which only affects the rotation, this is used for direction vectors, since directions have no concept of starting position.

1 Like

If you’d like to see where the light from the flashlight hits, use Camera.CFrame.LookVector.

If you’d like to know the position of the flashlight relative to the position of the camera, I believe you could use:
local ObjectRelative = Camera.CFrame:ToObjectSpace(FlashlightThing.CFrame)
local WorldRelative= Camera.CFrame:ToWorldSpace(ObjectRelative) --( To convert it back to world space )

1 Like

This works! Thank you so much!

Thank you so much for your help!

(The CFrame of this flashlight should be the same as the CFrame of the camera, but with the same rotation, and rotated around the X axis a little)
local flashlight = Instance.new(“PointLight”, game.Players.LocalPlayer.Character.Torso)
flashlight.Enabled = true
flashlight.Brightness = 0.5
flashlight.Range = 20
flashlight.Color = Color3.new(1, 1, 1)

You can do the following:
local myOffset = CFrame.new(0, 0, 0)
flashlight.CFrame = game.Players.LocalPlayer.Character.Torso.CFrame * myOffset

The CFrame of the flashlight will then be the CFrame of the player torso, plus the offset you specified.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.