Posts Tagged constant speed

Unity curved path following with easing at constant speed



Download

The Problem

I have a character than needs to follow paths curved through a series of control points as it moves around the world. I also want to ease in and out to speed of movement as the path starts and ends. Sometimes in the middle of a path follow I need to change my mind and have some other path taken or perhaps change from path following to another state. This isn’t possible with iTween at constant speed unless you use PutOnPath, which doesn’t allow easing functions. I would also like to be able to move at a constant speed – either by ignoring the length of individual path elements (and thereby specifying at time fraction for the whole path) or by having a maximum speed my object can travel at.

Solution

Building on the great work done by Andeeee and Renaud, I’ve made a path following class that enables you to specify from 2 to N points and have it build a curved path (if it has more than 2 points!), you can then follow that path using a time from 0…1 and also apply an easing function to make the start and the end points smooth.

It supports Linear, Sine, Quadratic, Cubic, Quintic and Quartic easing functions with control on over whether the path is eased in and out separately.


Line 1 – EaseIn, Line 2 – EaseOut, Line 3 – EaseIn and EaseOut

This allows fine grained control of an eased, spline path in an Update function that is not possible using iTween – this enables you to abort a movement halfway through if something more important is happening .

To use it you download and import the unity package.

Eased spline paths

To use no easing call Spline.Interp(arrayOfPoints, time). Where time is a float between 0 and 1.

The array of points can be an array of Vector3s, GameObjects or Transforms which are implicitly converted to a Spline.Path instance containing an array of Vector3s. DON’T BOTHER to create your own Spline.Path instances!

e.g. transform.position = Spline.Interp(myPathObject.GetComponentsInChildren<Transform>(), time);

This would use the myPathObject and all of its children to define the path. (For example only, don’t go calling GetComponentsInChildren every frame).

To ease the function you can supply one or more of the easing parameters:

Spline.Interp(arrayOfPoints, time, easingType /* e.g. EasingType.Sine */, easeIn /* e.g. true (default) */, easeOut /*e.g. false (default true) */ );

The Spline class also supports Andeeee’s Velocity and GizmoDraw functions to help you with debugging.

PLEASE NOTE: the function uses a different algorithm for short paths so that you can get a number of different effects. If your path has 2 points it does a linear interpolation, 3 points does a Quadratic spline and 4 points does a Cubic spline. The 5 or more point version uses Catmul Rom splines and ensures that the path passes through every point.

You can double up start and end points easily with the Spline.Wrap() call on your path array – you may want to do this for short paths if you want to ensure that all of the points are passed through – or just do it yourself. There should be no effect if you are using InterpConstantSpeed or MoveOnPath when you do this (See below).

Constant Speed

I’ve added an InterpConstantSpeed function too, this tries to make the speed of movement constant between sections. It works in the same way as Interp, with a time value between 0 and 1.

Now just a note on that.  If your path points are moving then you need to watch constant speed interpolations as one section getting much longer or shorter could cause the position to vary wildly.  If your path sections are increasing or decreasing in size then you are better off using Interp and trying to make sure that each path section is roughly the same size (short sections will appear to have lower velocity than large sections, so they need to be “kind of” the same size).  Interp says if your path has 5 sections, each one takes 1/5 of the time to cross.  If you are in section 2 and section 3 gets bigger, you will complete section 2 in the normal time.  Using constant speed, the position at time “t” is along the magnitude of the whole path, if it gets longer or shorter then the position at “t” will change between sections moving the Vector unrealistically forwards or backwards. This is a problem if you are in an early stage of the path and a later path point, far off screen, is moving.  The Vector will stutter for apparently no reason.  If you want constant speed then you need to ensure that your path points maintain exactly the same distance from each other when they move (they need to move in an arc centered on the previous end point).

The second point is that applying “Constant Speed” and “Easing” appears to create a paradox; but InterpConstantSpeed accepts easing functions! Well the “Constant” bit refers to the sizes of path sections being converted to a path magnitude and the easing is applied to the “time” fed into the path calculator: so you can have your cake and eat it 🙂

Path Movement and Character Speed

I’ve also added a speed limited version, this also solves the constant speed issue by having a maximum possible movement speed, you can therefore move those path elements as often as you like – but you can’t specify the time it takes to complete the path. It works kind of like a SmoothDamp – here’s how you go about it:

public class TestFollow : MonoBehaviour {

	public Transform[] path;

	float t = 0;

	// Update is called once per frame
	void Update () {
		transform.position = Spline.MoveOnPath(path, transform.position, ref t, 0.5f);

	}
}

Basically you pass the path, the current position and a reference to a float that will contain the currently targeted path point, the final parameter shown here is the number of world units per second to move.

This routine also has the added benefit that it will move to the start of the path before following it.

Here are all of the parameters to MoveOnPath:

Parameter Meaning
pts An array of transforms, Vector3s or GameObjects that describe the path
currentPosition The current position of the object being moved
pathPosition The position along the path, this is updated by the call so must be a variable of the class
(Optional)rotation A quaternion that will be updated to show the rotation that should be used. This can change quickly for slow moving objects, so you might want to smooth it. See below.
maxSpeed The maximum number of world units to move in one second, default is 1
smoothnessFactor The smoothing factor is the number of steps on the path that will be used, default is 100
ease The type from EasingType.Linear, EasingType.Sine, EasingType.Cubic, EasingType.Quadratic, EasingType.Quartic and EasingType.Quintic. Default is Linear
easeIn true if you want to ease in the function or false if you don’t. Default is true
easeOut true if you want to ease out the function or false if you don’t. Default is true

Here’s an example of the version that returns a rotation, in this example I use the SmoothQuaternion from a previous post to smooth out the rotations.

#pragma strict

var pathPoints : Transform[];

var t : float;
var sr : SmoothQuaternion;

function Start() {
	sr = transform.rotation;
	sr.Duration = 0.5f;
}

function Update () {
	var q : Quaternion;
	transform.position = Spline.MoveOnPath(pathPoints, transform.position, t, q, 0.5f, 100, EasingType.Sine, true, true);
	sr.Value = q;
	transform.rotation = sr;

}

Javascript

You can use the Spline class from JavaScript, as long as the .cs files are somewhere inside a Assets/Plugins folder (the download does this) and that JavaScript is not inside a Plugins folder.

This example will follow a constant speed path for 20 seconds.

#pragma strict

var pathPoints : Transform[];

var t : float;

function Update () {
	transform.position = Spline.InterpConstantSpeed(pathPoints, t, EasingType.Sine, true, true);
	t += Time.deltaTime/20;
}

The next one moves an item on a path at a maximum of 0.5 world units per second

#pragma strict

var pathPoints : Transform[];

var t : float;

function Update () {
	transform.position = Spline.MoveOnPath(pathPoints, transform.position, t, 0.5f, 100, EasingType.Sine, true, true);

}

, , , , , , ,

37 Comments