Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

why isn't my raycasting detecting anything?

Asked by 4 years ago
Edited 4 years ago

so this is the code for my raycast

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

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:

1local 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 502 — 4y
0
ok Im making you a whole new script SteamG00B 1633 — 4y
0
ok botw_legend 502 — 4y

2 answers

Log in to vote
1
Answered by
SteamG00B 1633 Moderation Voter
4 years ago
Edited 4 years 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.

01local tool = script.Parent
02local UserInputService = game:GetService("UserInputService")
03local player = game.Players.LocalPlayer
04local Camera = workspace.CurrentCamera
05local equipped = false
06local character = player.Character:GetChildren()
07local toolOrigin = character:FindFirstChildOfClass("Tool").tool.barrel.Position
08local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent1")
09 
10tool.Equipped:Connect(function()
11    equipped = true
12end)
13 
14tool.Unequipped:Connect(function()
15    equipped = false
View all 30 lines...

Server script:

01local RE = Instance.new("RemoteEvent")
02RE.Name = "RemoteEvent1"
03RE.Parent = game.ReplicatedStorage
04 
05RE.OnServerEvent:Connect(function(player,toolOrigin,worldCF,character)
06    local ray = Ray.new(toolOrigin,(worldCF.Position-toolOrigin).Unit)
07    local ray1 = Ray.new(ray.Origin,ray.Direction * 1000)
08    local part, hitPos, norm = workspace:FindPartOnRayWithIgnoreList(ray1,character)
09    if part then
10        print(part," yay!")
11    else
12        print(part," awww")
13    end
14end)
0
is there anyway that this can be done in a server script? botw_legend 502 — 4y
0
i just want a basic raycasting thing that goes for one stud and detects if it hit anything botw_legend 502 — 4y
0
Yes, just have a remote event that does the lines 29-36, just send it the variables used in those lines. SteamG00B 1633 — 4y
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. SteamG00B 1633 — 4y
View all comments (8 more)
0
Just tested it in studio, it should work. SteamG00B 1633 — 4y
0
ok thanks i was just wondering if all of that was nessesary botw_legend 502 — 4y
0
and can i do all of that in a server script because i noticed that you used camera... botw_legend 502 — 4y
0
camera has to be done in a local script because it gets the camera of that specific player. SteamG00B 1633 — 4y
0
Anything that includes a tool will require a local script because of how input is handled. SteamG00B 1633 — 4y
0
wait botw_legend 502 — 4y
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 502 — 4y
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 502 — 4y
Ad
Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years 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 502 — 4y
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 2017 — 4y
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 502 — 4y
0
i also made it so it shoots the raycast 50 studs and still doesn't hit anything botw_legend 502 — 4y
View all comments (2 more)
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) botw_legend 502 — 4y
0
also i noticed that people have like hit, position for line 2 i think the problem is there botw_legend 502 — 4y

Answer this question