Still have questions? Join our Discord server and get real time help.
4

# why isn't my raycasting detecting anything?

Edited 2 months ago

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?

0
the pos is mouse.Hit.p botw_legend 452 — 2mo
0
ok Im making you a whole new script SteamG0D 1489 — 2mo
0
ok botw_legend 452 — 2mo

1
SteamG0D 1489
2 months ago
Edited 2 months ago

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)
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)

0
is there anyway that this can be done in a server script? botw_legend 452 — 2mo
0
i just want a basic raycasting thing that goes for one stud and detects if it hit anything botw_legend 452 — 2mo
0
Yes, just have a remote event that does the lines 29-36, just send it the variables used in those lines. SteamG0D 1489 — 2mo
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. SteamG0D 1489 — 2mo
0
Just tested it in studio, it should work. SteamG0D 1489 — 2mo
0
ok thanks i was just wondering if all of that was nessesary botw_legend 452 — 2mo
0
and can i do all of that in a server script because i noticed that you used camera... botw_legend 452 — 2mo
0
camera has to be done in a local script because it gets the camera of that specific player. SteamG0D 1489 — 2mo
0
Anything that includes a tool will require a local script because of how input is handled. SteamG0D 1489 — 2mo
0
wait botw_legend 452 — 2mo
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 botw_legend 452 — 2mo
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 botw_legend 452 — 2mo
1
Fifkee 2016
2 months ago

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.

0
so barrel would be the start location and pos would be the end position so would i replace pos with barrel.Position-pos? botw_legend 452 — 2mo
0
Pos is where you're shooting to, right? You shoot from the barrel, so that would be PositionA, and Pos would be PositionB. Fifkee 2016 — 2mo
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? botw_legend 452 — 2mo
0
i also made it so it shoots the raycast 50 studs and still doesn't hit anything botw_legend 452 — 2mo