Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ItsSonic · Nov 09, 2014 at 08:28 PM · android2djavascripttouchsnake

Converting screen coordinates to world coordinates

I have a very basic problem that I cannot find an answer for anywhere. I'm making a snake style game with a few twists for android. The issue is that whenever I touch on screen, my "snake" will only go in two directions it thinks I'm touching way off the screen in the world coordinates.

Here I tried using the only convert method I could find, however this makes it so the the touch is converted to where the camera is which is the center of the screen.

 var touch : Touch;
 var worldTouchPos : Vector3;
  
 for (var i = 0; i < Input.touchCount; ++i) {
  
 touch = Input.GetTouch(i); ~~~~
  
 worldTouchPos = camera.main.ScreenToWorldPoint(touch.position);
  
 if(touch.phase == TouchPhase.Began)
 {
 if(direction == 0 || direction == 2)
 {
 if(transform.position.y < (worldTouchPos.y)) // Go up or down
 {
 direction = 1;
 }
 else
 {
 direction = 3;
  
 }
 }
 else // go right or left
 {
 if(transform.position.x > (worldTouchPos.x))
 {
 direction = 2;
 }
 else
 {
 direction = 0;
 }
 }
  
 }
  
 }//end for
  
  
 if(direction == 0)//right
 {
 transform.Translate(Time.deltaTime, 0, 0);
 }
 else if (direction == 1)//up
 {
 transform.Translate(0, Time.deltaTime, 0);
 }
 else if (direction == 2)//left
 {
 transform.Translate(-(Time.deltaTime), 0, 0);
 }
 else if (direction == 3)//down
 {
 transform.Translate(0,-(Time.deltaTime), 0);
 }



If I just use touch.position.x and touch.position.y where worldTouchPos is, then thats where it will go two directions and out of the screen.

How can I make it so my touch is exactly where it is in the game world coordinates?

Comment
Add comment · Show 5
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 ItsSonic · Nov 09, 2014 at 08:33 PM 0
Share

How would adding a z value move the touch point from where my camera is in the center of the screen to exactly where my touch is?

avatar image MrSoad · Nov 10, 2014 at 01:51 AM 0
Share

Here is the code above reformatted :

 var touch : Touch;
 var worldTouchPos : Vector3;
 
 for (var i = 0; i < Input.touchCount; ++i) {
 
     touch = Input.GetTouch(i); ~~~~
 
     worldTouchPos = camera.main.ScreenToWorldPoint(touch.position);
 
     if (touch.phase == TouchPhase.Began) {

         if (direction == 0 || direction == 2) {
             // Go up or down
             if (transform.position.y < (worldTouchPos.y)) {
                 direction = 1;
             } else {
                 direction = 3;
             }
             
         } else {
             // go right or left
             if (transform.position.x > (worldTouchPos.x)) {
                 direction = 2;
             } else {
                 direction = 0;
             }
         }
     }
 }
 
 
 if (direction == 0) {    //right
     transform.Translate(Time.deltaTime, 0, 0);
 
 } else if (direction == 1) {    //up
     transform.Translate(0, Time.deltaTime, 0);
     
 } else if (direction == 2) {    //left
     transform.Translate(-(Time.deltaTime), 0, 0);
     
 } else if (direction == 3) {    //down
     transform.Translate(0,-(Time.deltaTime), 0);
 }
avatar image robertbu · Nov 10, 2014 at 02:02 AM 0
Share

@tanoshimi had a answer for you 5 hours ago. For a perspective camera, the 'z' parameter of the position passed must be set to the distance in front of the camera. If you pass 0 for 'z', then ScreenToWorldPoint() returns the position of the camera. For example, if your 'action' is occurring at z = 0, and you have your standard 2D setup of the camera at z=-10, then the 'z' parameter would need to be set to 10. If your camera is at an angle with respect to your playing surface, then you need an alternate solution.

avatar image MrSoad · Nov 10, 2014 at 02:06 AM 0
Share

I may have confused the issue by posting his code redone above, I only did it because I saw he had posted again below 20 $$anonymous$$s ago, and looking at the original formatting pained me. Sorry... :S

avatar image ItsSonic · Nov 11, 2014 at 01:56 AM 0
Share

Thanks $$anonymous$$rSoad!!! I am confused still why the value 10 makes sense logically.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ExtremePowers · Nov 09, 2014 at 08:47 PM

Try this: http://docs.unity3d.com/ScriptReference/Camera.ViewportToWorldPoint.html

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 ItsSonic · Nov 10, 2014 at 01:44 AM 0
Share

This has similar results to just using touch.position.x and touch.position.y. $$anonymous$$y player goes off the screen as it thinks the touch is off the screen to the top right. It seems like ScreenToWorld is supposed to work but my touches are registered as where the camera is in the center of the screen so my player will circle the center of the screen no matter where I touch.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to properly implement snake style controls for android? 0 Answers

I Need Help With Mobile Touch Movement. 0 Answers

How long should it take me to make my game 2 Answers

touch position to world position 2D 2 Answers

Best way to do touch in with Unity2D? 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