How would I detect what way (x,y,z) a player is facing?

Trying to make my own gun system, but how would I detect what way the gun should shoot?

No matter what, it only shoots one way: https://gyazo.com/c15a2f0ce8f67c17ae694f77b88a8c2c - Which is Z, because that’s the one I have it set as currently, but how would I detect which way it’d need to go?

This is my current code:

Gun script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Equipped:Connect(function(tool)
	tool.Button1Down:Connect(function()
		local bullet = script.Bullet:Clone()
		bullet.Parent = workspace
		bullet.Position = mouse.Hit.p --script.Parent.Handle.Position
		bullet.Anchored = false
		wait(5)
		bullet:Destroy()
	end)
end)
2 Likes

this should detect where they are looking:

workspace.CurrentCamera.CFrame.LookVector
1 Like

This does give me the current position of the camera, but not really a solid direction.

image

I’d need to find out the direction of the camera (such as like x or something) so then I could then change the direction of the bodyvelocity/bodyforce

perse:

if somethingsomething ==  x then
bullet.BodyForce.X = 200
end

Try

Character.HumanoidRootPart.CFrame.LookVector

And replace Character to the path of the character.

Looks like that sorta works, just need to figure out speed stuff.

It now goes in the direction, but is extremely slow

This is the code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Equipped:Connect(function(tool)
	tool.Button1Down:Connect(function()
		print(player.Character.HumanoidRootPart.CFrame.LookVector)
		local position = Vector3.new(mouse.X, mouse.Y, 0)
		local bullet = script.Bullet:Clone()
		bullet.Parent = workspace
		bullet.Position = mouse.Hit.p
		bullet.Anchored = false
		bullet.BodyForce.Force = player.Character.HumanoidRootPart.CFrame.LookVector
		bullet.BodyVelocity.Velocity = player.Character.HumanoidRootPart.CFrame.LookVector
		wait(5)
		bullet:Destroy()
	end)
end)

Gif: https://gyazo.com/7cde30d22831a9919564a6e209af5e53

Multiply the LookVector by a number to increase the speed.

drrr, I was just thinking that the negatives would mess it up, but no, should have tested it haha