# How to move an object on a random path with iTween

I’m trying to make a ‘track’ that is generated randomly as a object travels along that path.

For this I’m using iTween, the issue I’m having is that I can’t seem to generate a random path of X nodes and animate the object on it.

iTween doesn’t let me remove nodes and my only option is to move all of the nodes but this leads to jumps in the objects postion.

When the object goes past say 2 nodes I want to delete the older nodes and add new ones so the path will be ‘infinite’.

Is there a way to make this happen? Any help would be great!

PD: I have seen this example iTween for Unity by Bob Berkebile (pixelplacement) but I can’t figure out how this is done because I can’t find the code.

Here is a bit of code that recalculates a path when you reach the end. There are no speed issues moving from one path to the next. There is sometimes just a bit of roughness in the LookAt() during the transition (and this may be solvable). Note I’m using iTween.PointPonPath() and iTween.PutOnPath() for a bit more control. To test this code:

• Create a large plane with position (0,-.5,0) and scale (100,100,100).
• Put a texture on the plane so you can see movement
• Add a cube at the origin
• Add a directional light to see the cube and the plane
• Add a Smooth Follow script to the camera with the cube as the target
• Add the following script to the cube

``````#pragma strict

var angle = 20.0;  // Maximum angle offset for new point
var speed = 8.0;   // Units per second
private var pos = 0.0;
private var segLength = 2.0;
private var path = new Vector3[100];

function Start () {
path[path.Length-2] = Vector3.zero;
path[path.Length-1] = Vector3(0,0,segLength);
RecalcPath();
}

function Update () {
iTween.PutOnPath(gameObject, path, pos);
var v3 = iTween.PointOnPath(path, Mathf.Clamp01(pos+.01));
transform.LookAt(v3);
pos = pos + Time.deltaTime * speed / path.Length;
if (pos >= 1.0) {
pos -= 1.0;
RecalcPath();
}
}

function RecalcPath() {
var v3 = path[path.Length-1] - path[path.Length-2];
path[0] = path[path.Length-1];
for (var i = 1; i < path.Length; i++) {
var q = Quaternion.AngleAxis(Random.Range(-angle, angle), Vector3.up);
v3 = q * v3;
path *= path[i-1] + v3;*
``````
• }*
}
The code walks a cube on the XZ plane. It generates the path in equal length segments and uses ‘angle’ to generate a random direction within an angle of the previous segment at each point.