This is a solution… not a question, C# files are attached at the bottom of this post…
So I started using iTween yesterday, and I’d like to say thank you to the developer for writing this tool, It will be a massive time saver for me I have no doubt.
While using it though I came to realize that it was missing 2 specific features that I need for my current work.
- The ability to sample a curves position uniformly based on length (ie constant speed in real world units)
- The ability to get the normal of a curve at a given point.
. Only way I can see to do this is using a dummy object and the PutOnPath() method, then read the rotation of the object.
A quick search online revealed many people having this same issue (at least with #1), so I’ve come up with a solution to both that will hopefully help others.
Other peoples workarounds seemed to suffer from accuracy problems that I think this one should help eliminate, also it doesn’t require re-sampling to a new curve (my first thought), and doesn’t add much at all to ram or cpu usage.
The concept is to use the AnimationCurve type built into unity and create a lookup table (ie. valueA == valueB type relationship).
The class is simple, meant only to answer the 2 features missing above.
All movement will have to be done in code, I’ve included a simple example script, as well as an Editor script that makes creating the spline much easier, (It is my first editor script so it looks rather messy but it works well, all controls are straight forward and shouldn’t require any explanation).
Other than the control points of the curve the only other value is “samples”, this number can be kept very low and still get good results since the AnimationCurve automatically interpolates the results of my sampling. The only time I’ve needed to up the samples is when you are looping the spline and the tangent from first and last point is high, or if your curvature has massive changes between points.
Numbers between 5-20 should be fine for almost every situation.
As commented in the class, if the samples setting or the points in the array for the curve change you’ll want to run Rebuild() on the curve, The editor script takes care of this automatically, but if you change point locations in code you’ll have to run Rebuild() manually.
One thing I couldn’t figure out, is how do I disable the control point handles from being movable if I’m orbiting in the scene view with alt key pressed down? Hopefully someone here can enlighten me?
Hope its of use to others.
Here are the files…
New Component that uses iTween
1248422–53881–$MotionPath.cs (6 KB)
Inspector for MotionPath type
1248422–53882–$MotionPathEditor.cs (4.54 KB)
Example of how to use
1248422–53888–$FollowMotionPath.cs (793 Bytes)