Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by kshankin · Jan 10, 2017 at 08:56 PM · 2draycastingraycasthit2d

I can't get Raycasting in 2D to work, what am I missing?

I'm trying to detect touches and clicks of a 2D object using Raycast, but the RaycastHit2D object comes back empty. It's not null, but all of its properties are either null or an initialized Vector of (0,0). I've looked online and gone through several answers, all of which seem to have the same basic code. Here's the Update method of the script that is meant to detect touches:

 void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Vector2 coords = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
             CheckForCollisions(coords);
         }
 
         if (Input.touchCount > 0)
         {
             for (int i = 0; i < Input.touchCount; i++)
             {
                 Vector2 touchPos = Input.GetTouch(i).position;
                 Debug.Log(touchPos.x + " : " + touchPos.y);
 
                 CheckForCollisions(touchPos);
             }
         }
     }
     
 
     void CheckForCollisions(Vector2 touchPos)
     {
         Vector3 wp = Camera.main.ScreenToWorldPoint(touchPos);
         Debug.Log("World: " + wp.x + " : " + wp.y);
         Debug.DrawRay(wp, new Vector3(wp.x, wp.y, 20),Color.green,1000.0f);
         RaycastHit2D hit = Physics2D.Raycast(wp, Vector3.forward, Mathf.Infinity);
 
         if (hit.collider != null)
                 {
                 Debug.Log("HIT SOMETHING");
                     Destroy(hit.collider.gameObject);
                 }
     }

And here's the method that actually checks for collisions:

  void CheckForCollisions(Vector2 touchPos)
     {
         Vector3 wp = Camera.main.ScreenToWorldPoint(touchPos);
         Debug.Log("World: " + wp.x + " : " + wp.y);
 
         Debug.DrawRay(wp, new Vector3(wp.x, wp.y, 20),Color.green,1000.0f);
         RaycastHit2D hit = Physics2D.Raycast(wp, Vector3.forward, Mathf.Infinity);
 
         if (hit.collider != null)
                 {
                 Debug.Log("HIT SOMETHING");
                     Destroy(hit.collider.gameObject);
                 }
     }

I've drawn the ray that is cast and it looks like it's hitting the object:

top view of ray that is rendered

angle view of ray that is rendered

I can only upload 2 pictures, but rest assured that the ray appears to be going through the blue circle from every angle in the editor.

Pertinent info:

  1. I have seen solutions using both: Physics2D.Raycast(start, Vector3.forward, Mathf.Infinity); and Physics2D.Raycast(start, Vector3.zero, Mathf.Infinity); I have tried them both (actually, I've gone through all of the Vector3 and Vector2 predefined directions) to no avail.

  2. The script is attached to my main camera, which is positioned at (0, 8.12, -1.5), set to Orthographic projection, has a size of 10 and whose clipping planes are Near:1, Far:5

  3. The circle is positioned at (0,0,0)

  4. The circle is a prefab that I've manually placed on the screen, however, there is a script that generates more circles. None can be touched.

  5. All circles (I believe) are on the default layer and the Default Layer's collision matrices for 2D and 3D have the option 'Ignore Raycast' deselected.

  6. The circles have a Rigidbody 2D component and a CircleCollider2D. The CircleCollider2D has the 'Is Trigger' option ticked on.

  7. I had previously been using OnMouseDown() to detect touches with success on both Web and Android builds, however the method stopped working on Android. I'm not sure why, but it happened around the time that I updated my Unity installation.

  8. Unity editor version is 5.6.0b2.

All help is appreciated. Thanks for reading!

raycast.png (10.4 kB)
raycast2.png (44.8 kB)
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 kshankin · Jan 10, 2017 at 09:39 PM

I figured this out shortly after posting my question. The cause of my issue is that the circle prefabs Collider2D had the 'Is Trigger' option checked but I was not handling the OnTrigger events. There are a few ways to resolve this issue:

  • Uncheck the 'Is Trigger' option on the Collider2D component.

  • Uncheck 'Queries Hit Triggers' Under Edit->Project Settings -> Physics2D

  • Handle the OnTrigger events in a script.

The simplest solution for me was the first one.

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

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

128 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D raycast not working 0 Answers

Select from multiple character to move 0 Answers

Rigidbody blocking raycasts 2D 0 Answers

Need help ironing out this weird error in Raycast2D and Vector2.Reflect, for a billiard game test 1 Answer

Raycast hit problem [ANDROID] 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