Grab the Rotation that was given from CFrame.lookAt(), Lerp the Objects Position Towards the Target with the Orientation Applied to it.
Something like this:
local lookAt = CFrame.lookAt(p0, p1) -- Return Direction
local newPos = p0:Lerp(p1, alpha) -- Lerp Position (Vector3)
obj.CFrame = CFrame.new(newPos) * lookAt.Rotation -- Applies CFrame with Position and Rotation
If you want it to transition slowly, use what @bytesleuth provided.
Now it works, I fixed it. However, I have another issue. I am looping through a folder and I am running a move module on it, but it only runs for one the of the parts? How can I fix this.
for i, v in pairs(mobs:GetChildren()) do
coroutine.wrap(moveModule.Part(v))
end
local current = moveObject.CFrame
local target = CFrame.lookAt(moveObject.Position, randomPosition.Position)
local alpha = 0 -- Set the initial interpolation value (0 means start position, 1 means target position)
while alpha < 1 do
alpha = math.min(1, alpha + 0.1 ) -- Ensure alpha doesn't exceed 1, 0.1 is the 'speed'
-- Apply lerp to the position
moveObject.CFrame = current:Lerp(target, alpha)
-- Update the orientation of the part to look at the target direction
local lookAtDir = (randomPosition.Position - moveObject.Position).unit
moveObject.CFrame = CFrame.new(moveObject.Position, moveObject.Position + lookAtDir)
wait() -- Wait for a short time before the next iteration
end