Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make something rotate until it faces a brick?

I'm trying to make a humanoid figure rotate to a brick. I used :

figure.HumanoidRootPart.CFrame = CFrame.new(figure.HumanoidRootPart.CFrame,game.workspace.Brick.Position)

I wanted it to keep rotating the HumanoidRootPart until it faces the brick. Is this possible? thanks.

1 answer

✓ Accepted answer

I would use the lerp function in ROBLOX.

To use lerp, you get a start point, or CFrame in this case, and an end point, and lerp though them.


So we know the start point. It'll be the brick, or whatever you're looking to rotate.

local partToLookAt = workspace.Part2
local part = workspace.Part

local start = part.CFrame

Now to get the finish, we can make a newCFrame, looking at partToLookAt, like this:

local partToLookAt = workspace.Part2
local part = workspace.Part

local start = part.CFrame
local finish = CFrame.new(part.Position, partToLookAt.Position)
> CFrame.new(position, look-at)

Then, we can lerp them in a for loop!

local partToLookAt = workspace.Part2
local part = workspace.Part

local start = part.CFrame
local finish = CFrame.new(part.Position, partToLookAt.Position)

for i = 0,1,0.03 do
	part.CFrame = start:lerp(finish, i)
	wait()
end

And we're done!

1 comment

1

Good answer, however it's noteworthy to mention that delta time should be used as progression of the animation, tween, lerp, etc (opposed to wait). Subsequently, it should be used with RunService so it's in sync with the processing of the client.

This discussion is preserved as it was. New answers, comments, and votes are closed.