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");
}
}
}
}
}
}