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 /
avatar image
0
Question by redphaze · Apr 06, 2017 at 02:05 AM · raycastcollider3dnamechild object

Just wondering how I can create a child for each generated hex and make that child a sphere collider? Thanks.

GameObject hex_go = new GameObject(); hex_go.name = "Hex_" + x + "_" + y; hex_go.transform.position = new Vector3(xPos, y * yOffset, 0); hex_go.transform.SetParent(this.transform); hex_go.AddComponent(); hex_go.AddComponent < SphereCollider > ();

This script places a collider on the sprite itself but it forces me to name the entire sprite "Hex Collider" because i need the following script to work. Where as I only want to, in theory, name the collider so that the ray can find it.

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hitInfo = Physics.RaycastAll(ray.origin, ray.direction); foreach (RaycastHit h in hitInfo) { if (h.collider.name == "HexCollider") //the name determines what happens when uncle ray ray strikes it. { //do something Debug.Log("Ray hit someone"); } }

Thanks Heaps!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Chiroculon · Apr 06, 2017 at 10:03 PM

@redphaze You could do it as follows:

 GameObject hex_go = new GameObject();
 hex_go.name = "Hex_" + x + "_" + y;
 hex_go.transform.position = new Vector3(xPos, y, * yOffset, 0);
 hex_go.transform.SetParent(this.transform);

 GameObject collider_go = new GameObject();
 collider_go.name = "HexCollider";
 collider_go.transform.SetParent(hex_go.transform);
 collider_go.AddComponent<SphereCollider>();

However, you should consider using a tag or a layer, rather than the object's name, when choosing what to do, because comparing strings is a slower operation.

  1. If you just want to determine whether the hit object is a hex, use a tag.

  2. If you want the trace to ignore some things and not others, use a layer.

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 redphaze · Apr 09, 2017 at 03:08 AM 0
Share

Awesome thanks, worked perfectly :) so what would the benefits be for using tags ins$$anonymous$$d? and how would i do that? thanks :) @Chiroculon

avatar image Chiroculon redphaze · Apr 11, 2017 at 12:25 PM 0
Share

I thought that tags weren't strings, but have since realized my mistake. I also thought one could have multiple tags on an object.

A tag does have the advantage that it is independent from the display, so you could have the name be "HexColl_x_y", or something otherwise useful, while the tag remains "HexCollider".

I do not know enough about your project to deter$$anonymous$$e whether implementing a more robust system to identify objects is worthwhile.

avatar image
1

Answer by Malleck666 · Apr 06, 2017 at 10:31 AM

Why not create a custom component instead, and check if the gameObject has that attached to it. It would probably prove more useful in the long run too.

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 redphaze · Apr 06, 2017 at 02:43 PM 0
Share

$$anonymous$$aybe that would work, I'm a bit new to C# in general but would i be able to name a component separately or is that basically what I'm doing when I add the collider to the sprite?

avatar image Malleck666 redphaze · Apr 07, 2017 at 07:33 AM 0
Share

A custom component would be, for example, your own script. If you had a script called "HexTile" or something, you could check to see if that gameObject has one attached. You can then even use this script to store any values directly related to the Hex Tile.

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

103 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

Related Questions

Raycasts ignoring colliders based on Collider Name 2 Answers

Raycast and colliders problem. Corners of two colliders 0 Answers

How to rotate an object 360 degrees around a sphere with collisions? 0 Answers

Physics.Raycast passing through Colliders 1 Answer

Surface with hole and Raycast - Which collider 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