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 jdelange · Mar 12, 2016 at 02:34 PM · 2d gameraycasthit2d

RayCastHit2D fails to hit script generated grid

This issue has been asked and answered several times before, but for some unknown reason, it doesn't work for me (beginner). I am generating a 2D grid with a C# script to create a variable size game board. Starting point is this tutorial procedural grid. To interface with the grid with the mouse, I want to cast a ray at mouse position. There is a 3D example here Tile maps #5, however this does not work in 2D mode (and the syntax is deprecated). First challenge (and perhaps error?) is that I have to add a meshcollider to the procedural grid script (ref. 1), the only way I get that to work is with gameObject.AddComponent<MeshCollider>().sharedMesh = mesh; however this is not how the meshfilter and meshrender are added, so this may not be the right way to do it. It also differs from ref. 2, but other ways give an error that a script is trying to access a collider that is not defined. I have tried many variations of the RayCastHit2D, but none of them generates a hit with the collider.

My most recent failed attempt is shown below (mouse script). To limit the size of this post I have added the Grid.cs code as an attachment.Grid.zip. Also attached is a screen dumpScreen.

Question: how can I get the RayCastHit2D to work on this particular implementation? Sorry for the long post, it's my 1st week with Unity...

     void Update () {
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         
         RaycastHit2D hitInfo = Physics2D.Raycast(mousePos, Vector2.zero);
         //Debug.Log(hitInfo.collider);
 
         if (hitInfo.collider != null)
         {
             Debug.Log(hitInfo.point);
             GetComponent<Renderer>().material.color = Color.red;
         }
         else
         {
             //Debug.Log(hitInfo.point);
             GetComponent<Renderer>().material.color = Color.green;
         }
     }


screendump.jpg (101.5 kB)
grid.zip (988 B)
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 hexagonius · Mar 12, 2016 at 04:26 PM

The MeshCollider is a 3D physics component. you either use the PolygonCollider2D component, or raycast in 3D. size note, 2D raycast works from the side only, since there's no z axis. use overlappoint for that kind of check when using 2D stuff

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 jdelange · Mar 13, 2016 at 01:33 PM 0
Share

That's three answers in one. Thanks, I will try all.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why does this code run infinitely? 1 Answer

Stucking with RTS Building detection using 2D Raycasts and 2DBoxColliders 0 Answers

Why aren't raycasts hitting any colliders? 0 Answers

Vertical Rays rapidly changing between "detecting ground" to "not detecting ground" 0 Answers

How do I make a laser that bounces around a room? 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