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