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 aleem.razzaq09 · Dec 04, 2013 at 11:17 AM · 2draycasttouch

Detect touched 2d object in Unity3d 4.3

Hi, How to detect 2D object in unity 4.3, I have tried code which following mentioned but it gets crash because I think this is for 3D objects. Could you please help me on that how can I detect 2D Object(name or tag) if user tap on object. I have all 2D Objects. Here is my code what I am doing to detect name of objects.

if (iPhoneInput.touchCount == 1 && iPhoneInput.touches[0].phase == iPhoneTouchPhase.Began)

     {
         
         Ray r =  camera.ScreenPointToRay(new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0f));
         
         
         RaycastHit hit0;
             
             switch (hit0.transform.gameObject.name)
                 
             {
                 
             case "char_enemy_alienSlug-dead":
                 
                 hit0.transform.gameObject.SendMessage("BubbleTouched", SendMessageOptions.DontRequireReceiver);
                 
                 break;
                 
             case "char_enemy_alienShip":
                 
                 hit0.transform.gameObject.SendMessage("Touched", SendMessageOptions.DontRequireReceiver);
                 
                 break;
                 
             }
             
         }
 }

Kindly help me on this, I used Ray2D and RaycastHit2D but it also didn't work out. Kindly share with me bit code to detect object name or tag which is touched. Thanks in advance.

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

Answer by robertbu · Dec 04, 2013 at 04:14 PM

Whether you are 3D or 2D, you have to actually cast the ray using some form of raycast either Physics.Raycast() or Physics.Raycast2D(). Just creating a ray doesn't initialize the RaycastHit structure. For this use, you want the 3D version (RaycastHit/Physics.Raycast()). And the raycast is not guarenteed to hit anything, so you need to check. See Raycast examples:

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

Comment
Add comment · Show 9 · 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 aleem.razzaq09 · Dec 05, 2013 at 06:39 AM 0
Share

Thanks robertbu, I tried this code { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, 100)) print("Hit something"); } in Update function and hit 2D object but this code not being call, I have added rigid body and collider to these objects. I don't know where I am doing wrong, this may be because I have to use 2D code like use Ray2D ins$$anonymous$$d of Ray, $$anonymous$$indly help me on that?

avatar image aleem.razzaq09 · Dec 05, 2013 at 06:41 AM 0
Share

Input.mousePosition is Vector3 type, what is alternate function for finger single touch which has Vector3 type because ScreenPointToRay takes Vector2 type. Thanks again.

avatar image aleem.razzaq09 · Dec 10, 2013 at 01:39 PM 0
Share

@robertbu, Did u get my problem for 2D in Unity4.3. I am still stuck over there.

avatar image robertbu · Dec 10, 2013 at 04:39 PM 0
Share

I've not played with the new 2D stuff much, so I just ran some tests. What I found in my limited testing:

  • You cannot add a 2D and 3D collider to the same object

  • Raycast does not detect 2D colliders

  • On$$anonymous$$ouseDown() Does detect 2D colliders

  • A $$anonymous$$esh collider does not appear to work with a sprite

Any time you raycast from the screen you have a 3D collider. You have several choices:

  • You can attach a 3D collider like a box collider to a child object and then use the hit on that child.

  • You can use On$$anonymous$$ouseDown() for processing or to set a flag of some sort.

  • You can use the Physics2D functions to make the detection. You would convert the position into a world point then use the xy in Physics2D.OverlapPoint().

avatar image aleem.razzaq09 · Dec 11, 2013 at 11:43 AM 0
Share

Thanks @robertbu - Raycast does not detect 2D colliders - On$$anonymous$$ouseDown() Does detect 2D colliders Does this mean that I cannot achieve my target(to move 2D object using finger touch), Is there any way other than ray that I can move 2D object using finger swipe.

Show more comments

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

16 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

Related Questions

Shoot Bullet At Touch Position : 2D 1 Answer

Inaccurate Touch Detection iOS 1 Answer

[Problem] Cant activate 2D objects with touch? 1 Answer

Change Input.GetTouch(0).deltaPosition to object transform position in Unity 4.3 for 2D 3 Answers

Raycast cone or cube to detect another object 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