grid movement via touch on iphone

Hello all,

here is the script that works really well, but with one glitch,and that glitch i will post about below the script.

var theplayer: Transform;
var tester: Vector3;
var smooth = 10.0;

function Update()
{
	var count: int = Input.touchCount;
	if(count == 1)
	{
		touch = Input.GetTouch(0);
		if(touch.phase == TouchPhase.Began)
		{
			var ray = Camera.main.ScreenPointToRay(touch.position);
			var hit: RaycastHit;
			if(Physics.Raycast(ray, hit))
			{
				if(hit.collider.gameObject.tag == "cube")
				{	
					tester = hit.collider.gameObject.transform.position;
				}	
			}	
		}			
	}
	if(tester.x > theplayer.transform.position.x || tester.x < theplayer.transform.position.x)
	{
		MoveLeftOrRight();	
	}
	if(tester.y > theplayer.transform.position.y || tester.y < theplayer.transform.position.y)
	{
		MoveUpOrDown();	
	}
}


function MoveLeftOrRight()
{
	theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(tester.x,theplayer.transform.position.y,1),(Time.deltaTime *smooth));
}

function MoveUpOrDown()
{
	theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(theplayer.transform.position.x,tester.y,1),(Time.deltaTime *smooth));
}

Now most of you who test this, will think (what is he on about, it works?!), yes but i do not want the diagonal movement. I strictly want it to work on a single axis movement (also known as 4 moveable directions).

I have attempted to say that if the values of tester.x and tester.y have changed to be greater than or less than the players position then do not move, but my code wants to be ignored!

Please if anyone can help me, a pint is on me when your next in wales!

Update!!!

var theplayer: Transform;
var tester: Vector3;
var smooth = 10.0;
var moving: boolean = false;

function Update()
{
	var count: int = Input.touchCount;
	if(count == 1)
	{
		touch = Input.GetTouch(0);
		if(touch.phase == TouchPhase.Began)
		{
			var ray = Camera.main.ScreenPointToRay(touch.position);
			var hit: RaycastHit;
			if(Physics.Raycast(ray, hit))
			{
				if(hit.collider.gameObject.tag == "cube")
				{	
					tester = hit.collider.gameObject.transform.position;

				}	
			}	
		}			
	}
	
	if(!moving && (tester.x != theplayer.transform.position.x || tester.y != theplayer.transform.position.y))
	{
		if(Mathf.Abs(tester.x - theplayer.transform.position.x) > Mathf.Abs(tester.y - theplayer.transform.position.y))
		{
			moving = true;
			MoveLeftOrRight();
		}	
		else
		{
			moving = true;
			MoveUpOrDown();	
		}
	}
	
}
function MoveLeftOrRight()
{
	theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(tester.x,theplayer.transform.position.y,1),(Time.deltaTime *smooth));
	yield WaitForSeconds(1);
	moving = false;
}

function MoveUpOrDown()
{
	theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(theplayer.transform.position.x,tester.y,1),(Time.deltaTime *smooth));
	yield WaitForSeconds(1);
	moving = false;
}125

125

Ahh, I see. Because the initial check is in the update, it will keep calling the function over and over.

var moving : boolean = false;
function Update()
{
//stuff
if(!moving && (tester.x!=theplayer.transform.position.x || tester.y!=theplayer.transform.position.y))
{
     if(Mathf.Abs(tester.x-theplayer.transform.position.x)>Mathf.Abs(tester.y-theplayer.transform.position.y))
     {
           moving=true;
           MoveLeftOrRight();
     }
     else
     {
           moving=true;
           MoveUpOrDown();
     }
}

Also, throw a yield statement in the move functions after the lerp and reset the moving variable.

function MoveLeftOrRight()
{  
     Vector3.Lerp(theplayer.transform.position,Vector3(tester.x,theplayer.transform.position.y,1),(Time.deltaTime *smooth));
    yield WaitForSeconds(1);
    moving=false;
}

Check if the x movement is larger than the y movement. If so, move in the x direction. If not, move in the y direction. Unless the user moves perfectly diagonally where x==y, then the movement will be predictable.

Hi … I have an object that moves in the form of a grid, by touching it on the phone. How do I write code to move the unit left, right and forward?