- Home /
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 http://itween.pixelplacement.com/examples.php#randomPaths but I can't figure out how this is done because I can't find the code.
First, he sells the example packages. I believe $10 gets you the whole set. There is an "Add to Cart" link at the top of the page. Second, I may have an idea that would give you want you want, but why can't you just generate a whole new path each time your game object arrives at the end? Why do you need to modify the path as you go?
wouldn't it jump a bit or change speed when moving from one path to another?? When I tried to change path it didn't look very smooth. $$anonymous$$aybe I was implementing it wrong.
Answer by robertbu · Oct 22, 2013 at 04:25 PM
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[i] = 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.
Robert that worked great. The only small issues is that the 'cube' keeps changing the orientation and looks a bit jagged so I just added a new cube that follows that one and controled its orientation seperatly and it looks perfect! Thank you for your help
If you don't want the cube to change its orientation, comment out lines 17 and 18.
Your answer
Follow this Question
Related Questions
choose random waypoint 1 Answer
iTween Help Please MoveTo not working 1 Answer
iTween - Path Movement by Touch 0 Answers
How do you create random paths in iTween? 0 Answers
Generate random path in grid 1 Answer