I would like to script the animated movement of an object between two positions. So there is the start vector and the end vector. The start and end positions are important, and the trajectory should look something roughly like in the image below, but it shouldn't be too precise.
Because the end position should be a fixed one, I can't really use rigidbody physics. I tried using Vector3.Slerp and Vector3.Lerp, but I couldn't manage to get an acceleration at the end of the trajectory curve.
Could you please give me a few hints, as I forgot the math behind these things?
Thanks a lot!
asked Nov 26 '09 at 04:02 PM
You're on the right track with Vector3.Lerp (but not Slerp - that's more for interpolating directional vectors rather than positions).
What you need is to just add a curve-based value to the object's height in addition to the straight-line interpolation that Lerp gives you. You can get a nice curved value range which goes from 0 to 1 and back to 0, by feeding a range from zero to PI into the Mathf.Sin function.
Here's a small c# example script which demonstrates:
answered Nov 26 '09 at 04:19 PM
Thanks a lot for your quick reply. In my case your code didn't really work. Using Time.time isn't the best idea (it would probably work only when you first hit play in the editor, as it accumulates)
Here's the code I used, which does pretty much what I wanted:
Because I added the distance between the 2 points in the equation (the magnitude), the animation has the same speed regardless of the distance between the 2 points. If you know any good way to optimize the above code, to run even faster, please let me know.
answered Nov 26 '09 at 05:19 PM