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

Raycasting from a zombie forward?

Im trying to make a beam raycast from the zombies torso forward. The problem is it is bent and not perpendicular to the torso and when the zombie moves towards me it generates a new beam on a similar rotation. I have figured out the all the beams are pointing towards the centre of the map(0,0,0)

function raycast()
	local ray = Ray.new(
		(script.Parent.Torso.CFrame).p ,
		(script.Parent.Torso.CFrame.lookVector * 5)
	)
	local part, position = workspace:FindPartOnRay(ray, nil, false, true)
	
	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 = 5
	beam.Size = Vector3.new(0.3, 0.3, distance)
	beam.CFrame = CFrame.new(script.Parent.Torso.Position, script.Parent.Torso.CFrame.lookVector ) * CFrame.new(0, 0, 2.5)
end


while true do
	wait(1)
	raycast()
end

1 answer

Raycasts are a nightmare for beginners.

I recommend creating a variable for the start and direction to make it easier to understand. Also at line 04, you should get the lookVector of the Torso and get the .unit, then multiply it by the range (I think, I'm not a pro in Raycasts).

Take a look to see if it works:

local start = script.Parent.Torso
local direction = (script.Parent.Torso.CFrame.lookVector).unit * 5

local ray = Ray.new(start,finish)

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