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 unity_FUTS41AFlwuXDQ · Jan 06, 2019 at 01:24 PM · 2d-platformerplatformerjoystickgameplaytouches

How to get a few touches on sprites?

I develop a platformer-game. How can I receive more than one touches on sprites on my device? On my mobile game I can't process more than one touches, for example I need to run and jump at the same time, you know ;D

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MichaI · Jan 06, 2019 at 02:23 PM

From https://docs.unity3d.com/Manual/MobileInput.html you can read example:

        foreach (Touch touch in Input.touches) {
            if (touch.phase == TouchPhase.Began) {
                // Construct a ray from the current touch coordinates
                Ray ray = Camera.main.ScreenPointToRay (touch.position);
                if (Physics.Raycast (ray)) {
                    // Create a particle if hit
                    Instantiate (particle, transform.position, transform.rotation);
                }
            }
        }
If you want to test ray against 2d colliders you can do something like this:
        foreach (Touch touch in Input.touches) {
            if (touch.phase == TouchPhase.Began) {
                Vector3 pos = Camera.main.ScreenToWorldPoint (touch.position);
                RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
                if (hit != null && hit.collider != null) {
                    // Do something with hit
                    Debug.Log ("I've hit "+hit.collider.name);
                }
            }
        }
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
avatar image
0

Answer by unity_FUTS41AFlwuXDQ · Jan 14, 2019 at 04:01 PM

Thank you so much, Michal. I have only one problem: when I press two buttons at once, one of them just hangs up until you press somewhere else. How to fix it?

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 MichaI · Jan 19, 2019 at 07:57 PM 0
Share

I don't know how exactly your buttons works, but I think you can simply remove if statement wich check for touch phase, so it executes for every touch phase (in this case you have to have button released as default whenever it isn't holded):

        foreach (Touch touch in Input.touches) {
              Vector3 pos = Camera.main.ScreenToWorldPoint (touch.position);
              RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
              if (hit != null && hit.collider != null) {
                  // Do something with hit
                  Debug.Log ("Press and hold button "+hit.collider.name);
              }
        }
, or you can add if statement for touch phase "Ended" :
        foreach (Touch touch in Input.touches) {
            if (touch.phase == TouchPhase.Began) {
                Vector3 pos = Camera.main.ScreenToWorldPoint (touch.position);
                RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
                if (hit != null && hit.collider != null) {
                    // Do something with hit
                    Debug.Log ("Press button "+hit.collider.name);
                }
            }
            if (touch.phase == TouchPhase.Ended) {
                Vector3 pos = Camera.main.ScreenToWorldPoint (touch.position);
                RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
                if (hit != null && hit.collider != null) {
                    // Do something with hit
                    Debug.Log ("Release button "+hit.collider.name);
                }
            }
        }
Also if you using this for buttons, you could use ins$$anonymous$$d built in UI buttons with your sprites on it.

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

109 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

Related Questions

Getting my character to slide 2D platformer 0 Answers

2D Combo Attack 1 Answer

Unity 2D Platformer Collision Issue 1 Answer

How to create a blood splatter effect 1 Answer

2d Platformer sprites background 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