Touch buttons for step movememt

Hello ,

I am new to unity . Please help me on this simple issue.

I have added four sprites “circle” on my screen. Each for up , down , right , left. I want to achieve that when I touch on the specific circle. the player should move by exact unit like 1 tile at a time.(it is a grid based game).I want to achieve the movement same as in chess. Just touch a button and move into that direction to next adjacent tile.

I have added raycast to detect which sprite is touched by user. To move I have added addforce function to move the player . So what is happening is When I touch a circle sprite it continues to move in that direction.

Can you guys recommend anything other than addforce.

using UnityEngine;
using System.Collections;

public class touchcontrol : MonoBehaviour {
public GameObject player;
public float speed=0;
private Rigidbody2D rb2d;
void Start () {
rb2d = player.GetComponent ();
}

// Update is called once per frame
void Update () {
 if (Input.touchCount > 0) {
		if(Input.GetTouch(0).phase==TouchPhase.Began)
		{
			RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), -Vector2.up);
			

			
			if (hit.collider != null)
			{
				
				if(hit.collider.tag=="up")
				{
					Vector2 move = new Vector2 (0,1);
					rb2d.AddForce (move * speed);
					Debug.Log("up");
				}
				if(hit.collider.tag=="down")
				{
					Vector2 move = new Vector2 (0,-1);
					rb2d.AddForce (move * speed);
					Debug.Log("down");
				}
				if(hit.collider.tag=="right")
				{
					Vector2 move = new Vector2 (1,0);
					rb2d.AddForce (move * speed);
					Debug.Log("");
				}
				if(hit.collider.tag=="left")
				{
					Vector2 move = new Vector2 (-1,0);
					rb2d.AddForce (move * speed);
					Debug.Log("left");
				}
			}

		}
	
	
	
	
	}

}

}

I have made this change in the code.Its working fine for now.If any one can suggest some thing better.It will be helpful.

if(hit.collider.tag==“up”)
{
Vector2 move = new Vector2 (player.transform.position.x,player.transform.position.y+1);
//rb2d.AddForce (move * speed);
player.transform.position=move;
Debug.Log(“up”);
}
if(hit.collider.tag==“down”)
{
Vector2 move = new Vector2 (player.transform.position.x,player.transform.position.y-1);
//rb2d.AddForce (move * speed);
player.transform.position=move;
Debug.Log(“down”);
}
if(hit.collider.tag==“right”)
{
Vector2 move = new Vector2 (player.transform.position.x+1,player.transform.position.y);
//rb2d.AddForce (move * speed);
player.transform.position=move;
Debug.Log(“”);
}
if(hit.collider.tag==“left”)
{
Vector2 move = new Vector2 (player.transform.position.x-1,player.transform.position.y);
//rb2d.AddForce (move * speed);
player.transform.position=move;
Debug.Log(“left”);
}

If you only want to go from point a to pint b, maybe try to Vector2.Lerp(). It will be more exact, if you want it to go to an exact point, then physics will not be your friend

If you just want to move for step by step, you can use RayCastHit2D, but also Touch, see Mobile Input.

By using this, you need to assign the position of your 4 key buttons (up, down, right, left). And then compare them with Input.GetTouch(0).position. One true, ba ba ba.
Reply me if you still dun have idea how to use this.

Besides, you dun actually need physics in this case as all your movement is uniform.
There is a more convenient way to invoke such control, and indeed this was already been asked before. The answer is using Vector.lerp in Update().

Here’s the link. Moving Object