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 Triqy · Nov 13, 2012 at 08:21 PM · cameragameobjectraycasttouchhit

Touch different Gameobjects using camera array and raycast hit

I want to be able to touch a cube in the scene and have it print "cube" in the debug log and when I touch a sphere in the same scene I want it to print "sphere". This is what i have...

 function Update () {
 
  
 
     for (var touch : Touch in Input.touches){
 
     var ray = Camera.main.ScreenPointToRay(touch.position);
     var hit : RaycastHit;
 
     if (Physics.Raycast (ray, hit, 100)) {
 
         if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
         
         
             print("cube");
         
         
 // this piece of code enables me to touch both cube and sphere but only display 
 //"cube" in the debug log...
 
 // I dont know how to separate the two
 }
 }
 }
 }
 
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
2
Best Answer

Answer by Seth-Bergman · Nov 13, 2012 at 09:55 PM

you're not using a Debug.Log (at least not shown here).. You're using "print"

you have a line that says

print("cube");
(not technically a Debug.Log)

this prints the word "cube" no matter what gets hit, the cube, the sphere, etc..

you could replace that line with this:

      print("Hit: " + hit.collider.name);

for a Debug Log, it would say:

   Debug.Log("Hit: " + hit.collider.name);

(to answer your other question, you can access the tag the same way, from the hit.collider):

 ...etc
 if (Physics.Raycast (ray, hit, 100)) {
    if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
        if(hit.collider.tag == "someTag") {
            ...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 Triqy · Nov 13, 2012 at 10:06 PM 0
Share

Thanks so much man! I really appreciate it. Ive been on this issue for like 5 hrs now. I feel dumb on how simple that was... i was using GameObject.FindWithTag("cube"), but didn't work.. Anyway. Thanks again.

avatar image
0

Answer by PlanetTimmy · Nov 13, 2012 at 10:03 PM

Use the collider member of the RaycastHit variable you pass through to Raycast:

var ray = Camera.main.ScreenPointToRay(touch.position); var hit : RaycastHit;

 if (Physics.Raycast (ray, hit, 100)) {
   if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
     print(hit.collider.name);
   }
 }

In this case it's printing out the name of the object which has the collider component on, but you could check the tag or layer or anything else you like to work out what it is you've hit.

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 test84 · Nov 13, 2012 at 09:18 PM

Why don't you tag each of them and then in the ray cast section, check tag of the object.

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 Triqy · Nov 13, 2012 at 09:40 PM 0
Share

i have the game objects tagged but im not sure where to check for the tags... in what section of my code do i need to check for them?

avatar image Nabil_fx · Feb 07, 2014 at 07:14 PM 0
Share

how can i use this code, to the touch just work on the 3D SPHERE, and not the whole screen. any help...thanks

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

13 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

Related Questions

GameObject Moving Slow when drag with hand 0 Answers

Camera collision detection. 0 Answers

Help destroying gameObject on RayCastHit? 1 Answer

checking colliders from multiple raycast 1 Answer

Orthographic Camera Raycast using ScreenPointToRay 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