Colliders not registering being hit after turning on ragdoll

I just finished lesson 54 ‘ragdoll on death’ of the Unity 3rd Person Combat and Traversal course. Before implementing the ragdoll my character and enemy were still able to hit each other. But after implementing the ragdoll they no longer seem to respond to being hit. All I did was add the scripts as shown in the course

Ragdoll.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ragdoll : MonoBehaviour
{
    [SerializeField] private Animator animator;
    [SerializeField] private CharacterController controller;
    private Collider[] allColliders;
    private Rigidbody[] allRigidbodies;

    private void Start()
    {
        allColliders = GetComponentsInChildren<Collider>(true);
        allRigidbodies = GetComponentsInChildren<Rigidbody>(true);
        ToggleRagdoll(false);
    }

    public void ToggleRagdoll(bool isRagdoll)
    {
        foreach (Collider collider in allColliders)
        {
            if(collider.gameObject.CompareTag("Ragdoll"))
            {
                collider.enabled = isRagdoll; // turns on the ragdoll if bool is true, off if false
                Debug.Log("Do I get here?");
            }
        }

        foreach (Rigidbody rigidbody in allRigidbodies)
        {
            if (rigidbody.gameObject.CompareTag("Ragdoll"))
            {
                rigidbody.isKinematic = !isRagdoll; // ragdoll is false means physics are applied and affect the rigidbody
                rigidbody.useGravity = isRagdoll;
            }
        }

        controller.enabled = !isRagdoll;
        animator.enabled = !isRagdoll; // turn off animator
    }

}

I added this line to the EnemyStateMachine.cs:
[field: SerializeField] public Ragdoll Ragdoll { get; private set; }

and added this to the PlayerStateMachine.cs
[field:SerializeField] public Ragdoll Ragdoll {get; private set; }

and then I added ’ stateMachine.Ragdoll.ToggleRagdoll(true); 'in the Enter method of both the EnemyDeadState.cs and PlayerDeadState.cs

I right-clicked on my player and enemy, created a 3D object of ragdoll and selected the bones that need to be affected by it. It now has rigidbodies on it, colliders and character joints and I changed the tags and layers to Ragdoll.

On the player object and enemy object, I added the ragdoll script, plugged in the animator and character controller… and then in the state machine dragged in the ragdoll into the serialized ragdoll field.

I don’t get why now it won’t register getting hit. I debug.Log’ed the attackingState, it still goes into the attacking state… it is as if it can no longer find the character controller to collide.

How do I solve this?

Thanks!

You should not turn the whole player or enemy into a ragdoll. You should just select the mixamo rig and turn that into a ragdoll. Your script is disabling all the colliders, which is why no hits are being registered. Make sure that only the model has the ‘ragdoll’ tag


Hi again!

I right-clicked the armature of my character (not a Mixamo character). Did not select the root. The tags are only on some of the bones (legs, arms, torso, pelvis etc) as I had to fill in when I created the ragdoll. The top level of my character only has the Player Tag and Player Layer.

Is this still the same as the one you shared previously (excluding the ragdoll)? Let me check the character’s setup quick

Nearly the same yeah, I just added the dead state. Thank you!

Looks like you may have to zip it again… Sorry. I’m missing a bunch of stuff in the older copy and it’s just quicker if you send me a newer one

Sure thing, here you go

Thank you!

Your weapons don’t have colliders on them

What… how is that even possible, I did not delete them. But seems you’re right. Maybe the ragdoll somehow overwrote it and removed it?

Thank you for finding that. I don’t understand how the colliders got removed. I also forgot what other step I need to take after adding them back… plug them in somewhere I suppose. Not seeing it just yet.

I got it now, thank you!

Uhm, not sure. I just added a capsule collider to the bat and made it a trigger. Then tested and all was ok. But I was only checking for collisions

I put the colliders back on the weaponhandlers now and it works again, pfeww! Thanks! In theory… can I now use these ragdoll colliders to hit different limbs and have a blood particle effect play wherever the enemy or player gets hit?

No, you can’t. You disabled all those colliders so they won’t respond.

You actually can, but you’ll need to make a small structural change to the way you manage the colliders. Rather than turning the colliders off and on when they are ragdolled, set the colliders to isTrigger= true when ragdoll is off, and and isTrigger=false when ragdoll is on.

This does add a bit more complexity, however, as your WeaponDamage will detect these but not find a corresponding health to damage. We can use that to our advantage, though:

        if (other.TryGetComponent<Health>(out Health health))
        {
            health.DealDamage(damage);
        } else if (other.gameObject.CompareTag("Ragdoll"))
        {
            Vector3 closestPoint = other.ClosestPoint(transform.position);
            //SpawnBloodEffect(closestPoint);
        }
1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms