• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nikhil12 · Jun 16, 2017 at 07:00 AM · movementtouchjoystickmovement scripttouch controls

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




         }
     
     
     
     
     }

 }

}

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nikhil12 · Jun 17, 2017 at 06:41 AM 0
Share

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?

avatar image MatrixTai · Jun 19, 2017 at 05:29 PM 0
Share

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.

avatar image Nikhil12 · Jun 22, 2017 at 10:57 AM 0
Share

This code is working as I wanted .

using UnityEngine; using System.Collections;

public class touchcontrol : MonoBehaviour { 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.MoveTowards (player.transform.position,moven,playerspeed*Time.deltaTime);
     //player.transform.position=Vector3.Lerp(player.transform.position,moven,speed);
     if (player.transform.position == moven)
         ismoving = false;
 
 
 }

}

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MatrixTai · Jun 16, 2017 at 04:23 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brogan89 · Jun 16, 2017 at 08:09 AM 0
Share

Transform.Translate() is also another option

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How could I implement diagonal movement in this code? 0 Answers

How to make basic touch screen controls 1 Answer

different devices -> Different touch speed 3 Answers

store the user touch input 0 Answers

Character Movement w/ Both Joysticks 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges