- Home /
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?
Thinking about it some more, what bothers me is that in $$anonymous$$oveTo() I can use a "path" key in the hashtable and thus benefit from the visual path editor, but in $$anonymous$$oveUpdate() I have to specify a position in space. So I guess I need to figure a way to reference the path in a different way with $$anonymous$$oveUpdate()...
Answer by supamanu · Jul 08, 2012 at 04:02 PM
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 :)
Answer by Bitpocketer · Dec 30, 2015 at 08:51 AM
I in my project have come to the point where i need to achieve the same functionality. Did this work for you?
Your answer
Follow this Question
Related Questions
C# - create an iTweenPath in the Sourcecode 1 Answer
Using iTween to move a gameobject from node to node 1 Answer
Combine iTween with physics acceleration 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers