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 Harryliu · Jun 04, 2016 at 05:22 AM · raycastraycastingparentparent transform

Use Raycast to select empty parent game object?

I am using Raycast to select GameObjects. But when I use hit.transform.gameObject, it returns the first collider it hits. Instead, I want the ray cast to select the empty Parent object that stores all the physical models and the movement script for them. I know hit.transform.gameObject wouldn't work so is there another method?

here's the code. It returns the first collider it hits, which is the top half of the vehicle.

 function SelectRay () {
 
     var r : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var h : RaycastHit;
 
     if(Input.GetButtonDown("Left Mouse")) {
         if(Physics.Raycast(r, h, 100, pLayer)) {
             currentPlayer = h.transform.gameObject;
             playerManager = currentPlayer.GetComponent(PlayerMovement);
             Debug.Log("Player Found");
             if(playerManager.isSelected == true) {
                 playerManager.isSelected = false;
                 Debug.Log("Payer disabled.");
             }    
 
             else {
             playerManager.isSelected = true;
             Debug.Log("Player enabled.");
             }
         }    
     }
 }
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 Alec-Slayden · Jun 04, 2016 at 11:10 AM

First, to answer, this depends on how you need to structure your hierarchy, but if your colliders don't have rigidbodies, you can add a rigidbody to the parent to create a compound collider, which raycast supports.

When a compound collider is hit: h.collider will be the precise collider the raycast hit. h.transform will be the parent object that has the rigidbody component. h.rigidbody will be the parent object's rigidbody component.

If you do have rigidbodies on the child objects, The following are options:

A) Use a 'bubble' collider on the parent or a new child that is set to a special layer, and does not collide with other objects (in physics settings). Use a mask for your raycast so it only hits these bubbles. If they have to be a child, add a helper script that can get give player info.

B) traverse the hierarchy in code using transform.parent to find the object you need.

C) use transform.GetComponentInParent to search parent hierarchy (slow)

D) put a helper script on each collider that returns a reference to the parent script or can call a helper method (if you do choose this, be careful not to let helpers or forwarding get too complex; ideally scripts should encapsulate all their own logic! )

Keep in mind MonoBehaviour includes methods like OnMouseDown(), which gets called when you click on a collider, then you wouldn't need to RayCast or GetComponent, saving you a bit of performance minus the overhead.

I also strongly recommend looking into unity's event system since it's pretty friendly and flexible. By simply adding a physics raycaster component to your camera, and adding unity's event system to your scene (if it isn't there already), you can then add Event Trigger components to the colliders or compound collider and have them call any methods you want on a referenced script. It includes common events like Pointer enter, pointer down, pointer up, pointer click, drag, etc.

[Edit] It occurs to me that you might be attempting to fetch the parent of a multitude of active agents (like a container of RTS units or actors). If this is the case, a compound collider would not be ideal. Compound colliders are more appropriate for a single object that has multiple collision shapes to consider. If you are trying to fetch the parent of a dynamic group of agents, I might advise putting selection code in the click event of the object being selected itself using event triggers or OnMouseDown. If you can avoid using GetComponent every update, definitely do avoid it.

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

58 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

Related Questions

raycast bug when hitting two adjacent objects 0 Answers

Raycast line renderer not drawing correctly 0 Answers

Raycasting is running in two different objects with the same Tag when I touch just one of them 1 Answer

Raycast objects with a tag C# 0 Answers

Raycast on left side and right side of the player 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