- Home /
Ray cast not hitting cloned objects
I have an object on which I can cast rays to check if user taps on that object. I am using the following:
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit rayHit;
bool hit = Physics.Raycast(ray, out rayHit, float.PositiveInfinity, 1 << LayerMask.NameToLayer("Kubes"));
if (hit) {
....
}
So far everything works and hit = true
when tapping the object. At some point I clone this object using:
GameObject cloneObj = (GameObject)Instantiate(gameObject, transform.position, transform.rotation);
and for this cloned object the raycast hit does not pass. I verified that the ray passes through the cloned object and it is. Made sure the object layer and tag are the same as the original object.
Any ideas on what am I doing wrong here?
If it´s just an idea, maybe is because of reusing rayHit variable. All the information you get is from the first collision if you are using the same raycastHit for both .
I tested it and everything seems to be working fine for me. I feel it's some mistake you made. Please recheck everything again. $$anonymous$$ake sure you Layered the instance properly.
Answer by giorashc · Jan 11, 2020 at 04:31 PM
After some more digging I found the problem... for some reason the cloned object's Rigidbody component has its detectCollisions
flag set to false
(although the original object has it as true
)