Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MDennis · Jul 07, 2011 at 02:59 PM · movementiphonetouchgrid

grid movement via touch on iphone

Hello all,

here is the script that works really well, but with one glitch,and that glitch i will post about below the script.

 var theplayer: Transform;
 var tester: Vector3;
 var smooth = 10.0;
 
 function Update()
 {
     var count: int = Input.touchCount;
     if(count == 1)
     {
         touch = Input.GetTouch(0);
         if(touch.phase == TouchPhase.Began)
         {
             var ray = Camera.main.ScreenPointToRay(touch.position);
             var hit: RaycastHit;
             if(Physics.Raycast(ray, hit))
             {
                 if(hit.collider.gameObject.tag == "cube")
                 {    
                     tester = hit.collider.gameObject.transform.position;
                 }    
             }    
         }            
     }
     if(tester.x > theplayer.transform.position.x || tester.x < theplayer.transform.position.x)
     {
         MoveLeftOrRight();    
     }
     if(tester.y > theplayer.transform.position.y || tester.y < theplayer.transform.position.y)
     {
         MoveUpOrDown();    
     }
 }
 
 
 function MoveLeftOrRight()
 {
     theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(tester.x,theplayer.transform.position.y,1),(Time.deltaTime *smooth));
 }
 
 function MoveUpOrDown()
 {
     theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(theplayer.transform.position.x,tester.y,1),(Time.deltaTime *smooth));
 }


Now most of you who test this, will think (what is he on about, it works?!), yes but i do not want the diagonal movement. I strictly want it to work on a single axis movement (also known as 4 moveable directions).

I have attempted to say that if the values of tester.x and tester.y have changed to be greater than or less than the players position then do not move, but my code wants to be ignored!

Please if anyone can help me, a pint is on me when your next in wales!

Update!!!

var theplayer: Transform; var tester: Vector3; var smooth = 10.0; var moving: boolean = false;

 function Update()
 {
     var count: int = Input.touchCount;
     if(count == 1)
     {
         touch = Input.GetTouch(0);
         if(touch.phase == TouchPhase.Began)
         {
             var ray = Camera.main.ScreenPointToRay(touch.position);
             var hit: RaycastHit;
             if(Physics.Raycast(ray, hit))
             {
                 if(hit.collider.gameObject.tag == "cube")
                 {    
                     tester = hit.collider.gameObject.transform.position;
 
                 }    
             }    
         }            
     }
     
     if(!moving && (tester.x != theplayer.transform.position.x || tester.y != theplayer.transform.position.y))
     {
         if(Mathf.Abs(tester.x - theplayer.transform.position.x) > Mathf.Abs(tester.y - theplayer.transform.position.y))
         {
             moving = true;
             MoveLeftOrRight();
         }    
         else
         {
             moving = true;
             MoveUpOrDown();    
         }
     }
     
 }
 function MoveLeftOrRight()
 {
     theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(tester.x,theplayer.transform.position.y,1),(Time.deltaTime *smooth));
     yield WaitForSeconds(1);
     moving = false;
 }
 
 function MoveUpOrDown()
 {
     theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(theplayer.transform.position.x,tester.y,1),(Time.deltaTime *smooth));
     yield WaitForSeconds(1);
     moving = false;
 }125

125

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by kieblera5 · Jul 08, 2011 at 01:29 PM

Ahh, I see. Because the initial check is in the update, it will keep calling the function over and over.

 var moving : boolean = false;
 function Update()
 {
 //stuff
 if(!moving && (tester.x!=theplayer.transform.position.x || tester.y!=theplayer.transform.position.y))
 {
      if(Mathf.Abs(tester.x-theplayer.transform.position.x)>Mathf.Abs(tester.y-theplayer.transform.position.y))
      {
            moving=true;
            MoveLeftOrRight();
      }
      else
      {
            moving=true;
            MoveUpOrDown();
      }
 }

Also, throw a yield statement in the move functions after the lerp and reset the moving variable.

 function MoveLeftOrRight()
 {  
      Vector3.Lerp(theplayer.transform.position,Vector3(tester.x,theplayer.transform.position.y,1),(Time.deltaTime *smooth));
     yield WaitForSeconds(1);
     moving=false;
 }
Comment
Add comment · Show 2 · 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 MDennis · Jul 08, 2011 at 02:22 PM 0
Share

its very clunky, meaning i have to repeatedly press the grid location(of where i want the player to go) and its still diagonal, perhaps i repost my code to the state its at? i will do it now to save time if i need to...not sure where the 125 are co$$anonymous$$g from in the code

avatar image Joshua · Jul 08, 2011 at 02:34 PM 1
Share

The 125's are bugs. Not your fault

avatar image
0

Answer by kieblera5 · Jul 07, 2011 at 03:05 PM

Check if the x movement is larger than the y movement. If so, move in the x direction. If not, move in the y direction. Unless the user moves perfectly diagonally where x==y, then the movement will be predictable.

Comment
Add comment · Show 8 · 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 MDennis · Jul 07, 2011 at 03:10 PM 0
Share

this wouldnt really work though would it? what if i need to start moving in the opposite direction, i'd be checking if the values are less than the other value, where as at the same time checking if they are larger...

avatar image kieblera5 · Jul 07, 2011 at 03:18 PM 0
Share

Use the absolute value of the difference of the positions. Save the initial position of the touch on TouchPhase.Began and then compare it to the current position when TouchPhase.$$anonymous$$oved or TouchPhase.Stationary is true. You probably also want to check if the player moved a certain distance first, unless you want it to "follow" your finger, but on fixed axes. To summarize: Save initial position. Compare initial and current positions. If the distance is greater in the x, move x direction. Else if the distance is greater in the y, move y direction.

avatar image MDennis · Jul 07, 2011 at 03:43 PM 0
Share

that just sounds like the way i am doing it now?....

avatar image MDennis · Jul 07, 2011 at 03:48 PM 0
Share

i should explain i just want to be able to select a tile on the grid (like i am) and then the player will move towards it at a slow speed, but only on four moveable directions

avatar image kieblera5 · Jul 07, 2011 at 04:41 PM 0
Share

Ahh, my fault. I thought you were trying to drag the object. Try something like this:

if($$anonymous$$athf.Abs(tester.x-theplayer.transform.position.x)>$$anonymous$$athf.Abs(tester.y-theplayer.transform.position.y)) { $$anonymous$$oveLeftOrRight(); } else { $$anonymous$$oveUpOrDown(); }

If you also want to move up/down after a left/right, then in the bottom of the $$anonymous$$oveLeftOrRight function, add a check to see if the y needs to be changed as well. i.e. function $$anonymous$$oveLeftOrRight() { theplayer.transform.position = Vector3.Lerp(theplayer.transform.position,Vector3(tester.x,theplayer.transform.position.y,1),(Time.deltaTime *smooth)); if(tester.y!=theplayer.transform.position.y) { $$anonymous$$oveUpOrDown(); } } The same would go for the up/down function, just check the x and call the other function.

Show more comments
avatar image
0

Answer by sycr99 · Nov 05, 2020 at 09:29 AM

Hi .. I have an object that moves in the form of a grid, by touching it on the phone. How do I write code to move the unit left, right and forward?

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with iphone/android touch controls 2 Answers

Separate functions for separate touches 1 Answer

How to drag object along with dragging touch? 1 Answer

Massive Touch Lag on mobile devices? 3 Answers

Detecting Touch on Iphone and Android 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