How to detect camera direction(xyz) with distance

Hello,i’m making GMOD like game. So i need help with camera direction and the distance to where is the camera looking at.(srry my english isn’t that good)

current spawnscript:
robloxapp-20250108-1851475.wmv (1.3 MB)
what i want to do:
https://gyazo.com/dcfdf847530c767bc357b064c2e5b8f3
the script is(in the all spawn buttons):

local RS = game:GetService(“ReplicatedStorage”).SpawnLists
local spawnButton = script.Parent
local Players = game:GetService(“Players”)

spawnButton.MouseButton1Click:Connect(function()
local Humanoid = Players.LocalPlayer.Character:FindFirstChild(“HumanoidRootPart”)
local object = RS.Part:Clone() --edit the “object” to serverstorage name.
object:PivotTo(Humanoid.CFrame * CFrame.new(1,5,-10)) – you can edit this for how far the cloned object are away from the player.
object.Parent = game.Workspace.Clones

end)

2 Likes

Just do:

Camera.CFrame * CFrame.new(0,0,-1)
Replace -1 with some negative number as distance.

1 Like

i want to make when the part cloned i need the part to spawn on where is tha character looking at. ect: hes looking at the wallk then the part is spawns on wall

Then you need to do this:

local ray = workspace:RayCast(Camera.CFrame.Position,Camera.CFrame.LookVector*1024)
local Position = ray.Position

thanks!. Can u show me to how to do it on my script? im newbie so sorry*

uhh do you wanna do this in a server script or a local script?? because if its local you can just do game.Players.LocalPlayer:GetMouse().Hit.Position to get the position and from there you can get the direction and length from (game.Players.LocalPlayer:GetMouse().Hit.Position - workspace.CurrentCamera.CFrame.Position).Unit-- for direction and (game.Players.LocalPlayer:GetMouse().Hit.Position - workspace.CurrentCamera.CFrame.Position).Magnitude – for length

its a local script thanks for helping too ima try it uhh how can i do it on the cctual script
im really that newbie

can you plz describe how you’d want it to function first

hmm it doesnt matter for me if its not gonna change

give me a min, ill look at your script and see where you could put it

ill assume that you want the direction to be calculated every time the player’s gonna move the object soo just call it then

or do you want it to get spawned where the player is looking?

uh i dont meant it i want to object spawn where the character is looking surface

spawnButton.MouseButton1Click:Connect(function()
local Humanoid = Players.LocalPlayer.Character:FindFirstChild(“HumanoidRootPart”)
local object = RS.Part:Clone() --edit the “object” to serverstorage name.
local ray = workspace:RayCast(Camera.CFrame.Position,Camera.CFrame.LookVector*1024)
local Position = ray.Position
object:PivotTo(CFrame.new(Position)) – you can edit this for how far the cloned object are away from the player.
object.Parent = game.Workspace.Clones

end)

We aren’t allowed to give complete scripts but because you are a noob then ok.

i didnt make “camera” local thing,

Then make it? I am not suppose to do everything for you.

k ima look it on youtube idk to do it too lol

but thanks for the all of the helps

the way @awry_y did it here is the best if you want it to be adjucsted to the camera’s perspective but ill give you another version where it moves to your mouse.

local Humanoid = Players.LocalPlayer.Character:FindFirstChild(“HumanoidRootPart”)
local object = RS.Part:Clone() --edit the “object” to serverstorage name.
local mousepos = game.Players.LocalPlayer:GetMouse().Hit.Position
object:PivotTo(CFrame.lookAt(mousepos, Humanoid.Position))

object.Parent = game.Workspace.Clones

end)

and, also, if youre doing all this in a local script, it wont appear for the server. just mentioning it here.