Why not just define a function describing animation curve for movement

I do feel it is clean code, looking at the way the training movement code just changes the position by deltaT*speed*moveVector until reaching destination while forward is just slerped. Especially when looking at the need for special handling at arriving close enough to the destination.

I want to understand whether there is a real reason why this was used instead of a method which in my eyes is both easier to reason about and obviously more generic.

What I did is calculate movement time in advance as well as coordinate distance vector. On every update decrease remaining time it by effectiveDeltaT = Mathf.Min(deltaT, remainingMoveTime) every Update. No need for quirks to detect end of movement. Define a delegate similar to what we have in animation curves that starts at one and goes to zero. Then the position part of the update code is as simple as transform.position = f(remainingMoveTime)*(distanceVector)+startingPosition;

Even if you do not want to save starting position you can use something like
transform.position += (f(remainingTime - effectiveDeltaT) - f(remainingTime))*distanceVector;

Function can be just linear. As for replacing (S)Lerp I suspect Unity gives an easy way to define a Bezier curve function in code, somehow I did not find it in documentation.

So what is the advantage of the approach used in training video? Also how do I make function for two dimensional curve like the ones in animation windows using existing Unity libraries?

All the math up there means nothing to me (sorry) so I’m skipping that bit.

You can define an AnimationCurve in your component to get the curve in the inspector.

[SerializeField] AnimationCurve myTestCurve;

This will give you the curve in the inspector, with the curve editor window when you click on it
image

To use it, you can evaluate the time to get the curve value

float value = myTestCurve.Evaluate(0.5f);

Ah nice, thanks. This explains why I did not see it while looking for stuff like “function” and “Bezier” in component creation search bar.

Much simpler than the solution used in the training.

I was just wondering if there is a specific reason @CodeMonkey did not use this in training video and picked a less intuitive way to do it. Reading the comments for the lecture I see some people just used Vector3.MoveTowards(Vector3, Vector3, float) to avoid overshot. While less generic than animation curve it is the simplest. If second edition of the training is ever made this one should be a candidate for improvement.

Quite probably because this is the method that best describes what actually happens within a move function.

transform.position += moveDirection * moveSpeed * Time.deltaTime;  

is the classic formula for determining frame based movement.

There are, of course, many ways to achieve the same goal.

2 Likes

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

Privacy & Terms