Search Unity

Question Fixing ragdoll issues

Discussion in 'Scripting' started by DeadEnd77, Jul 14, 2022.

  1. DeadEnd77

    DeadEnd77

    Joined:
    Jul 13, 2022
    Posts:
    15
    I'm making a game where you can play around with a lightsaber, and I want to make it so that when you stab an enemy with the saber, the enemy turns into a ragdoll and flops to the ground. I've already set up the ragdoll and made a bunch of code, but while the code does work as in the enemy doesn't immediately, the enemy won't ragdoll once I hit it with the lightsaber . I really need advice on how to fix this...please.
    (See scripts below)

    Code (csharp):
    1.  
    2. //The script for the lightsaber's collision detection:
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class CollisionDetection : MonoBehaviour
    8. {
    9.     public WeaponController wc;
    10.     public EnemyController ec;
    11.  
    12.  
    13.  
    14.     private void OnTriggerEnter(Collider other)
    15.     {
    16.         if(wc.IsAttacking = true && other.tag == "enemy")
    17.         {
    18.             Debug.Log("you just commited murder");
    19.             ec.GetComponent<EnemyController>().die();
    20.        
    21.         }
    22.     }
    23. }
    24.  
    25. //The enemy's ragdoll script:
    26. using System.Collections;
    27. using System.Collections.Generic;
    28. using UnityEngine;
    29.  
    30. public class EnemyController : MonoBehaviour
    31. {
    32.     // Start is called before the first frame update
    33.     void Start()
    34.     {
    35.         setRigidbodyState(true);
    36.         setColliderState(false);
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.    
    43.     }
    44.  
    45.     public void die()
    46.     {
    47.         GetComponent<Animator>().enabled = false;
    48.         setRigidbodyState(false);
    49.         setColliderState(true);
    50.     }
    51.  
    52.  
    53.     void setRigidbodyState(bool state)
    54.         {
    55.             Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();
    56.  
    57.             foreach(Rigidbody rigidbody in rigidbodies)
    58.             {
    59.                 rigidbody.isKinematic = state;
    60.             }
    61.         GetComponent<Rigidbody>().isKinematic = !state;
    62.         }
    63.  
    64.     void setColliderState(bool state)
    65.         {
    66.             Collider[] colliders = GetComponentsInChildren<Collider>();
    67.  
    68.             foreach (Collider collider in colliders)
    69.             {
    70.                collider.enabled = state;
    71.             }
    72.         GetComponent<Collider>().enabled = !state;
    73.     }
    74. }
    75.  
    76. //Script that controls the lightsaber:
    77. using System.Collections;
    78. using System.Collections.Generic;
    79. using UnityEngine;
    80.  
    81. public class WeaponController : MonoBehaviour
    82. {
    83.     public GameObject Lightsaber_darthvader;
    84.     public bool CanAttack = true;
    85.     public float AttackCooldown = 1.0f;
    86.     public AudioClip SwordAttackSound;
    87.     public bool IsAttacking = false;
    88.  
    89.     void Update()
    90.     {
    91.         if (Input.GetMouseButtonDown(0))
    92.         {
    93.             if(CanAttack)
    94.             {
    95.                 SwordAttack();
    96.             }
    97.         }
    98.     }
    99.  
    100.  
    101.     public void SwordAttack()
    102.     {
    103.         IsAttacking = true;
    104.         CanAttack = false;
    105.         Animator anim = Lightsaber_darthvader.GetComponent<Animator>();
    106.         anim.SetTrigger("Attack");
    107.         AudioSource ac = GetComponent<AudioSource>();
    108.         ac.PlayOneShot(SwordAttackSound);
    109.         StartCoroutine(ResetAttackCooldown());
    110.     }
    111.  
    112.     IEnumerator ResetAttackCooldown()
    113.     {
    114.         StartCoroutine(ResetAttackBool());
    115.         yield return new WaitForSeconds(AttackCooldown);
    116.         CanAttack = true;
    117.     }
    118.  
    119.     IEnumerator ResetAttackBool()
    120.     {
    121.         yield return new WaitForSeconds(1.0f);
    122.         IsAttacking = false;
    123.     }
    124.  
    125. }
    126. [CODE]
     
    Last edited: Jul 14, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,743
    Photographs of code are not a thing.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.

    Steps to success:

    - get the ragdoll transition working 100% - test it with a UI button until you're confident it works

    - sort out the lightsaber detection stuff, hook it up to the ragdoll transition


    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    You must find a way to get the information you need in order to reason about what the problem is.
     
    DevDunk likes this.