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 DaveRune · Mar 13, 2012 at 10:33 AM · mobiletouchoptimizationraytouch screen

Touch screen input - Optimisation

I'm rather new to scripting for mobile and the best solution I have found for detecting if an object is being touched is this:

 if(Input.touchCount > 0) {
    var hit : RaycastHit;
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    if (Physics.Raycast(ray, hit, distance)){
        if (hit.collider.gameObject == something)
    }

}

However, I have 50 objects that can be touched. This means that 50 rays are being cast.

How do I go about optimising this?

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

Answer by syclamoth · Mar 13, 2012 at 10:38 AM

Why do you need to do the raycast 50 times? Even if there's no way of detecting the correct object other than using an if-statement, why can't you just put it all under the first raycast?

 if(Physics.Raycast(ray, hit, distance))
 {
     switch(hit.collider.gameObject)
     {
         case something:
         {
             // Do things
             break;
         }
         case somethingElse:
         {
             // Do other things
             break;
         }
     }
 }

(and so on)

Or, if you have an array of touchable objects, you can do something like this-

 if(touchableObjectCollection.Contains(hit.collider.gameObject))
 {
     // do stuff
 }

(syntax is obviously dependent upon your exact implementation).

Comment
Add comment · Show 2 · 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 DaveRune · Mar 13, 2012 at 10:43 AM 0
Share

So you're suggesting I don't put the touch script on the object itself. That may well answer my question.

What I originally had was that script running on each object that I wanted to be touchable.

So ins$$anonymous$$d have a script to fire the ray and check if it has hit something "touchable"?

avatar image syclamoth · Mar 13, 2012 at 10:51 AM 0
Share

Yes, that's what I'm suggesting. If you have one master script that handles all touches, it can save you a lot of time.

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

Touch Input only working on Unity Remote 0 Answers

How to stop second finger from manipulating position of gui joystick 1 Answer

Detect the mouse swept direction and adding force to object. 0 Answers

Unity ads problem for mobile 0 Answers

Is there a built in method that works like OnMouseDown() but for touch input? 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