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.");
}
}
}
}
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.