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

How do I 'raycast' ?

Asked by 10 years ago

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

3 answers

Log in to vote
1
Answered by
Kruer7 0
10 years ago

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)
0
What does .unit mean? marioman1932 48 — 4y
0
a kinda late response but the .unit means the studs the ray will go seikkatsu 110 — 4y
Ad
Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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

Rays 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".


0
This helped! Thank you! (: kayden963 25 — 10y
Log in to vote
0
Answered by 10 years ago

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.

0
fuck you xxIamInevitable 2 — 3y

Answer this question