- Home /
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 tried the movement with lerp . Here is a bit of code
if(hit.collider.tag=="up") { Vector3 move = new Vector3 (player.transform.position.x,player.transform.position.y+4,0);
player.transform.position=Vector3.Lerp(player.transform.position,move,0.3f);
Debug.Log("up");
}
In above code 0.3f is the percentage of distance it should move every frame but in the present situation it is only moving 30percent of total distance not the complete distance . I think the update is checking the condition of touch began every frame that.s why it is not moving total distance.
Any solution for this?
You misunderstand the meaning of lerp, 0.3 means you just walk through 30% from starting point to the end point. You must use 1 for the whole path.
This code is working as I wanted .
using UnityEngine; using System.Collections;
public class touchcontrol : $$anonymous$$onoBehaviour { public GameObject player; public float playerspeed=0.1f; Vector3 moven; bool ismoving=false;
void Start () {
}
// 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")
{
moven = new Vector3 (player.transform.position.x,player.transform.position.y+4,0);
ismoving = true;
}
if(hit.collider.tag=="down")
{
moven = new Vector3 (player.transform.position.x,player.transform.position.y-4,0);
ismoving =true;
}
if(hit.collider.tag=="right")
{
moven = new Vector3 (player.transform.position.x+1,player.transform.position.y,0);
ismoving = true;
}
if(hit.collider.tag=="left")
{
moven = new Vector3 (player.transform.position.x-1,player.transform.position.y,0);
ismoving = true;
}
}
}
}
if (ismoving == true) {
moveplayer ();
}
}
void moveplayer(){
player.transform.position = Vector3.$$anonymous$$oveTowards (player.transform.position,moven,playerspeed*Time.deltaTime);
//player.transform.position=Vector3.Lerp(player.transform.position,moven,speed);
if (player.transform.position == moven)
ismoving = false;
}
}
Answer by Nikhil12 · Jun 16, 2017 at 10:29 AM
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"); }
I think the result will be "The object suddenly disappears and appears at the point you want, when you press the key". If it is fine, then your code's logic is correct. However, if you want it moves progressively, you still need to use lerp.
Answer by Brogan89 · Jun 16, 2017 at 08:07 AM
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
Answer by MatrixTai · Jun 16, 2017 at 10:11 AM
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
Your answer
Follow this Question
Related Questions
How could I implement diagonal movement in this code? 0 Answers
How to make a rigidbody move in the direction of finger(x,y,z) 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do I move an object with my finger?[C#] 1 Answer
Get the position of the first finger that touched the collider? 1 Answer