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 Oniony · May 27, 2014 at 11:19 PM · 2djoysticktouchscreenscreentoworldpointmulti-touch

2D Touch Input Problem with ScreenToWorldPoint

I'm trying to get my virtual joystick to work with my game. I set everything up and got it all working using Input.GetMouseButton and using the mouse position. It worked perfectly. The touch positions were relative to what was on the screen. E.g. If I touched 3 units away from the joystick, the game reacted as such.

Picture Reference: http://puu.sh/943hC.png

(Random grass and the virtual joystick in the corner).

But since this is a multitouch game, I switched over to this code:

 for (var touch : Touch in Input.touches){
             ray = Camera.main.ScreenPointToRay(touch.position);
             hit2d = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Vector2(touch.position.x/2, touch.position.y/2)), Vector2.zero);
             var fingerCount = 0;
             
             if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){
                 leftFinger = false;
             }
             
             if (touch.phase == TouchPhase.Began){
                 if (hit2d.transform.tag == "Joystick"){
                     leftFinger = true;
                 }
             }
         }

However when I check the position of the touch with: Input.GetTouch(0).position, the game is giving me a point off of the screen, and the joystick goes wild. I'm assuming touch has a different unit measurement or something from mouse input? Can someone please help me.

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
0

Answer by meat5000 · May 28, 2014 at 12:04 AM

Don't use Physics2D for GUITextures.

Use Physics.

ScreenToWorldPoint has a Vector3 input. The z represents the distance from the camera that you wish to shoot your ray.

There is already a prefab Joystick in Unity that supports touch. If you cant find it in the Standard Assets Import theres a version in AngryBots tutorial.

On that note, I don't think I've ever used Raycast for GUI touches.

I use something like

 gui.HitTest( touch.position )

where gui is GetComponent(GUITexture) for the Joystick.

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 Oniony · May 28, 2014 at 03:11 AM 0
Share

I'm not actually using gui textures. The joystick is made up of sprites.

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

21 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

Related Questions

SidescrollControl X and Y Axis Movement Problem 1 Answer

Rotating game object using UI Joystick 0 Answers

Joystick problems 1 Answer

Touchscreen Joystick Character Control using PointerEventData 1 Answer

Moving 2D Sprite Player 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