Right now, the laser eyes work but the positioning is wrong. I tried to position the laser between the target position and the origin position but it doesn’t seem to work.
local leftRayOrigin = leftLazerEyeBone.WorldPosition
local rightRayOrigin = rightLazerEyeBone.WorldPosition
local leftDirection = (target.Position - leftRayOrigin).Unit
local rightDirection = (target.Position - rightRayOrigin).Unit
local leftRaycastResult = workspace:Raycast(leftRayOrigin, leftDirection * raycastLazerEyesMaxRange)
local rightRaycastResult = workspace:Raycast(rightRayOrigin, rightDirection * raycastLazerEyesMaxRange)
local leftCenter = (leftRayOrigin + target.Position) / 2
local rightCenter = (rightRayOrigin + target.Position) / 2
if leftRaycastResult then
local distance = leftRaycastResult.Distance
leftLazer.Size = Vector3.new(leftLazer.Size.X, leftLazer.Size.Y, distance)
leftLazer.Position = leftCenter
leftLazer.CFrame = CFrame.lookAt(leftRayOrigin, target.Position)
else
leftLazer.Size = Vector3.new(leftLazer.Size.X, leftLazer.Size.Y, raycastLazerEyesMaxRange)
leftLazer.Position = leftCenter
leftLazer.CFrame = CFrame.lookAt(leftRayOrigin, target.Position)
end
if rightRaycastResult then
local distance = rightRaycastResult.Distance
rightLazer.Size = Vector3.new(rightLazer.Size.X, rightLazer.Size.Y, distance)
rightLazer.Position = rightCenter
rightLazer.CFrame = CFrame.lookAt(rightRayOrigin, target.Position)
else
rightLazer.Size = Vector3.new(rightLazer.Size.X, rightLazer.Size.Y, raycastLazerEyesMaxRange)
rightLazer.Position = rightCenter
rightLazer.CFrame = CFrame.lookAt(rightRayOrigin, target.Position)
end
Instead of positioning the laser at the midpoint between the target and the eye, it would be more accurate to position it at the eye, then point it toward the target.
if leftRaycastResult then
local distance = leftRaycastResult.Distance
leftLazer.Size = Vector3.new(leftLazer.Size.X, leftLazer.Size.Y, distance)
leftLazer.Position = leftRayOrigin -- change this line
leftLazer.CFrame = CFrame.lookAt(leftRayOrigin, target.Position)
else
leftLazer.Size = Vector3.new(leftLazer.Size.X, leftLazer.Size.Y, raycastLazerEyesMaxRange)
leftLazer.Position = leftRayOrigin -- and this line
leftLazer.CFrame = CFrame.lookAt(leftRayOrigin, target.Position)
end
if rightRaycastResult then
local distance = rightRaycastResult.Distance
rightLazer.Size = Vector3.new(rightLazer.Size.X, rightLazer.Size.Y, distance)
rightLazer.Position = rightRayOrigin -- change this line
rightLazer.CFrame = CFrame.lookAt(rightRayOrigin, target.Position)
else
rightLazer.Size = Vector3.new(rightLazer.Size.X, rightLazer.Size.Y, raycastLazerEyesMaxRange)
rightLazer.Position = rightRayOrigin -- and this line
rightLazer.CFrame = CFrame.lookAt(rightRayOrigin, target.Position)
end