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 redphaze · Apr 05, 2017 at 07:41 AM · raycastspriterayturn-basedhexagon

I need help to make a Ray hit a 2D sprite hexagon map.

I also need to be able to select and detect 3D models aswell as the sprites, is it possible for me to do both?

Thanks guys.

 // Use this for initialization
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {

    


    
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);


     RaycastHit hitInfo;
     if (Physics.Raycast(ray, out hitInfo))
     {
         RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);
         if (hit.collider != null)
         {

             Debug.Log("Uncle Ray Ray hit someone ");

         }


     

     }
 }

}

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 Zorbaxy · Apr 05, 2017 at 08:40 AM

Try with Physics.RaycastAll function. Both 2D (Sprites) and 3D objects should have 3D colliders on them. Put a 3D collider on an empty child game object and resize the collider to size appropriate to your sprite. Physics2D.RaycastAll will hit only 2D colliders, and Physics.RaycastAll only 3D colliders.

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit[] hitInfo = Physics.RaycastAll(ray.origin, ray.direction);
 foreach (RaycastHit h in hitInfo)
 {
     if (h.collider.name == "UncleRayTarget")
     {
         //do something
     }
 }

I am not able to test this right now, but it should do the trick.

Comment
Add comment · Show 3 · 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 redphaze · Apr 05, 2017 at 10:42 AM 0
Share

Thanks! it works excellently :) now I'm just wondering if there is a way to generate a collider on each sprite that makes up the map and if it is going to be a problem that each hex has name generated depending on where they are in the world?

avatar image Zorbaxy redphaze · Apr 05, 2017 at 11:29 AM 0
Share

It is possible, depending on what you really need. :) You can make editor script, that will find and add Collider component on all your hex sprites and rename them accordingly. Later on in your RayCast method expect collider.name for sprite you need... Again, depending on what you need...

avatar image redphaze Zorbaxy · Apr 05, 2017 at 01:19 PM 1
Share

Yeah fair enough, I'll have to do some more research, probably by asking more questions, and work it all out, 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

143 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 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

Range not working c# - shooting script using raycast 0 Answers

Moving dotted guide line 0 Answers

How to stop raycast at first object hit? 2 Answers

Do touch on point where ray hits 0 Answers

How to store field of hexagons 1 Answer


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