Comments and answers for "Take my useful script - but help me improve it?"
http://answers.unity.com/questions/1362845/take-my-useful-script-but-help-me-improve-it.html
The latest comments and answers for the question "Take my useful script - but help me improve it?"Answer by _AkSeL_
http://answers.unity.com/answers/1362900/view.html
You are actually slerping between the current object position (moving) and the next step position (fixed).
As your object move away from last step's position, the distance to next step's position decrease, so the slerping is actually "slowing down" the movement at each step.
If you want to have a constant speed, you need to slerp between fixed positions, on a time-based factor, e.g. between "current" step and next step based on step duration.
Here is an example that works with your script:
// The step we are currently "in"
public int currentStep = 0;
// The step we move to
public int nextStep = 1;
// Interpolation between currentStep and nextStep : 0 -> currentStep, 1 -> nextStep.
public float interpolationFactor = 0f;
// The time at the start of currentStep
public float startTime = 0f;
// Total duration of a step.
public float stepDuration = 1f;
In the `Start()` function, we need to initialize `startTime`:
void Start()
{
speed = steps[0].speed;
smooth = steps[0].smooth;
worldSpace = steps[0].worldSpace;
startTime = Time.time;
}
And then in the `Update()` function we do the slerping:
void Update()
{
// Switch between steps based on time
// Equivalent to your Next() function
if (Time.time - startTime > stepDuration)
{
currentStep = nextStep;
nextStep++;
if (nextStep >= steps.Length)
nextStep = 0;
startTime = Time.time;
}
// Interpolate based on time
interpolationFactor = ((Time.time - startTime) / stepDuration);
// Slerp
movingObj.transform.position = Vector3.Slerp(steps[currentStep].pos, steps[nextStep].pos, interpolationFactor);
movingObj.transform.rotation = Quaternion.Slerp(Quaternion.Euler(steps[currentStep].rot), Quaternion.Euler(steps[nextStep].rot), interpolationFactor);
// [...]Sat, 10 Jun 2017 10:28:31 GMT_AkSeL_Comment by Thomas-Hawk on Thomas-Hawk's answer
http://answers.unity.com/comments/1362930/view.html
Ah, ok, that clarifies it a little bit. Thank you sir.Thu, 08 Jun 2017 06:42:48 GMTThomas-HawkAnswer by jdean300
http://answers.unity.com/answers/1362918/view.html
You are using lerp wrong (at least for what you want to accomplish). `Vector3.Lerp(a, b, .5)` returns the point half way between a and b. But if you repeatedly do `a = Vector3.Lerp(a, b, .5)` you are repeatedly moving half way to b but never quite getting there.
Eventually you get close enough and this line in your code `if (Vector3.Distance (movingObj.transform.localPosition, steps [i].pos) <= .1f)` causes you to move to the next point.
What you should be doing is lerping from the previous point (not the objects current position) to the next point according to to t. So something like this:
movingObj.transform.localPosition = Vector3.Lerp (steps [i - 1].pos, steps [i].pos, t);
You're going to have to watch out for when `i == 0` because you will get an index out of bounds exception, but hopefully you get the idea.Thu, 08 Jun 2017 06:05:18 GMTjdean300