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