Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
1
Answered by 7 years ago

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
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. ScriptGuider 5640 — 7y
Ad

Answer this question