Lerp calculation

So I am making an entity system, and dont suggest me to use for i loops to get alpha as it reduces performance for the 100s of entities for me to render.

local Time = 0;
local startPosition = script.Parent.Position;
local TimeToBe = workspace:GetServerTimeNow() + 5
while (Time < TimeToBe) do
    local alpha = (startPosition + (startPosition - script.Parent.Parent.Part2.Position)).Magnitude *       Time / TimeToBe
    local lerp =            
    script.Parent.Position:Lerp(script.Parent.Parent.Part2.Position,     alpha)
    script.Parent.Position = lerp
    print(script.Parent.Position)
    Time = workspace:GetServerTimeNow();
    task.wait()
end
script.Parent.Position = script.Parent.Parent.Part2.Position 

I dont understand whats wrong with it but it keeps going to like the millions in the x,y,z positions.

this is an example i tried to do for how i will calculate alpha in the future

Isn’t TweenService better for this?

I don’t use lerps, so I may be completely off with this response. However, it may be relevant so I’ll post this anyway:

You are initially setting time to 0, when it may make more sense for it to be assigned as workspace:GetServerTimeNow()

If that doesn’t help, what is the value of (startPosition + (startPosition - script.Parent.Parent.Part2.Position)).Magnitude when you print it out?

its the calculation for finding the alpha, let me print it out right now(a + (b - a) * t)

Running with 100 entities and it might lag

image
Intresting, alpha should be 0 to 1

Yeah, that is more or less what I suspected.
I think It may be to do with where you have put .Magnitude. Currently, it is ((a + (b-a)) * t instead of a + (b-a)*t

since computers follow the bodmas/pedmas rules the magnitude get added to the position of start
image

Try adding a .Magnitude to the first a / startPosition as well

local alpha = startPosition.Magnitude + (startPosition - script.Parent.Parent.Part2.Position).magnitude * Time / TimeToBe

i suppose like this?

Yeah

Its still the same thing, 80 alpha
image

I also tried a.mag + (b.mag - a.mag)

If you print out individual parts of it, what does it output? (e.g. print out the startPosition magnitude, then the magnitude when there was a subtraction, then the time / timetobe thing)

alright, i will do it right now

image

Maybe cframe:toworldspace might solve the problem

I think you are confusing the general numerical lerp formula with the built in Vector3 lerp function.

You might want to consider this formula for calculating lerp alpha:

Also time/TimeToBe should be all thats needed as the range of the value is 0-1

It works just like i intended it to be

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.