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 samz · May 01, 2012 at 12:49 PM · raycastbuttontouchscreenhit

i done this b4 but forgot :( touch screen

im sure all you know what this question is going to be lol, sorry for repeating. Anyone kind enuf to give a quick reminder as to why the code runs when touching anywhere on the screen? it shud run when i touch the gameobject with the collider=="Button"

 function Update () 
 {
     if( Input.touchCount > 0 )
     {
         var touch = Input.GetTouch(0);  // Cache touches for better performance
         var hit : RaycastHit;
         var ray = Camera.main.ScreenPointToRay( touch.position );
 
         // B E G A N
         if( touch.phase == TouchPhase.Began )
         {
             if( Physics.Raycast( ray, hit, 50 ) )   // If theres a hit
             {
                 if( hit.collider.tag == "Button"  ) // If the hit is with button collider
                 {
                     //be happy
                 }
             }
         }
 }
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
Best Answer

Answer by AtomicMarine · May 01, 2012 at 02:04 PM

I did a little check up on your code, assuming your tagged object is a gameObject (which it should be) then this should work I just broke up the act of finding the tagged object and the act of returning weather it has been hit or not for elegance and hopefully to get the code working. let me know.

  function Update () 
     {
         if( Input.touchCount > 0 )
         {
          
             var touch = Input.GetTouch(0);  // Cache touches for better performance
             var hit : RaycastHit;
             var ray = Camera.main.ScreenPointToRay( touch.position );
     
             // B E G A N
             if( touch.phase == TouchPhase.Began )
             {
                 if( Physics.Raycast( ray, hit, 100 ) )   // If theres a hit
                 {
                     var obj1 : GameObject = GameObject.FindWithTag("Button"); // Break up the task of finding the button and hitting it in two
                     if( hit.transform == obj1.transform  ) // If the hit is with button collider
                     {
                         //be happy
                     }
                 }
             }
     }
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 samz · May 01, 2012 at 05:16 PM -1
Share

BIG FAT VIRTUAL $$anonymous$$ISS! I LOVE YOU!!!!

avatar image Bunny83 · May 01, 2012 at 05:17 PM 0
Share

@samz: stop spam$$anonymous$$g such posts as answers. Use comments!

avatar image samz · May 01, 2012 at 06:57 PM 0
Share

one second. i got 2 objects. when touched they are deleted. problem is you always have to touch object1 and delete before you can touch and delete object2 :S whats the deal with that? i want the script to delete which ever object is touched

 function Update () 
 {
     if( Input.touchCount > 0 )
     {
         var touch = Input.GetTouch(0);  // Cache touches for better performance
         var hit : RaycastHit;
         var ray = Camera.main.ScreenPointToRay( touch.position );
 
         if( Physics.Raycast( ray, hit, 100 ) )   // If theres a hit
         {
             var obj1 : GameObject = GameObject.FindWithTag("Button"); // Break up the task of finding the button and hitting it in two
             if( hit.transform == obj1.transform  ) // If the hit is with button collider
             {
                 // B E G A N
                    if( touch.phase == TouchPhase.Began )
                 {
                     Destroy(obj1.gameObject);
                 }
             }
         }
     }
 }
avatar image
0

Answer by Seth-Bergman · May 01, 2012 at 05:22 PM

Using GameObject.FindWithTag is returning the first guy it finds, you could use FindGameObjectsWithTag, which will return a list of all objects with that tag:

var objects = GameObject.FindGameObjectsWithTag("Button");

then use

 for (var object in objects)
 {
 
 if( hit.transform == object.transform  ){ // If the hit is with button collider
 
         //etc...
     }
     }
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 samz · May 01, 2012 at 06:59 PM 0
Share

0o0o0o0o thank youu

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

I am making android fps and my raycast shooting button don't work. What I should do? 1 Answer

NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer

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

Trouble with Ray & Raycasting and AddForce() 1 Answer

Button Navigation none - not working with touchscreen 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