Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 ashkatchen · Dec 05, 2021 at 07:00 PM · movementgriddragswipe

Swipe Movement On A Grid

I'm making a game like tetris for mobile devices. It's movement should be on a grid. When i swipe right or left, the object moves 1 unit to right or left. No matter how much i drag it, it just moves 1 unit. But i want it to move more than 1 unit if i drag it longer. How can i achieve that? The game Tetris in google play store uses the method i want but i dont know how they did it. So please help. Here is the script that makes it move once:

     private bool fingerDown;
     public Vector2 startPos;
     public int pixelDistToDetect = 41;
 
     // Update is called once per frame
     void Update()
     {
           if(!fingerDown && Input.GetMouseButtonDown(0))
           {
               startPos = Input.mousePosition;
               fingerDown = true;
           }
           if (fingerDown)
           {
               if(Input.mousePosition.x >= startPos.x + pixelDistToDetect)
               {
                 fingerDown = false;
                 gameObject.transform.position += new Vector3(0.82f, 0, 0);
                 startPos = Input.mousePosition;
                 return;
                 
             }
               if (Input.mousePosition.x <= startPos.x - pixelDistToDetect)
               {
                 
                  fingerDown = false;
                  gameObject.transform.position -= new Vector3(0.82f, 0, 0);
                  startPos = Input.mousePosition;
                  return;
                 
             }
           }
 
           if(fingerDown && Input.GetMouseButtonUp(0))
           {
               fingerDown = false;
           }

i made it with mouse input for testing. It will be touch input in the final.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by metalted · Dec 06, 2021 at 05:50 PM

I think your problem lies with the fact that you have "fingerDown = false" on line 17 and line 26. The logic is fine, you check if the finger is down and if the finger is down you check the distance between the starting point and the current position. If the distance is greater than the set value, you move the object and set a new start point.

BUT! , this is not an indication the player has removed their finger from the screen and therefore, fingerDown should still be true to register the movement, otherwise it will only start tracking again if the player puts a finger back on the screen, because that is the only point in your code where fingerDown is set to true.

Comment
Add comment · Show 5 · 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 ashkatchen · Dec 06, 2021 at 06:00 PM 0
Share

I know that lines are preventing it from going multiple times but when i remove them this happens: 1) I click and finger down becomes true. 2) I drag it beyond the threshold 3)It moves until i release the button (Far far away)

I want it to move as much as i moved my mouse. Same distance. But i cant figure it out.

avatar image metalted ashkatchen · Dec 06, 2021 at 06:59 PM 0
Share

I'm not really sure what the problem is. If i use this exact code without the fingerDown=false part, it works perfectly for me. I hold down the mouse, move to the right 41 pixels, the object moves 0.82 units to the right. If i then keep dragging, mouse button still down, 41 pixels to the right, the object moves again 0.82 units. This works until i release the mouse button. Is this not the effect you are going for?

avatar image ashkatchen metalted · Dec 06, 2021 at 10:31 PM 0
Share

Yea exactly. But in my case, it moves until i let go. Do you use it in update function? Can you give me the code you're using?

Show more comments

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.

Follow this Question

Answers Answers and Comments

188 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 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

Tell an object that a position in grid is occupied 0 Answers

Auto Generated pathing on a grid 0 Answers

Touch and drag in the middle of the screen to rotate camera around the player 1 Answer

Problem with raycast detection? 1 Answer

Row Boat script 0 Answers


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