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.

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        script.Parent.ClickDetector.MouseClick:Connect(function()
            local player = plr
            local ray = Ray.new(script.Parent.CFrame.p, char.HumanoidRootPart.CFrame.p)
            local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
            local beam = Instance.new("Part", workspace)
            beam.BrickColor = BrickColor.new("Royal purple")
            beam.FormFactor = "Custom"
            beam.Material = "Neon"
            beam.Transparency = .25
            beam.Anchored = true
            beam.Locked = true
            beam.CanCollide = false
            local distance = (script.Parent.CFrame.p - position).magnitude
            beam.Size = Vector3.new(.3,.3,distance)
            beam.CFrame = CFrame.new(script.Parent.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
            game:GetService("Debris"):AddItem(beam, .1)
        end)
    end)
end)

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

        script.Parent.ClickDetector.MouseClick:Connect(function(plr)
            local player = plr
            local char = plr.Character
            local ray = Ray.new(script.Parent.CFrame.p, (char.HumanoidRootPart.CFrame.p-script.Parent.CFrame.p).unit*1000)--the direction wasnt defined properly

            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

            local beam = Instance.new("Part", workspace)
            beam.BrickColor = BrickColor.new("Royal purple")
            beam.FormFactor = "Custom"
            beam.Material = "Neon"
            beam.Transparency = .25
            beam.Anchored = true
            beam.Locked = true
            beam.CanCollide = false
            local distance = (script.Parent.CFrame.p - position).magnitude
            beam.Size = Vector3.new(.3,.3,distance)
            beam.CFrame = CFrame.new(script.Parent.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
            game:GetService("Debris"):AddItem(beam, .1)
        end)

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.

script.Parent.ClickDetector.MouseClick:Connect(function(char)
            local ray = Ray.new(script.Parent.CFrame.p, char.HumanoidRootPart.CFrame.p)
            local part, position = workspace:FindPartOnRayWithWhitelist(ray, char, false, true)
            local beam = Instance.new("Part") --the parent parameter of Instance.new() is decaperated, set the parent last instead
            beam.BrickColor = BrickColor.new("Royal purple")
          -- formfactor is decaperated, dont use it. the wiki has a ton of decaperated stuff
            beam.Material = "Neon"
            beam.Transparency = .25
            beam.Anchored = true
            beam.Locked = true
            beam.CanCollide = false
        beam.Parent = workspace

            local distance = (script.Parent.CFrame.p - position).magnitude
            beam.Size = Vector3.new(.3,.3,distance)
            beam.CFrame = CFrame.new(script.Parent.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
            game:GetService("Debris"):AddItem(beam, .1)
        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