Help with Laser Eyes

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

3 Likes

Shouldn’t it be leftLazer.CFrame.lookAt?

I don’t think thats a thing. leftLazer.CFrame.lookAt doesn’t exist.

Oh nevermind then I didn’t know.

CFrame.LookAt isn’t the problem. Its the positioning. I need the lazer to spawn between the target and the origin position.

Oh, I believe it has something to do with the pivot of the cylinder being in the center and you are moving it to the incorrect position.

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

Uhh. Then the laser would go though the eye.

I currently need help, please help me.