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!