playMaker

Author Topic: Raycast occasionally won't hit colliders on Mecanim animated characters [SOLVED]  (Read 7730 times)

RAWilco

  • Playmaker Newbie
  • *
  • Posts: 20
    • StinBeard
I asked this question over on Unity answers, but I figured I ought to ask it here as well because it does pertain to the Raycast action within Playmaker.

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 if 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.
« Last Edit: June 09, 2015, 03:09:43 PM by RAWilco »

600

  • Beta Group
  • Hero Member
  • *
  • Posts: 713
    • Flashing Lights
Hello,

I had something similar, this happens because Animator overrides the Position and Rotation for bones, so you wont notice the force.
For now I left this unfinished, but I was thinking maybe, if you hit the head, a layer mask for Animator is set to ignore the head, so you can set it to ragdoll(head only) and recieve the force(bullet). Could be done to all parts of a body.
Just a thought, havent tested.


RAWilco

  • Playmaker Newbie
  • *
  • Posts: 20
    • StinBeard
Actually, I haven't tried messing with the layer masks yet. I'll give it a go, thanks.

Also, just in case it helps, this is a rough idea of what my current setup looks like:

- Raycast is fired when Left Mouse Button (or Fire1) is pressed
- Get the GameObject that Raycast hits (usually a body part on the character).
- Get the root of the hit GameObject.
- Check to see if the root has an Animator.
- Destroy the Animator (thereby activating the ragdoll).
- Apply a Force at Raycast Hit Point in the direction that the FPS camera is facing.

This setup works perfectly (and looks really cool)...but only when the Raycast performs correctly.

blackant

  • Hero Member
  • *****
  • Posts: 521
  • http://blackantmaster.com
    • blackantmaster.com
Re: Raycast occasionally won't hit colliders on Mecanim animated characters
« Reply #3 on: August 05, 2014, 09:03:42 AM »
i have the same problem but with a simple collider for the whole character.

when the ray is having a long distance to hit, i have more chance to see the desired effect happening, but with a short ray, it's totally hazardous to have a positive answer ...

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Raycast occasionally won't hit colliders on Mecanim animated characters
« Reply #4 on: November 24, 2014, 01:48:27 AM »
Hi,

 can you reference the UA question? just so we can cross reference from this for when others will face this issue.

 I am really puzzled by this, I mean raycast is rock solid in every thing I done since day one... I would definitly try to isolate this with a totally reproducable case and submit to Unity as a bug.

 Bye,

 Jean

RAWilco

  • Playmaker Newbie
  • *
  • Posts: 20
    • StinBeard
Re: Raycast occasionally won't hit colliders on Mecanim animated characters
« Reply #5 on: November 24, 2014, 03:57:31 AM »
Here it is:

http://answers.unity3d.com/questions/746716/raycast-occasionally-wont-hit-colliders-on-mecanim.html

However, I've done a bit of tinkering in my own project and I do believe that a suggestion someone else made may have provided the solution.

Turn on Kinematics for each rigid body on the Mecanim controlled character.

In my own experiments I've found that the raycasts  are much more reliable with Kinematics switched on (I've not been able to reproduce my original problem since making this alteration). Of course, this does mean you will need an extra FSM or script to turn off the Kinematics at the same time as when you deactivate the Mecanim Animator. It's not exactly the cleanest solution but it does seem to solve the problem.

If anyone else who was having this problem wants to try this in their own projects, let us know if it works for you too and I reckon we could mark this topic solved.

cmonroy

  • Playmaker Newbie
  • *
  • Posts: 34
Re: Raycast occasionally won't hit colliders on Mecanim animated characters
« Reply #6 on: November 24, 2014, 04:39:45 PM »
I had this problem a while back when I was developing a targeting control for my game. It may be due to a parallelax error since the raycast comes from a limb but the sighting is done trough the camera...