Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

why isn't my raycasting detecting anything?

so this is the code for my raycast

local ray = Ray.new(char:FindFirstChildOfClass("Tool").tool.barrel.Position,(pos).Unit *5)--starts the ray
local hit = game.Workspace:FindPartOnRay(ray)
if hit then
	print(hit," yay!")
else
	print(hit," awww")
end

it always prints nil awww even when i hit something and it is closer than 5 studs and at the thing im standing on too please help

fifkey i changed the raycast to this:

local ray = Ray.new(char:FindFirstChildOfClass("Tool").tool.barrel.Position,(pos-char:FindFirstChildOfClass("Tool").tool.barrel.Position).Unit *5)--starts the ray

it didn't work is there anything else i did wrong?

3 comments

0

the pos is mouse.Hit.p

0

ok Im making you a whole new script

0

ok

2 answers

✓ Accepted answer

So this script basically gets the position from the camera to the point in world space and then makes a new ray from your gun barrel to where ever the click location is at. I have explained a bit more with comments.

local tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local equipped = false
local character = player.Character:GetChildren()
local toolOrigin = character:FindFirstChildOfClass("Tool").tool.barrel.Position
local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent1")

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

function getMouseHit(x,y)
	local ray = Camera:ScreenPointToRay(x, y, 0)
	local ray1 = Ray.new(ray.Origin,ray.Direction * 1000)
	local part, hitPos, norm = workspace:FindPartOnRayWithIgnoreList(ray1,character)
	return CFrame.new(hitPos,ray1.Origin)*CFrame.Angles(0,math.rad(180),0)
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if equipped and not gameProcessed and input.UserInputType == Enum.UserInputType.MouseButton1 then
		local worldCF = getMouseHit(input.Position.X,input.Position.Y)
		RE:FireServer(toolOrigin,worldCF,character)
	end
end)

Server script:

local RE = Instance.new("RemoteEvent")
RE.Name = "RemoteEvent1"
RE.Parent = game.ReplicatedStorage

RE.OnServerEvent:Connect(function(player,toolOrigin,worldCF,character)
	local ray = Ray.new(toolOrigin,(worldCF.Position-toolOrigin).Unit)
	local ray1 = Ray.new(ray.Origin,ray.Direction * 1000)
	local part, hitPos, norm = workspace:FindPartOnRayWithIgnoreList(ray1,character)
	if part then
		print(part," yay!")	
	else
		print(part," awww")
	end
end)

12 comments

0

is there anyway that this can be done in a server script?

0

i just want a basic raycasting thing that goes for one stud and detects if it hit anything

0

Yes, just have a remote event that does the lines 29-36, just send it the variables used in those lines.

0

I tried to do it by hand right in the edit box, so let me know if I missed anything, but I'm pretty sure I got it right.

0

Just tested it in studio, it should work.

0

ok thanks i was just wondering if all of that was nessesary

0

and can i do all of that in a server script because i noticed that you used camera...

0

camera has to be done in a local script because it gets the camera of that specific player.

0

Anything that includes a tool will require a local script because of how input is handled.

0

wait

0

im so stupid the gun get teleported to the player threw a local script i think ill do it in a local script then with a remote event ill update the bullets position

0

ok i made it into a local script and it works it prints yay when i aim it at a part and says aww when i aim it up in the sky! thx

Make sure that the second value of Ray.new is a unit vector. A unit vector is a vector value with a magnitude of 1, used to indicate a direction. To make a unit out of two positions, you use (PositionB - PositionA).Unit and multiply that value by a scalar (a number). If that's too complicated, you can also do CFrame.new(PositionA, PositionB).LookVector * Scalar as well.

6 comments

0

so barrel would be the start location and pos would be the end position so would i replace pos with barrel.Position-pos?

0

Pos is where you're shooting to, right? You shoot from the barrel, so that would be PositionA, and Pos would be PositionB.

0

it still doesn't work i edited the question showing what changes i made to the code is there anything i didn't do correctly?

0

i also made it so it shoots the raycast 50 studs and still doesn't hit anything

0

also i added a print doing the pos-barrel.Position it printed 203.379059, 3.97526407, 63.1472015 (i added this to the chance that this would help)

0

also i noticed that people have like hit, position for line 2 i think the problem is there

This discussion is preserved as it was. New answers, comments, and votes are closed.