Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 yyykobe · Jun 25, 2013 at 12:27 PM · objecttouchnamegesture

get/return the touched object name when i touch on the screen

I am doing a map, there are many objects in the map, what i want to realize is when one finger touch on the screen,i want to know which object i touch,there may be a function can return the object name. i am thinking about the "raycast" function, however it doesn't work. can anybody help me?

Comment
Add comment · Show 5
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 amphoterik · Jun 25, 2013 at 12:29 PM 0
Share

Can you show us how you are using the raycast? It should work.

avatar image yyykobe · Jun 25, 2013 at 12:42 PM 0
Share
   Touch touch = Input.touches[0];
   Ray touchRay = cam.ScreenPointToRay(touch.position);
 
   foreach( RaycastHit hit in Physics.RaycastAll(touchRay) ) {
 
   print("touching object name="+hit.transform.name);
                  
                  }

here is partial code. thanks for your help.

avatar image DocteurCox · Jun 25, 2013 at 12:48 PM 0
Share

What kind of problem do you have ? Does it log something in console ?

Anyway, try that code :

         void Update()
         {
             if ( Input.touchCount != 0 )
             {
                 Touch touch = Input.touches[0];
                 Ray ray = Camera.mainCamera.ScreenPointToRay(touch.position);
                 RaycastHit hit;
                 
                 if ( Physics.Raycast(ray, out hit, 100f ) )
                 {
                     Debug.Log(hit.transform.gameObject.name);
                 }
             }
         }
avatar image yyykobe · Jun 25, 2013 at 01:54 PM 0
Share

in fact, i write the code "print(hit.transform.gameObject.name);"and nothing print out.
$$anonymous$$y project is a map using gesture work. i put these code in Drag function, i want to realize when my finger on the screen,there is a function can return the name of object which i am touching on. Do you have any good idea?

avatar image DocteurCox · Jun 25, 2013 at 02:25 PM 0
Share

I didn't know there was a print(). But I guess if you've been using it so far, it must work well. :)

Looking at your code, it's quite possible that your Raycast does not hit anything and thus foreach statement is never entered. Try to specify a distance in your RaycastAll function (though that shouldn't be it, since default is infinity) :

 Physics.RaycastAll ( ray, 100f );

BTW, you should also check that your objects are not on the IgnoreRaycast layer. Also, try to draw your ray like this and see if it hit anything in your scene :

 Debug.DrawLine( ray.origin, ray.origin+ray.direction*100f );

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by tapticc · Mar 08, 2014 at 07:48 PM

For other noobs like me, you need to add a collider e.g. a box collider to the object you want to touch. I feel like such a noob although I am only 3 weeks in to Unity lol

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
3

Answer by amphoterik · Jun 25, 2013 at 12:57 PM

I have modified your code a bit. Try this:

 Touch touch = Input.touches[0];
 Ray touchRay = Camera.mainCamera..ScreenPointToRay(touch.position);

 RaycastHit[] hits = Physics.RaycastAll(touchRay);
 foreach( RaycastHit hit in hits ) {
  
     print("touching object name="+hit.gameObject.name);
  
 }

Minor changes. See if they work.

EDIT: Do you need all of the objects hit or just the first object? RaycastAll may not be doing what you want. You may want to consider (as was posted already) using:

 Physics.Raycast(ray, out hit);

Instead

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
2

Answer by Imankit · Jun 25, 2013 at 12:51 PM

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit = new RaycastHit();
         if(Input.GetMouseButton(0)) {
                if(Physics.Raycast(ray, out hit)) {
                   print(hit.collider.name);
                 }
              }



This is for C#

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 proVeritas_vk · Jul 31, 2018 at 06:43 AM 0
Share

Imankit, this is piece of code for mouse click, not for touch

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

20 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

Related Questions

Rotate Camera around Object 2 Answers

How do I show a custom dynamic string instead of the script name of a MonoBehaviour component in the Inspector? 1 Answer

Add objects on the surface of another object 1 Answer

How to Touch Drag 3D Objects 2 Answers

JS, get child 2 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