I tried the Roblox wiki, no luck... Can anyone help me, please? I just want like, a laser brick, from my torso, to the baseplate or wherever I click..
The Workspace has a FindPartOnRay method.
The ray is a ray object that has stored the origin and direction of the ray. ignoreDescendentsInstance is an instance value that makes it so that, when the ray is created, it will not be obstructed by the descendants of the instance passed to it.
In Coding Terms:
local ray = Ray.new( Weapon.Handle.CFrame.p, -- origin (mouse.Hit.p - Weapon.Handle.CFrame.p).unit * 500 -- direction ) local ignore = game.Players.LocalPlayer.Character local hit, position = Workspace:FindPartOnRay(ray, ignore)
The Workspace
has a FindPartOnRay method.
(You can also use FindPartOnRayWithIgnoreList
for a customizable list of parts to ignore during case).
Its parameters are a Ray
an optional Instance
to ignore (and an optional way to treat terrain).
Ray
s are constructed with Ray.new( from, direction)
. Unfortunately, FindPartOnRay
only looks the length of the direction, so the direction must be very large, so it usually has to be multiplied by a large number.
FindPartOnRay
returns two things. First, the part it hits, and second, the position it hits.
Using it could look like this
-- assume "from" is some Vector3, in your example the Torso's Position -- assume "target" is some other Vector3, in your example -- the mouse's hover position (mouse.Hit.p) local direction = (target - from).unit; -- Unit makes it length 1. direction = direction * 1000; -- Allow raycasting up to 1000 studs away local partHit, positionHit = Workspace:FindPartOnRay( Ray.new(from, direction) , game.Players.LocalPlayer.Character ); -- We ignore the local player's character for the raycast. -- (This would require this script to be in a local script) -- The laser should stretch from "from" to "positionHit".
So, what you need to do (for the torso to baseplate) is this:
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) local start = character.Torso.Position local ray = Instance.new("Part", game.Workspace) ray.Name = "Ray" ray.Position = Ray.new(Vector3.new(start),Vector3.new(game.Workspace.Baseplate)) end) end)
See if this works. I have never tried ray casting. I just saw it on CodeTheorem's tutorials. Check him out on ROBLOX. His channel is in his blurb.