- Home /
RaycasthitAll specific object and its children
Basically, I want to raycast an specific object and its children without layers and / or tags. Therefore, I used RaycasthitAll to iterate through each item of the object. Code:
RaycastHit[] hits = Physics.RaycastAll(cam.ScreenPointToRay(Input.mousePosition), 100, hitMask);
if (hits.Length != 0) {
for (int i = 0; i < hits.Length; i++) {
foreach (var obj in myObject.GetComponentsInChildren<Transform>()) {
if (hits[i].collider.transform.Equals(obj)) {
// code
}
}
}
}
else {
// revert, or other functionality
}
If the mouse cursor hits the object and its children, it will execute the first part of the code. If the cursor hits nothing, the second part. But the problem here is, if the cursor hits anything (like walls, ground, etc. anything), it will still execute the first part. Therefore I added this:
if (hits[i].collider.transform.Equals(obj)) {
// code
}
else {
// other code
}
However, this only works if the cursor is over the last child of the object.
If anything is not clear enough, I can provide more information
If you use mouse pointet. This might help https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseOver.html
Hi there, unfortunately, I try to avoid On$$anonymous$$ouseEnter/Hover/Exit functions, because the objects have children, and I would have to attach a script to each of the objects and have to remove them afterwards the object has been deselected. So therefore, I think the raycast approach seems more adaptive.
Your transform.equal seem wrong. It check type == type not gameobject == gameobject
Debug that and see if else statements got called on what object