Transform Position question

I have an object thats going back and forth in the X position, from -120 to -91 when it gets to -91 it comes back to -120 then starts all over again.

How would I say in code if its going to -91 //DoSomething.

and when it comes back going to -120 //DoSomethingElse?

I know Im doing it wrong but I cant think how to do it the correct way :stuck_out_tongue: Can someone show me the right way?

var ghost : GameObject;

function backforth()
{
	iTween.MoveTo(ghost,{"x":-91, "time":timeBackForth, "easetype":"linear","looptype":"pingPong"});
	
		if(ghost.transform.position < -120)
		{
			animation.CrossFade("walk_left90");//DoSomething
		{
		if(ghost.transform.position > -91)
		{
			animation.CrossFade("walk_right90");//DoSomethingElse
		}		
}

Im searching the tutorials now but would appreciate the help, before I get even more lost and off topic :slight_smile:

You should probably use an update callback. Look at the documentation for ā€œonUpdateā€.

Ok it says ā€œfor the name of a function to launch on every step of the animationā€ So I tried this:

function backforth()
{
	iTween.MoveTo(ghost,{"x":-91, "time":timeBackForth, "easetype":"linear","looptype":"pingPong","onupdatetarget": gameObject, "onupdate":"back"});
	animation.CrossFade("walk_right90");	
		}
function back()
{
	print("palying the other animation");
	animation.CrossFade("walk_left90");	
}

which works because I got the print message but because the animation is a pingPong I need one animation when he’s going one direction and another animation when he’s going the other direction?

I think I gotch ya instead of having a looptype just bounce it back and forth through 2 functions.

If thats what you meant? thank you, if not, thank you anyway :smile:

Cheers,
Zeek

Edit:

Just incase anyone else needs something like this:

function ActivateGhost()
{
	iTween.MoveTo(ghost,{"z":300, "x":-120, "time":timeWalking, "easetype":"linear","oncompletetarget": gameObject,"oncomplete":"backforth"});
	animation["walk_left45"].speed = -1.5;//play animation backwards 
	animation.CrossFade("walk_left45");
}
function back()
{
	
	iTween.MoveTo(ghost,{"x":-120, "time":timeBackForth, "easetype":"linear","oncompletetarget": gameObject, "oncomplete":"backforth"});
	print("palying the other animation");
	animation.CrossFade("walk_left90");	
}
function backforth()
{
	iTween.MoveTo(ghost,{"x":-91, "time":timeBackForth, "easetype":"linear","oncompletetarget": gameObject, "oncomplete":"back"});
	print("Ok back here again");
	animation.CrossFade("walk_right90");	
}

Strange it prints the second print(ā€œOk back here againā€); before it prints print(ā€œpalying the other animationā€); But its working :smile: