- Home /
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
Answer by kieblera5 · Jul 08, 2011 at 01:29 PM
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;
}
its very clunky, meaning i have to repeatedly press the grid location(of where i want the player to go) and its still diagonal, perhaps i repost my code to the state its at? i will do it now to save time if i need to...not sure where the 125 are co$$anonymous$$g from in the code
Answer by kieblera5 · Jul 07, 2011 at 03:05 PM
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.
this wouldnt really work though would it? what if i need to start moving in the opposite direction, i'd be checking if the values are less than the other value, where as at the same time checking if they are larger...
Use the absolute value of the difference of the positions. Save the initial position of the touch on TouchPhase.Began and then compare it to the current position when TouchPhase.$$anonymous$$oved or TouchPhase.Stationary is true. You probably also want to check if the player moved a certain distance first, unless you want it to "follow" your finger, but on fixed axes. To summarize: Save initial position. Compare initial and current positions. If the distance is greater in the x, move x direction. Else if the distance is greater in the y, move y direction.
i should explain i just want to be able to select a tile on the grid (like i am) and then the player will move towards it at a slow speed, but only on four moveable directions
Ahh, my fault. I thought you were trying to drag the object. Try something like this:
if($$anonymous$$athf.Abs(tester.x-theplayer.transform.position.x)>$$anonymous$$athf.Abs(tester.y-theplayer.transform.position.y)) { $$anonymous$$oveLeftOrRight(); } else { $$anonymous$$oveUpOrDown(); }
If you also want to move up/down after a left/right, then in the bottom of the $$anonymous$$oveLeftOrRight function, add a check to see if the y needs to be changed as well. i.e. function $$anonymous$$oveLeftOrRight() { theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(tester.x,theplayer.transform.position.y,1),(Time.deltaTime *smooth)); if(tester.y!=theplayer.transform.position.y) { $$anonymous$$oveUpOrDown(); } }
The same would go for the up/down function, just check the x and call the other function.
Answer by sycr99 · Nov 05, 2020 at 09:29 AM
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?
Your answer
Follow this Question
Related Questions
Help with iphone/android touch controls 2 Answers
Separate functions for separate touches 1 Answer
How to drag object along with dragging touch? 1 Answer
Massive Touch Lag on mobile devices? 3 Answers