How to make the part look towards its target when lerping

How do I make a part look at its target direction while lerping it?

local cf = CFrame.lookAt(moveObject.Position, randomPosition.Position)

moveObject.CFrame = current:Lerp(randomPosition,alpha)

I am getting a look at (tell me if I am doing this wrong too) and I am also lerping it, how do I put those two together?

1 Like

You weren’t implementing the cf variable into the lerp so here:

local cf = CFrame.lookAt(moveObject.Position, randomPosition.Position)

moveObject.CFrame = current:Lerp(cf,alpha)

It is not longer moving, just slowly spinning in circles.

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.

it is still just spinning slowly?


			local cf = CFrame.lookAt(moveObject.Position, randomPosition.Position)
			local newPos = current:Lerp(cf,alpha)
			
			moveObject.CFrame = newPos * cf.Rotation

wait i didnt change it from cf hold on

This isn’t looking directly at its target, and sometimes it slowly looks there, not instantly.

I think it is because I am somehow lerping the looking at its target part? Idk if I am but I would appreciate some insight

it should be

local cf = CFrame.lookAt(moveObject.Position, randomPosition.Position)
local newPos = moveObject.Position:Lerp(randomPosition.Position,alpha)
			
moveObject.CFrame = CFrame.new(newPos) * cf.Rotation

Now it just goes to the position way faster than it should, and then disappears?

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

how to many objects are in mobs

Just 2 for now, but I plan on adding more since I am purposely making this modular

I also printed in the for loop, and it only prints once

remove courotine.wrap for now and check if it prints twice

Yup, just prints once
sdf sdfsdf

try ipairs

character qwrqwrqwrqtw

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

Same result

caldchasdlfkjfkfkjfkjfkjfkjfkjfkjfkfk

Sorry for the misunderstanding, that part is fixed. I just didn’t want to make another post for a short little issue.

1 Like