- Home /
iTween, moving to a node on a path.
I'm making a board game and I decided to try and use iTween for the pathing around the board. I have 42 tiles around my board, each with it's own node. I can get the gameObject to go completely around the path, but I'm lost at how to get it to only move to the node I need it to. I'm using a dice in the game to deiced how many spots the gameObject is to move if that makes a difference.
Would really appreciate any help, an example of a scenario would be if the game object was on node 3 and rolled a 6 it would go to node 9.
Note: I'm writing this in javascript.
there's only one node per game position. in this picture each of those dots has it's own node. http://i.imgur.com/fd7ODyw.jpg
Answer by robertbu · Mar 19, 2013 at 03:20 AM
I should have read the question more carefully since you say "each with it's own node." Anyway, if you can move around the whole path, somewhere you have an array of either Vector3s or Transforms that you pass to iTween. Since you have a singe ordered path you can extract any part of it to build a list of Vector3s to pass to iTween. Note you should label your positions starting with 0. I'm using arv3AllPos as a list the list for the whole path. You array will have a different name. MakePath extracts any specified segment out of the total array to use as the path for a player move on the roll of the dice.
var arv3AllPos : Vector3[];
function MakePath(startNode : int, endNode : int) : Vector3[] {
var v3New = new Vector3[endNode - startNode + 1];
for (var i = startNode; i <= endNode; i++)
v3New[i-startNode] = arv3AllPos[i];
return v3New;
}
This is just what I needed, I'll try it out when I get home but it looks perfect. I'll let you know if it worked soon. Thanks you.
I'm really close to getting it I just need a little more help with it. Right now as a test I'm using this as my code:
var Pathcoords : Vector3[];
Pathcoords[1]=Vector3(85.2, 7.41 ,117.4);
Pathcoords[2]=Vector3(85.7, 7.41 ,121.8);
Pathcoords[3]=Vector3(85.7, 7.41 ,126.0);
Pathcoords[4]=Vector3(86.7, 7.41 ,130.8);
Pathcoords[5]=Vector3(87.0, 7.41 ,134.8);
Pathcoords[6]=Vector3(87.8, 7.41 ,138.7);
function $$anonymous$$akePath(startNode : int, endNode : int) : Vector3[] {
var v3New = new Vector3[endNode - startNode + 1];
for (var i = startNode; i <= endNode; i++)
v3New[i-startNode] = Pathcoords[i];
return v3New;
}
function Start () {
var v3path:Vector3[] = $$anonymous$$akePath(1,3);
iTween.$$anonymous$$oveTo(gameObject,v3path[0],1);
iTween.$$anonymous$$oveTo(gameObject,v3path[1],1);
iTween.$$anonymous$$oveTo(gameObject,v3path[2],1);
}
When running this code I get two IndexOutOfRangeException: Array index is out of range errors. I feel like I should be defining the size of the arrays but can't figure out how for some reason.
here's the exact errors I get:
IndexOutOfRangeException: Array index is out of range. player1.$$anonymous$$akePath (Int32 startNode, Int32 endNode) (at Assets/Scripts/player1.js:20) player1.Start () (at Assets/Scripts/player1.js:32)
and
IndexOutOfRangeException: Array index is out of range. player1.$$anonymous$$ain () (at Assets/Scripts/player1.js:7)
I know this is a really simple problem but I'm new to javascript and just can't see what's wrong. Thanks you for your help.
You need to create the memory for the array, not just declare the variable. For example:
var Pathcoords : Vector3[] = new Vector3[50];
You an also modify it in the Inspector ins$$anonymous$$d. If you click on the object this script is attached to, in the Inspector you will see a triangle next to Pathcoords. You can click on the triangle to open it up. Set it size, and then you can enter the values in the inspector. If you don't want to use the inspector, I recommend making the variable private:
private var Pathcoords : Vector3[] = new Vector3[50];
Excellent, everything is working the way I want it to. You have my thanks. I really appreciate all the help.
Your answer
Follow this Question
Related Questions
iTweens MoveTo and "path" property 7 Answers
iTween - look for new path, move to or reverse at end? 0 Answers
Pathfinding help 1 Answer
itween MoveTo IndexOutOfRangeException 0 Answers