- Home /
Using iTween to move a gameobject from node to node
Hello all,
I am using the iTween path for a level select menu. I have the path nodes set up to the location of the level buttons. I have a user icon that I want to move from node to node when the level button is pressed. I've tried using the documentation and other forum posts but I wasn't getting anywhere fast! So, my user icon starts on level 1. When I click level 6 I want the user icon to travel the path of the iTweenPath until it gets to the sixth node(same location as the level 6 button). How can I accomplish that? I would post my code but its pointless b/c its very broken. I came close to what I want with this code but the user icon gameobject displayed very wierd behavior(usericon transporting around the scene).
Before trying the .moveto() method I was using the .putonpath() which just teleported the usericon to the button location. It was great minus the fact it didn't walk there. Any suggestions?? If you need code I'll upload it but like I said before it really doesn't work at all.
Also, I got the moveto to work but the usericon wouldn't move along the iTweenPath it would just pan from button location to button location
Update 1: Ok, so I got close to what I want.
iTween.MoveTo(userIcon.gameObject, iTween.Hash("position", new Vector3(worldPath.nodes[moveDestination].x, worldPath.nodes[moveDestination].y, 0.0f), "path", iTweenPath.GetPath("World0Path"), "movetopath", false, "time", 5.0f, "looptype", "pingPong"));
This line of code moves the usericon along the path but it always moves it to the last node in the iTweenPath. I thought I could use the position parameter to be the end position but clearly I was wrong.
Update 2: The iTween documentation says the position arg is supposed to be the "for a point in space the GameObject will animate to." With my line of code(still the one from above) no matter what I set the position arg to be it still always ends at the end of the iTweenPath http://itween.pixelplacement.com/documentation.php
Answer by Screenhog · Nov 27, 2012 at 06:54 PM
The key to this is to give it the correct path.
Right now, you're always giving it the same path, right? (iTweenPath.GetPath("World0Path")) Instead, give it only the nodes it needs.
var nodes: Vector3[]; //master list of all nodes, in order
var path: Vector3[]; //the path that you will use for iTween
function ChoosePath (pathStart: int, pathEnd: int) {
path = new Vector3[Mathf.Abs(pathEnd-pathStart)+1];
//make the path move in the opposite direction is pathStart is greater than pathEnd
var sign: int; //negative or positive value
if (pathStart > pathEnd) {
sign = -1;
} else {
sign = 1;
}
//assign values from nodes to path
for (var i:int; i<path.length; i++) {
path[i] = nodes[pathStart+(sign*i)];
}
}
This will assign a path based on the start and end points you choose. Use that value with your iTween.MoveTo.
ChoosePath(2, 5); //move from element 2 in nodes to element 5 in nodes (from nodes[2] to nodes[5])
Wow this great! Thank you so much! It works perfectly now!
Hey, I have one more question for you. So everything works great like I said before. But I didn't notice this behavior. When the user icon is moving from button x to button y and I click on button z while the user icon is still moving to button y it will teleport to button y position and then move to button z. How can I have it move from where ever it was to button z. Or how can I have it wait to move again after the user icon finishes its path?
One way: When your icon reaches the destination, check to see if path.length-1 (the last node of the path) is equal to the node that you're currently on. If it's not, call another iTween.$$anonymous$$oveTo to go to the new location.
This will require you to track the node that your icon is occupying, if you're not already doing so.
What if the icon is between two nodes? I can't check if it's on a node if it is in between two nodes. Also, what is$$anonymous$$oving bool are you talking about? One that I handle myself or one that iTween has b/c I don't see a is$$anonymous$$oving variable within iTween.
Your answer
Follow this Question
Related Questions
iTween movetopath 1 Answer
How to set iTweenpath dynamically 1 Answer
Combine iTween with physics acceleration 0 Answers
iTween MoveTo 4 Answers
Multiple Cars not working 1 Answer