using iTween in Update() for greater control

I’ve just started playing around with iTween (and its nifty visual path editor add-on). I was hoping it would help me do one specific thing: control the movement of an object bound to a complex path. But by control, I mean that the speed along the path can vary at any time, including the object stopping or reversing at any point along the path based on user input.

Unfortunately most of the material I have come across points towards a “fire and forget” behaviour, e.g. the MoveTo() method is called in Start() and either the time or speed is fixed for the whole duration of the tween.

I can see in the doc that you can Pause() and Resume(), and there’s a MoveAdd() method that seems designed to be used in Update() but is much poorer for options. And I haven’t found any good examples to help me figure out how to use them.

Has anyone come across an implementation resembling what I’m trying to achieve?

So, after downloading the examples provided by pixelplacement, I hacked this together. Is this the most flexible way to do it? I don’t know, we’ll see how far I can make this work until I try to do something too fancy for this approach:

public class Player : MonoBehaviour {

// instance variables
public float playerSpeed=0.5f; // speed of player on path
public Vector3 controlPath; // reference to the path

private float pathPosition=0;

void Awake() {
controlPath = iTweenPath.GetPath (“MainPath”);
}

void OnDrawGizmos() {
iTween.DrawPath (controlPath, Color.red);

}

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (Input.GetKey(“space”)) {
pathPosition += playerSpeed * Time.deltaTime;
Vector3 coordinateOnPath = iTween.PointOnPath (controlPath, pathPosition);
iTween.MoveUpdate (gameObject, iTween.Hash (“position”, coordinateOnPath,
“time”, Time.deltaTime, “orienttopath”, true));
if (pathPosition>=1) pathPosition=0; // loop to start of path

}

}
}

The good news is I understood how to reference a path drawn with the visual editor :slight_smile:

I in my project have come to the point where i need to achieve the same functionality. Did this work for you?