Search Unity

navmeshagent causes Ragdoll to float upon activation

Discussion in 'Scripting' started by omega_volta, Jun 29, 2021.

  1. omega_volta

    omega_volta

    Joined:
    Apr 28, 2021
    Posts:
    15
    I've been having problems implementing a Navmeshagent to an enemy character in my game that turns into a Ragdoll upon death. When I kill the enemy, its Ragdoll floats in the air. Help would be greatly appreciated.

    The script:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.AI;
    7.  
    8. public class NewZombieScript : MonoBehaviour
    9. {
    10.     public float ZombieSpeed = 8;
    11.     public float ZombieRotationSpeed = 5;
    12.     public float MaxDistzombie = 32.1f; // if larger than this, don't chase, otherwise, chase if lower than this and higher than MinDistzombie
    13.     public float MinDistzombie = 3; // if larger than this and smaller than MaxDistzombie, chase player
    14.     public Transform target;
    15.     private bool chasing;
    16.     private bool Alive;
    17.     public Rigidbody rb;
    18.     public float PlayerDistance;
    19.    
    20.  
    21.     public int MaxZombieHP = 50;
    22.     public int CurrentZombieHP;
    23.     public HealthBar ZombieHealthBar;
    24.     public Animator ZombieAnimator;
    25.  
    26.  
    27.     public NavMeshAgent agent;
    28.  
    29.     private float attackTime; //time since last attack
    30.     public const float attackRate = 1f; // attack cooldown
    31.  
    32.     private void Awake()
    33.     {
    34.         chasing = false;
    35.         Alive = true;
    36.     }
    37.  
    38.     public void Damage(int ZombieDamage)
    39.     {
    40.         CurrentZombieHP -= ZombieDamage;
    41.         ZombieHealthBar.SetHealth(CurrentZombieHP);
    42.     }
    43.  
    44.  
    45.     // Start is called before the first frame update
    46.     void Start()
    47.     {      
    48.         Alive = true;
    49.         CurrentZombieHP = MaxZombieHP;
    50.         ZombieAnimator = GetComponentInChildren<Animator>();
    51.         ZombieAnimator.enabled = true;
    52.         ZombieRigidbody(true);
    53.         rb = GetComponent<Rigidbody>();
    54.         NavMeshAgent agent = GetComponent<NavMeshAgent>();
    55.     }
    56.  
    57.     // Update is called once per frame
    58.     void Update()
    59.     {
    60.  
    61.         PlayerDistance = Vector3.Distance(target.position, transform.position);
    62.        
    63.        
    64.         if (Alive == true)
    65.         {
    66.  
    67.  
    68.             if (PlayerDistance <= MaxDistzombie)
    69.             {
    70.                
    71.        
    72.                 if (PlayerDistance > MinDistzombie)
    73.                 {
    74.                     GoToTarget();
    75.                     ZombieSpeed = 4f;
    76.                     chasing = true;
    77.                     Debug.Log("Chasing");
    78.                 }
    79.             }
    80.             if (PlayerDistance < MinDistzombie)
    81.             {
    82.                 StopZombie();
    83.                 ZombieAttack();
    84.             }
    85.         }
    86.        
    87.    
    88.  
    89.  
    90.         if (attackTime > 0)
    91.         {
    92.             attackTime -= Time.deltaTime;
    93.         }
    94.  
    95.  
    96.  
    97.  
    98.         void GoToTarget()
    99.         {
    100.             if (Alive == true)
    101.             {
    102.                 if (PlayerDistance > MinDistzombie)
    103.                 {
    104.                     ZombieSpeed = 4f;
    105.                     chasing = true;
    106.                     Debug.Log("Chasing");
    107.                 }
    108.             }          
    109.         }
    110.  
    111.         if (CurrentZombieHP <= 0)
    112.         {
    113.             Die();
    114.         }
    115.  
    116.  
    117.  
    118.  
    119.         if (chasing)
    120.         {
    121.             //rotate to look at the player
    122.              
    123.             if (Alive == true)
    124.             {
    125.                 //move towards the player
    126.  
    127.                 FaceTarget();
    128.                 agent.SetDestination(target.position);
    129.                 ZombieAnimator.SetFloat("Speed", agent.velocity.magnitude);
    130.  
    131.                 Debug.Log("MovingToPlayer");
    132.  
    133.                 if (PlayerDistance > MaxDistzombie)
    134.                     chasing = false;
    135.  
    136.                 //give up when out of range
    137.             }
    138.  
    139.             if (Alive == false)
    140.             {
    141.                 chasing = false;
    142.             }
    143.  
    144.  
    145.         }
    146.  
    147.  
    148.  
    149.         else
    150.         {
    151.             //not currently chasing
    152.  
    153.             //start chasing when player is close enough
    154.  
    155.         }
    156.  
    157.  
    158.         void StopZombie()
    159.         {
    160.             chasing = false;
    161.             ZombieSpeed = 1;
    162.             Debug.Log("StoppedMoving");
    163.         }
    164.  
    165.         void FaceTarget ()
    166.         {
    167.             Vector3 direction = (target.position - transform.position).normalized;
    168.             Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    169.             transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    170.         }
    171.        
    172.  
    173.         void ZombieAttack()
    174.         {
    175.             if (attackTime <= 0)
    176.             {
    177.                 attackTime = attackRate;
    178.                 target.GetComponent<PlayerHealth>().TakeDamage(8);
    179.                 Debug.Log("AttackingPlayer");
    180.             }
    181.  
    182.         }
    183.     }
    184.  
    185.     void Die()
    186.     {
    187.         GetComponent<Animator>().enabled = false;
    188.         ZombieRigidbody(false);
    189.         ZombieCollider(true);
    190.         Alive = false;
    191.         ZombieHealthBar.gameObject.SetActive(false);
    192.  
    193.  
    194.     }
    195.  
    196.     private void ZombieCollider(bool state)
    197.     {
    198.         Collider[] colliders = GetComponentsInChildren<Collider>();
    199.  
    200.         foreach (Collider collider in colliders)
    201.         {
    202.             collider.enabled = state;
    203.         }
    204.  
    205.         GetComponent<Collider>().enabled = !state;
    206.     }
    207.  
    208.     private void ZombieRigidbody(bool state)
    209.     {
    210.         Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();
    211.  
    212.         foreach (Rigidbody rigidbody in rigidbodies)
    213.         {
    214.             rigidbody.isKinematic = state;
    215.         }
    216.  
    217.         GetComponent<Rigidbody>().isKinematic = state;
    218.  
    219.     }
    220.  
    221.     void OnDrawGizmosSelected()
    222.     {
    223.         Gizmos.color = Color.red;
    224.         Gizmos.DrawWireSphere(transform.position, MaxDistzombie);
    225.  
    226.         Gizmos.color = Color.blue;
    227.         Gizmos.DrawWireSphere(transform.position, MinDistzombie);
    228.     }
    229.  
    230. }
    231.  
    232.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Disable the navmesh agent on death as well?
     
  3. omega_volta

    omega_volta

    Joined:
    Apr 28, 2021
    Posts:
    15
    i tried that. Gravity worked, but the zombie object started to float away from the zombie's model (the zombie's model is a child of the gameobject that has the script, navmeshagent and rigidbody in it)