• 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
Question by Nikhil12 · Jun 16, 2017 at 07:00 AM · movementtouchmovement scriptjoysticktouch 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

People who like this

0 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

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

People who like this

0 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

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

People who like this

0 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

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

People who like this

0 Show 0 · 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges