Search Unity

Unknown forces being added to ragdoll

Discussion in 'Scripting' started by Clydey2Times, Dec 17, 2019.

  1. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    I'm using ragdoll physics in place of death animations for enemies in my game. However, even when I'm not applying forces, forces are seemingly being applied to the various rigidbodies on the enemy game object.

    I was initally applying a force to all rigidbodies on the various limbs when the enemy's health reaches 0. However, I noticed that even when I removed the aforementioned code, the ragdoll would still fly all over the place whenever I shot the enemy.

    There's nothing special about the shooting. I'm just raycasting. I can't think of any reason why any forces are being applied from a raycast. I've searched the entire solution to see if I've applied forces elsewhere.

    The relevant scripts are below. Can anyone help me? Have I missed something obvious?

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Weapon : MonoBehaviour
    7. {
    8.     [SerializeField] Camera FPCamera;
    9.     [SerializeField] float range = 100f;
    10.     [SerializeField] float effectiveRange;
    11.     [SerializeField] int damagePerBullet = 20;
    12.     [SerializeField] ParticleSystem muzzleFlash;
    13.     [SerializeField] GameObject hitVFX;
    14.     [SerializeField] Ammo ammoSlot;
    15.     [SerializeField] AmmoType ammoType;
    16.     [SerializeField] float timeBetweenShots;
    17.     [SerializeField] bool isAutomatic = false;
    18.  
    19.     private bool canShoot = true;
    20.  
    21.     private void OnEnable()
    22.     {
    23.         canShoot = true;
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         if (isAutomatic)
    29.         {
    30.             if (Input.GetMouseButton(0))
    31.             {
    32.                 if (canShoot)
    33.                 {
    34.                     StartCoroutine(Shoot());
    35.                 }
    36.             }
    37.         }
    38.         else
    39.         {
    40.             if (Input.GetMouseButtonDown(0))
    41.             {
    42.                 if (canShoot)
    43.                 {
    44.                     StartCoroutine(Shoot());
    45.                 }
    46.             }
    47.         }
    48.     }
    49.  
    50.     private IEnumerator Shoot()
    51.     {
    52.         if (ammoSlot.GetCurrentAmmo(ammoType) > 0)
    53.         {
    54.             PlayMuzzleFlash();
    55.             ProcessRayCast();
    56.             ammoSlot.ReduceCurentAmmo(ammoType);
    57.             canShoot = false;
    58.         }
    59.  
    60.         yield return new WaitForSeconds(timeBetweenShots);
    61.         canShoot = true;
    62.     }
    63.  
    64.     private void PlayMuzzleFlash()
    65.     {
    66.         muzzleFlash.Play();
    67.     }
    68.  
    69.     private void ProcessRayCast()
    70.     {
    71.         RaycastHit hit;
    72.         if (Physics.Raycast(FPCamera.transform.position, FPCamera.transform.forward, out hit, range))
    73.         {
    74.             CreateHitVFX(hit);
    75.             if (hit.collider.CompareTag("Enemy"))
    76.             {
    77.                 EnemyHealth enemyHealth = hit.transform.GetComponent<EnemyHealth>();
    78.                 if (Vector3.Distance(hit.transform.position, transform.position) <= effectiveRange)
    79.                 {
    80.                        // enemyHealth.EnemyTakeDamage(damagePerBullet);                  
    81.                 }
    82.                 else
    83.                 {
    84.                        // enemyHealth.EnemyTakeDamage(damagePerBullet / 2);
    85.                 }
    86.             }
    87.         }
    88.     }
    89.  
    90.     private void CreateHitVFX(RaycastHit hit)
    91.     {
    92.         GameObject hitVFXInstance = Instantiate(hitVFX, hit.point, Quaternion.LookRotation(hit.normal));
    93.         Destroy(hitVFXInstance, 0.1f);
    94.        
    95.     }
    96. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyHealth : MonoBehaviour
    6. {
    7.     public delegate void EnemyDeath();
    8.     public EnemyDeath enemyDeath;
    9.     public int currentEnemyHealth = 100;
    10.  
    11.     private Rigidbody[] limbRBs;
    12.     private CapsuleCollider[] limbColliders;
    13.     private Animator anim;
    14.     private EnemyAI enemyAI;
    15.  
    16.     private void Start()
    17.     {
    18.         limbColliders = GetComponentsInChildren<CapsuleCollider>();
    19.         limbRBs = GetComponentsInChildren<Rigidbody>();
    20.         anim = GetComponent<Animator>();
    21.         enemyAI = GetComponent<EnemyAI>();
    22.  
    23.        
    24.         DisableRagDoll();
    25.     }
    26.  
    27.     private void DisableRagDoll()
    28.     {
    29.         foreach (CapsuleCollider c in limbColliders)
    30.         {
    31.             if (c != gameObject.GetComponent<CapsuleCollider>())
    32.             {
    33.                 c.enabled = false;
    34.             }
    35.         }
    36.  
    37.         foreach (Rigidbody rb in limbRBs)
    38.         {
    39.             rb.isKinematic = true;
    40.         }
    41.     }
    42.  
    43.     private void EnableRagDoll()
    44.     {
    45.         foreach (CapsuleCollider c in limbColliders)
    46.         {
    47.             c.enabled = true;
    48.         }
    49.  
    50.         foreach (Rigidbody rb in limbRBs)
    51.         {
    52.             rb.isKinematic = false;
    53.         }
    54.     }
    55.  
    56.     public void EnemyTakeDamage(int damage)
    57.     {
    58.             BroadcastMessage("OnDamageTaken");
    59.             currentEnemyHealth -= damage;
    60.             EnemyDeathSequence();    
    61.     }
    62.  
    63.     private void EnemyDeathSequence()
    64.     {
    65.         if(currentEnemyHealth <= 0)
    66.         {
    67.             enemyDeath += enemyAI.OnDeath;
    68.             enemyDeath();
    69.             anim.enabled = false;
    70.             EnableRagDoll();
    71.  
    72.         }
    73.     }
    74. }
     
  2. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    OK, I've figured out the issue. The particle effect was applying a force. When I disabled the particle effect, the enemy stopped responding to the shots.

    However, I'm not sure what setting within the particle effect is causing the issue. The collision box is unchecked. Any ideas?

    Nevermind. I've sorted it. There was an explosive force script added to the particle effect I'm using.