Move an object down a little, then up to a position

Hey,

I have a game object, that I want to move down a little, and then up to a specific position in t time.

Lets say my object is at P0 position (0, 22, 0), and has a speed of S0(-15).

Now, I want to modify it’s speed variable, to decrease (it will be calculated based on a distance of an object, lets say it is 2 at the moment), and after that, I want to increase it to move the object up to P1 position smoothly (it is constant, (0, 26, 0)), and do all of this in t time (it will be calculated as well, lets say it is 0.75 seconds at the moment).

It should move in a path like in the diagramm below (so, we should smoothly decrease the speed value, then increase it, and smoothly stop).

9257-diagram.png

My question is, what is a good way to do this?
The object is moving by this line (so the object only moves only up and down):

thisTransform.position += Vector3.up * speed * Time.deltaTime;

A good way: Animation A more dynamic way: 3d Bezier Curves Don't try to do it procedurally

1 Answer

1

In addition to just using an Animation clip, you can also use an Animation Curve. This is one of the more powerful features of Unity that few people think to use.

Create a public member of type AnimationCurve. In the inspector you can edit that property with an interactive curve editor. In your code use the Evaluate() method on the curve member. Just pass in T.

In your case you might have a curve for the Y position and another for speed and just plug the evaluations into your maths.