Ray points down if you shoot at the sky or put the camera to your feet

I’ve made my first gun using raycasting, but for some reason, whenever I shoot at the sky, my bullet goes straight down into the floor, and whenever I put the camera to my feet, the bullet goes in a random direction.

Code:

local tool = script.Parent

local reload_event = tool:WaitForChild("Reload")
local fire_event = tool:WaitForChild("Fire")
local player_killed_event = tool:WaitForChild("PlayerKilled")

local players = game:GetService("Players")
local repStor = game:GetService("ReplicatedStorage")

local damage = 20

local function shootEffect()
	spawn(function()
		tool.Handle:WaitForChild("FireLight").Enabled = true
		tool.Handle.FireSound:Play()
		wait(0.01)
		tool.Handle:WaitForChild("FireLight").Enabled = false
	end)
end

fire_event.OnServerEvent:Connect(function(player, mouseP)
	shootEffect()
	local origin = tool:WaitForChild("Handle").Barrel.Position
	local direction = (mouseP - origin).Unit
	local rayInfo = RaycastParams.new()
	rayInfo.FilterDescendantsInstances = {player.Character}
	rayInfo.FilterType = Enum.RaycastFilterType.Blacklist

	local result = workspace:Raycast(origin, direction * 500, rayInfo)
	local mid = result and result.Position or origin - direction * 500
	local distance = (origin - mid).Magnitude

	local bulletClone = game.ServerStorage.Bullet:Clone()
	bulletClone.Size = Vector3.new(0.1, 0.1, distance)
	bulletClone.CFrame = CFrame.new(origin, mid) * CFrame.new(0, 0, -distance / 2)
	bulletClone.Parent = workspace
	
	if result then
		local resultInstance = result.Instance
		if resultInstance then
			if resultInstance.Parent:FindFirstChild("Humanoid") or resultInstance.Parent.Parent:FindFirstChild("Humanoid") then
				local humanoid = resultInstance.Parent:FindFirstChild("Humanoid") or resultInstance.Parent.Parent:FindFirstChild("Humanoid")
				if humanoid.Health <= damage then
					humanoid:TakeDamage(damage)
					player_killed_event:FireClient(player, humanoid.Parent)
				else
					humanoid:TakeDamage(damage)
					fire_event:FireClient(player)
				end
			end
		end
	end
	wait(0.01)
	bulletClone:Destroy()
end)
1 Like

Code looks good, try putting a part where mouseP is to debug the vector3 positions a bit more.

1 Like

Parts are going into workspace in the explorer, but I can’t find them ingame

local tool = script.Parent

local reload_event = tool:WaitForChild("Reload")
local fire_event = tool:WaitForChild("Fire")
local player_killed_event = tool:WaitForChild("PlayerKilled")

local players = game:GetService("Players")
local repStor = game:GetService("ReplicatedStorage")

local damage = 20

local function shootEffect()
	spawn(function()
		tool.Handle:WaitForChild("FireLight").Enabled = true
		tool.Handle.FireSound:Play()
		wait(0.01)
		tool.Handle:WaitForChild("FireLight").Enabled = false
	end)
end

fire_event.OnServerEvent:Connect(function(player, mouseP)
	shootEffect()
	local part = Instance.new("Part")
	part.Anchored = true
	part.Transparency = 0
	part.Parent = workspace
	part.Position = Vector3.new(mouseP)
	local origin = tool:WaitForChild("Handle").Barrel.Position
	local direction = (mouseP - origin).Unit
	local rayInfo = RaycastParams.new()
	rayInfo.FilterDescendantsInstances = {player.Character}
	rayInfo.FilterType = Enum.RaycastFilterType.Blacklist

	local result = workspace:Raycast(origin, direction * 500, rayInfo)
	local mid = result and result.Position or origin - direction * 500
	local distance = (origin - mid).Magnitude

	local bulletClone = game.ServerStorage.Bullet:Clone()
	bulletClone.Size = Vector3.new(0.1, 0.1, distance)
	bulletClone.CFrame = CFrame.new(origin, mid) * CFrame.new(0, 0, -distance / 2)
	bulletClone.Parent = workspace
	
	if result then
		local resultInstance = result.Instance
		if resultInstance then
			if resultInstance.Parent:FindFirstChild("Humanoid") or resultInstance.Parent.Parent:FindFirstChild("Humanoid") then
				local humanoid = resultInstance.Parent:FindFirstChild("Humanoid") or resultInstance.Parent.Parent:FindFirstChild("Humanoid")
				if humanoid.Health <= damage then
					humanoid:TakeDamage(damage)
					player_killed_event:FireClient(player, humanoid.Parent)
				else
					humanoid:TakeDamage(damage)
					fire_event:FireClient(player)
				end
			end
		end
	end
	wait(0.01)
	bulletClone:Destroy()
end)

Try changing this:

local mid = result and result.Position or origin - direction * 500

to this:

local mid = result and result.Position or (origin - direction) * 500

Came to the assumption that this line was the issue due to there being no result when you click the sky. So, the “or” part might be the issue. PEMDAS baby.

Edit: if that doesn’t work, here’s my only other idea without opening studio:

local mid = result and result.Position or (direction * 500) - origin -- pos_final - pos_initial
1 Like

MouseP is already a vector3 no need to vector 3 a vector 3.

1 Like