Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What do I write to complete my Raycast?

I have a brick that fires a ray. The ray should fire in the direction the brick is facing, so whenever the direction the brick is facing change, the direction of the rays being fired should change too, however I don't know what to write to make this happen please help.

This is what it looks like: Raycasting Brick

and another: Raycasting Brick2

while wait() do

	local ray = Ray.new(script.Parent.CFrame.p, (Vector3.new(0, 0, -60) - script.Parent.CFrame.p).unit * 300) -- Problem is here
	
	local part, endPoint = workspace:FindPartOnRay(ray, script.Parent)
		
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
 
		local distance = (script.Parent.CFrame.p - endPoint).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(script.Parent.CFrame.p, endPoint) * CFrame.new(0, 0, -distance / 2)
		
		print(part)
		game:GetService("Debris"):AddItem(beam, 0.1)
end

1 answer

✓ Accepted answer

You can use the lookVector for the ray cast.

Here is an example

while wait() do

    local ray = Ray.new(script.Parent.CFrame.p,  script.Parent.CFrame.lookVector * 300) -- Problem is here

    local part, endPoint = workspace:FindPartOnRay(ray, script.Parent)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Bright red")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (script.Parent.CFrame.p - endPoint).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(script.Parent.CFrame.p, endPoint) * CFrame.new(0, 0, -distance / 2)

        print(part)
        game:GetService("Debris"):AddItem(beam, 0.1)
end

Hope this helps.

2 comments

0

Thank you~!

0

Np

This discussion is preserved as it was. New answers, comments, and votes are closed.