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

Why doesn't my ray hit the right thing?

Asked by
Jexpler 63
6 years ago
Edited 6 years ago

I'm experimenting with raycasting, and I made this script that is supposed to cast a ray towards the player whenever they click the part.

01game.Players.PlayerAdded:Connect(function(plr)
02    plr.CharacterAdded:Connect(function(char)
03        script.Parent.ClickDetector.MouseClick:Connect(function()
04            local player = plr
05            local ray = Ray.new(script.Parent.CFrame.p, char.HumanoidRootPart.CFrame.p)
06            local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
07            local beam = Instance.new("Part", workspace)
08            beam.BrickColor = BrickColor.new("Royal purple")
09            beam.FormFactor = "Custom"
10            beam.Material = "Neon"
11            beam.Transparency = .25
12            beam.Anchored = true
13            beam.Locked = true
14            beam.CanCollide = false
15            local distance = (script.Parent.CFrame.p - position).magnitude
View all 21 lines...

But for some reason, it casts it nowhere near me.

2 answers

Log in to vote
0
Answered by
aazkao 787 Moderation Voter
6 years ago
Edited 6 years ago

The real problem is that you didnt define the Vector3 direction when you cast the ray correctly and also you want the ray to stop at the player that clicked it, but yet in the IgnoreDescendants arguments of FindPartOnRay you put the players character, it will shoot off into infinity if you do that, what its supposed to ignore is the origin part.

also the first 2 lines isnt needed, you can easily get the player character with .Character

01script.Parent.ClickDetector.MouseClick:Connect(function(plr)
02    local player = plr
03    local char = plr.Character
04    local ray = Ray.new(script.Parent.CFrame.p, (char.HumanoidRootPart.CFrame.p-script.Parent.CFrame.p).unit*1000)--the direction wasnt defined properly
05 
06    local part, position = workspace:FindPartOnRay(ray, script.Parent, false, true)--do not ignore the player character which you want the ray to hit! ignore the part that the ray originated from
07 
08    local beam = Instance.new("Part", workspace)
09    beam.BrickColor = BrickColor.new("Royal purple")
10    beam.FormFactor = "Custom"
11    beam.Material = "Neon"
12    beam.Transparency = .25
13    beam.Anchored = true
14    beam.Locked = true
15    beam.CanCollide = false
16    local distance = (script.Parent.CFrame.p - position).magnitude
17    beam.Size = Vector3.new(.3,.3,distance)
18    beam.CFrame = CFrame.new(script.Parent.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
19    game:GetService("Debris"):AddItem(beam, .1)
20end)
0
Thanks Jexpler 63 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

the problem is that the player's character is on the :FindPartOnRay() IgnoreList so it wont raycast towards the player. You could fix this by using FindPartOnRayWithWhitelist, since you only want it to raycast on the player and you dont need to remove the player.Character part since its not a IgnoreList but a Whitelist. another way is just removing the player.Character, but i would suggest using the Whitelist method.

01script.Parent.ClickDetector.MouseClick:Connect(function(char)
02            local ray = Ray.new(script.Parent.CFrame.p, char.HumanoidRootPart.CFrame.p)
03            local part, position = workspace:FindPartOnRayWithWhitelist(ray, char, false, true)
04            local beam = Instance.new("Part") --the parent parameter of Instance.new() is decaperated, set the parent last instead
05            beam.BrickColor = BrickColor.new("Royal purple")
06          -- formfactor is decaperated, dont use it. the wiki has a ton of decaperated stuff
07            beam.Material = "Neon"
08            beam.Transparency = .25
09            beam.Anchored = true
10            beam.Locked = true
11            beam.CanCollide = false
12        beam.Parent = workspace
13 
14            local distance = (script.Parent.CFrame.p - position).magnitude
15            beam.Size = Vector3.new(.3,.3,distance)
16            beam.CFrame = CFrame.new(script.Parent.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
17            game:GetService("Debris"):AddItem(beam, .1)
18        end)

also you could instead use the parameter of the MouseClick event to get the player's character instead of using the character parameter or player parameter of CharacterAdded and PlayerAdded. NOTE im not sure if it gets the player's character or the player on the parameter of the MouseClick event, so try doing char.Character and char to be sure.

0
It ended up being char.Character. It also said "Unable to cast value to Objects" for line 3 Jexpler 63 — 6y

Answer this question