Hello. I’m trying to add a slightly cartoony level of force when an enemy dies. I’ve got it sort of working, but with a nullref error and another problem.
I added the commented lines to Ragdoll.cs.
public class Ragdoll : MonoBehaviour // Place on character's/enemy's main gameobject
{
[SerializeField] private Animator animator;
[SerializeField] private CharacterController controller;
//[SerializeField] private WeaponDamage weapondamage;
//[SerializeField] private PlayerAttackingState pattackingstate;
private Collider[] allColliders;
private Rigidbody[] allRigidbodies;
//public Vector3 forceDirection;
//public float currentForce;
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;
}
}
// forceDirection = -weapondamage.hitDirection;
// currentForce = pattackingstate.currentForce;
foreach (Rigidbody rigidbody in allRigidbodies)
{
if (rigidbody.gameObject.CompareTag("Ragdoll"))
{
rigidbody.isKinematic = !isRagdoll;
rigidbody.useGravity = isRagdoll;
// rigidbody.AddForce(Vector3.up * 15, ForceMode.Impulse);
// rigidbody.AddForce(forceDirection * currentForce, ForceMode.Impulse);
}
}
controller.enabled = !isRagdoll;
animator.enabled = !isRagdoll;
}
}
The line for rigidbody.AddForce(Vector3.up * 15, ForceMode.Impulse); works fine for the upward force.
I wanted a reference to the Vector3 direction in WeaponDamage.cs (forceDirection) and the amount of attack force from PlayerAttackingState.cs (currentForce) to create something similar to the ‘impact’ we normally have when attacking the live enemy.
When currentForce = pattackingstate.currentForce; is in the code I notice the enemy rig is really jittery glitchy in-game, so I guess this might be a conflict with the player script being referenced with the enemy or I chose the wrong place to place this line. What’s a better way to get the current attack’s force reference to Ragdoll.cs?
The nullref brings me to forceDirection = -weapondamage.hitDirection;
Does anyone have any suggestions on what to try or have an idea of what missteps I’m taking?
Thanks