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
1
Question by Basen · May 04, 2015 at 01:25 PM · unity 5raycasttouch

How to Raycast on Touch?

Greetings,

I currently raycast for each touch in each frame.I only need to raycast once the touch is detected. Is there something like OnTouchDown?

Thanks!

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
Best Answer

Answer by HarshadK · May 04, 2015 at 01:30 PM

Use TouchPhase.Began

Usage example: Input.GetTouch

Comment
Add comment · Show 4 · 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 Basen · May 04, 2015 at 02:35 PM 0
Share

Does TouchPhase.Began work for mul$$anonymous$$ch too? When I try it it just cancels the previous touch. And yes I can support 5 touches.

avatar image HarshadK · May 04, 2015 at 02:41 PM 0
Share

Yes it can support mul$$anonymous$$ch. In the example usage link above just replace 0 to any other touch index in GetTouch()

avatar image Basen · May 04, 2015 at 02:59 PM 0
Share
 RaycastHit2D hit;
     Vector2[] touches = new Vector2[5];
 
     void Update () 
     {
         if(Input.touchCount > 0)
         {
             foreach(Touch t in Input.touches)
             {
                 touches[t.fingerId] = Camera.main.ScreenToWorldPoint(Input.GetTouch(t.fingerId).position);
                 if(Input.GetTouch(t.fingerId).phase == TouchPhase.Began)
                     hit = Physics2D.Raycast(touches[t.fingerId], Vector2.zero);
                 if(hit.collider.name == "Tri(Clone)")
                     hit.transform.position = touches[t.fingerId];
             }
         }
     }

I have this, however only one touch works...

avatar image HarshadK · May 05, 2015 at 05:45 AM 0
Share

Just perform some debug logs to check if it is the touch that is not working or it is the issue with the Raycast.

avatar image
3

Answer by DiebeImDunkeln · May 04, 2015 at 01:34 PM

if you want to check the touch on an object, use this:

  void Update()
  {
         if (platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer)
         {
             if (Input.touchCount > 0 && Input.touchCount < 2)
             {
                 if (Input.GetTouch(0).phase == TouchPhase.Began)
                 {
                     checkTouch(Input.GetTouch(0).position);
                 }
             }
         }
         else if (platform == RuntimePlatform.WindowsEditor || platform == RuntimePlatform.OSXEditor)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 checkTouch(Input.mousePosition);
             }
         }
 
     }
 
     private void checkTouch(Vector3 pos)
     {
          Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
          Vector2 touchPos = new Vector2(wp.x, wp.y);
          Collider2D hit = Physics2D.OverlapPoint(touchPos);
      
         if(hit && hit == gameObject.GetComponent<Collider2D>())
         {
 
            // do something
          }
     }
Comment
Add comment · Show 3 · 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 altair2020 · Mar 08, 2017 at 08:38 AM 0
Share

where do you get platform?

avatar image altair2020 · Mar 08, 2017 at 08:48 AM 0
Share
     if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
     {
         if (Input.touchCount > 0 && Input.touchCount < 2)
         {
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 checkTouch(Input.GetTouch(0).position);
             }
         }
     }
     else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             checkTouch(Input.mousePosition);
         }
     }
avatar image OSContract · Oct 03, 2020 at 08:09 PM 0
Share

Thanks so much for the answer. It helped me a lot. I was search for this past one week. The only change with your script is if (Application.platform == RuntimePlatform.Android) instead of platform == as per 2019 version

avatar image
1

Answer by codemaker2015 · Oct 05, 2020 at 12:08 PM

 void Update () {
          if ((Input.touchCount > 0) && (Input.GetTouch (0).phase == TouchPhase.Began)) {
              Ray raycast = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
              RaycastHit raycastHit;
              if (Physics.Raycast (raycast, out raycastHit)) {
                  Debug.Log ("Something Hit " + raycastHit.collider.name);
                  switch(raycastHit.collider.name) {
                      case "Level1": Debug.Log("Level1 selected"); break;
                      case "Level2": Debug.Log("Level2 selected"); break;
                      case "Level3": Debug.Log("Level3 selected"); break;
                      case "Level4": Debug.Log("Level4 selected"); break;
                      case "Level5": Debug.Log("Level5 selected"); break;
                      default: Debug.Log("Wrong level selection");
                  }
              }
          }
      }
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

23 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

Related Questions

Touch interface not working 1 Answer

Button Focus Prevents Touch Raycast (Android C#) 1 Answer

Unity touch input 1 Answer

How to make object face touch position 1 Answer

Paddle script for Pong-based video game for Android 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