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

How can I keep a model rotated to point at another part until the for loop is done?

Asked by 5 years ago

I'm using the CFrame:lerp() function to move/Tween a model from one place to another in a for i,v loop. Here's the code I wrote for that:

local model = script.Parent.Parent
local dest = CFrame.new(72.69, 2, -27.46)

for i = 0,1,.00001 do
    wait(0.01)
    model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:lerp(dest, i))
end

Notice: The model variable is a model (no duh) and it is also anchored.

What this script does is it Tweens the model, without tweening it because you can't tween models. All I want to know is how can I keep the model's Orientation to keep pointing at the destination? I attempted CFrame.lookVector And here's how I took a shot at it:

local model = script.Parent.Parent
local dest = CFrame.new(69.69, 2, -27.46)

for i= 0,1,.00001 do --.00001
    wait(0.01)
    model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:lerp(dest, i))
    model.PrimaryPart.Orientation = CFrame.lookVector(72.69, 2, -27.46)
end

Line 7 was where I put the thing I want it to do. But when I ran it, it stopped the level preview and highlighted the line in red. Welp, can someone tell me what I can do? Thank u!!!

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Alright so on line 7, I'm not exactly sure what your intent was but from reading from the question, I assume you always want the model to point towards the destination cframe. So instead of doing that you should use CFrame.new(pos, lookatpos) where the a cframe is created, located at the first argument(pos) and pointing towards the 2nd(lookatpos). Here is the code you should use:

local model = script.Parent.Parent
local dest = CFrame.new(69.69, 2, -27.46)

for i= 0,1,.00001 do --.00001
    wait(0.01)
    model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:lerp(dest, i))
    model:SetPrimaryPartCFrame(CFrame.new(model.PrimaryPart.CFrame.p,dest.p))
end

I would like to note that this only works if the primary part front face is also facing what you consider the front of the model. If this isn't the case you will have to multiply the cframe in line 7 times a CFrame.Angles to compensate.

Ad

Answer this question