I have a a fairly simple setup that involves a Raycast being fired from the Main Camera (attached to an FPS Controller) every time the Left Mouse Button is clicked. When this Raycast hits a collider with a rigidbody, it applies a force on the rigidbody in the direction that the camera is facing. All of this works perfectly on stationary game objects and rag dolls.
However, if the Raycast is fired at a character that is being animated using a Mecanim animator, it becomes very unreliable.
Here’s a screenshot of what I’m talking about:
The yellow debug line proves that the Raycast is definitely hitting the character’s head collider, but Unity thinks that it is actually hitting the wall behind him. The weird thing is though, if I keep the Raycast in exactly this position but repeatedly fire it off over and over again, Unity will occasionally recognise that the Raycast is indeed hitting the head collider, but most of the time it just seems to ignore it. It’s almost as iff the collider is flickering in and out of existence. And what’s even weirder is, if I turn off the Animator so that the character flops down into a rag doll, the Raycast hits his colliders 100% of the time.
From this I’ve gathered that there must be no problems with the Raycast, and since all of the objects are on the same layer (and definitely not on the Ignore Raycast layer), then there must be an issue with the Mecanim system somewhere. I’ve tried fiddling with the Animator’s Update Mode, but the same issue occurs regardless of whether this is set to Normal, Animate Physics or Unscaled Time.
Any advice on how I might be able to solve this would be muchly appreciated.