- Home /
Weird raycasting behaviour with rigidbody objects
Hey,
I've found a weird raycasting behaviour with rigidbody objects. Let me explain, when i "Debug.Log" name of a raycasted child object of a rigidbody object, the named return is the parent's one. But, when i "Debug.Log" name of a raycasted child object of a "none rigidbody object", the named return is the child's one.
The 'simplified' code:
Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2)); RaycastHit hit; if(Physics.Raycast(ray, out hit)) { Debug.Log(hit.transform.name); } }
Again, sorry for the bad English.
Answer by Cynikal · Nov 03, 2016 at 11:45 AM
Rigidbodys act sort of as a big collider.
If you've got a rigidbody on the parent... but aim for a child object. It's hitting the PARENTS rigidbody. Therefore, returning the parents name.
If you're hitting a child objects collider, then it'll return the child objects name.
You can check the parents name..
If the child is one layer underneath the parent, you can do something along the lines of:
if (transform.parent != null){
Debug.Log("My Parent is: "+ transform.parent.name);
} else {
Debug.Log("I am the parent! My name is: "+ transform.name);
}
If the rigidbody act sort as a big collider, to get the child's collider, hit.collider.transform.nam should work... Thanks!
Your answer
Follow this Question
Related Questions
How can I can I cast a ray from a gameobject? 1 Answer
Raytracing through an octree? 0 Answers
Raycast won't hit Collider 1 Answer