- Home /
Raycasting not working as expected
Hey Guys,
In my game I have a first person character controller which can pick up cubes. I do this by casting a ray from the middle of the screen and then the cube gets picked up if it is hit. The raycast, however, only works around half of the time. I have used Debug.drawray so that I can see the ray in the design view and I can see it intersecting the cube. I also print the name of any successful ray casts to see if any hit was actually detected and it seems that when the raycast fails, that the hit is not detected even though, in debug view, I can see the ray intersecting the object.
Any help on this would be greatly appreciated
Thanks guys :)
Answer by Rafes · Jul 05, 2012 at 11:50 PM
If you are using a layer mask, check the layer the target is on. This should fail 100% of the time though
Does the target have a rigidbody to go with its collider. Again, I would expect 100% fail if not
Does it matter if the rigid body of the target is asleep or not? This could be the cause. Keep it awake and see if it still fails to work.
Answer by DarKKendO · Jul 06, 2012 at 07:43 PM
Solved the problem. While trying the steps above (many thanks for those by the way), it seemed that the raycast was only failing when I was looking down at the cube. After putting in some Debug.Logs it seemed that the raycast was colliding with the first person controller as it was being fired from the camera. I set up a LayerCast and it worked perfectly :)
That's what the thumbs up is for (hint hint ;) ).... glad you got it sorted!
Your answer
![](https://koobas.hobune.stream/wayback/20220613073754im_/https://answers.unity.com/themes/thub/images/avi.jpg)