CFrame.LookAt makes part disappear

So when using CFrame.LookAt and making it look at a direction with a vector of (0, 0, 0), it would make the part disappear. It sets the position to (0, -inf, 0).

Code:

local part = script.Parent
local targetPart = workspace:WaitForChild("TargetPart")

local direction = Vector3.new(0, 0, 0) --(targetPart.Position - part.Position).Unit * Vector3.new(1, 0, 1) --example
local cframe = CFrame.lookAt(part.Position, part.Position + direction)

part.CFrame = cframe

Because it breaks the math. You cannot look at “nothing”, it literally doesn’t make sense. It’s probably dividing by zero somewhere because you set it to 0,0,0.

image

1 Like

The part moves and it updates the LookAt so if the part is at the exact target position, it would disappear.

Any solutions to this?

I still don’t know what it is exactly that you’re trying to do. If there isn’t a specified direction then just don’t use .lookAt. Or alternatively, use the part’s own LookVector as the backup direction so it doesn’t actually change the rotation.

CFrame.lookAlong(part.Position, direction or part.CFrame.LookVector)

Note: CFrame.lookAlong(p, d) is the same as CFrame.lookAt(p, p+d)

This part won’t run since it isn’t nil:
or part.CFrame.LookVector

CFrame.lookAlong(part.Position, if direction==Vector3.zero then part.CFrame.LookVector else direction)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.