Hermite Spline Controller

From Unify Community Wiki
Jump to: navigation, search

Contents

Download

JavaScript version

First version: Download SplineController.zip

Latest version: Download SplineControllerJavascript.zip

C# version

Download SplineController C#.zip by Benoit FOULETIER

Just transcribed everything to C# without any functional change.
Only tweak: the SplineInterpolator was added at runtime, or during OnDrawGizmos using new SplineInterpolator(), which is incorrect => it's now added at design-time (added RequireComponent to simplify things).
Note: that tweak causes the routine to keep resetting the SplineInterpolator, which prevents it from functioning correctly in the game (at least, in the iPhone version of the Unity IDE this is true). You will also need to add "DestroyImmediate(interp)" as the end of OnDrawGizmos.

Unity iPhone C# version (no generics)

Download SplineController C# for iPhone.zip by Matt Maker

Tweaked, as minimally as I could figure out, to allow this to run in Unity iPhone. It's not pretty; feel free to refactor it!

C# version extended with break time

Download SplineController C# extended.zip by Francesco Montorsi

This is an extended version of Benoit FOULETIER's version, supporting "break times": i.e., the developer may specify in this version the amount of time the camera should stop at each spline node (if zero, then the camera does not stop and the behaviour is identical to the non-extended version).

Description

A hermite spline interpolator. It allows you to move your GameObject smoothly through an easily defined path, interpolating both position and orientation.

It features two orientation modes:

  • NODE : It directly takes the orientation from the control point
  • TANGENT : Calculates the orientation based on the tangent to the curve in the control point.

It has a feature called "Auto Close", which makes automatically both starting and end points and tangents coincident.

It also has two playback modes, "ONCE" and "LOOP".

Usage

  • Add to your GameObject the script "Components/Splines/Spline Controller"
  • Create a GameObject which is going to be the parent of all the control points.
  • Create children GameObjects, which are your spline control points. They will be read in alphabetical order.
  • Asign the parent of the control points to your moving GameObject in the "Spline Parent" slot.


That's all.

A video is priceless Video Tutorial/Demo

A picture is worth a thousand words:

SplinePicture.png

C# codefix?

I was running into problems with the transform list being a null object when OnDrawGizmos was accessing it, here's a fix:

<csharp > /// <summary> /// Returns children transforms, sorted by name. /// </summary> Transform[] GetTransforms() { if (SplineRoot == null) return new Transform[] { };

List<Component> components = new List<Component>(SplineRoot.GetComponentsInChildren(typeof(Transform))); List<Transform> transforms = components.ConvertAll(c => (Transform)c);

transforms.Remove(SplineRoot.transform); transforms.Sort(delegate(Transform a, Transform b) { return a.name.CompareTo(b.name); });

return transforms.ToArray(); } </csharp >

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools