Ragdoll won't recieve damage from raycast gun, or allow move code, or deactivate
I have experienced many issues when trying to apply Ragdoll physics to an enemy in my game. When I applied these ragdoll physics to the enemy, it wouldn't follow its script to follow the player, it wouldn't recieve damage from the raycast gun i made, and I also couldn't disable the ragdoll in the enemy's script.
Help or fixes to the script would be greatly appreciated.
the enemy script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewZombieScript : MonoBehaviour
{
public float ZombieSpeed = 4;
public float ZombieRotationSpeed = 3;
public float MaxDistzombie = 22.1f; // if larger than this, don't chase, otherwise, chase if lower than this and higher than MinDistzombie
public float MinDistzombie = 3; // if larger than this and smaller than MaxDistzombie, chase player
public Transform target;
public float PlayerDistance;
private bool chasing;
public bool Alive;
public int MaxZombieHP = 40;
public int CurrentZombieHP;
public HealthBar ZombieHealthBar;
public Animator ZombieAnimator;
List<Rigidbody> rigidBodies;
private float attackTime; //time since last attack
public const float attackRate = 1f; // attack cooldown
private void Awake()
{
chasing = false;
Alive = true;
}
public void Damage(int ZombieDamage)
{
CurrentZombieHP -= ZombieDamage;
ZombieHealthBar.SetHealth(CurrentZombieHP);
}
internal static void Damage()
{
throw new NotImplementedException();
}
// Start is called before the first frame update
void start()
{
rigidBodies = new List<Rigidbody>(transform.GetComponentsInChildren<Rigidbody>());
Alive = true;
CurrentZombieHP = MaxZombieHP;
rigidBodies.Remove(GetComponent<Rigidbody>());
PlayerDistance = Vector3.Distance(transform.position, target.position);
}
// Update is called once per frame
void Update()
{
PlayerDistance = Vector3.Distance(transform.position, target.position);
if (Alive)
{
DeActivateRagDoll();
if (PlayerDistance <= MaxDistzombie)
{
GoToTarget();
}
if (PlayerDistance < MinDistzombie)
{
StopZombie();
ZombieAttack();
}
}
if(Input.GetKeyDown(KeyCode.M))
{
ActivateRagdoll();
}
if (attackTime > 0)
{
attackTime -= Time.deltaTime;
}
void GoToTarget()
{
if (PlayerDistance > MinDistzombie)
{
ZombieSpeed = 4f;
chasing = true;
Debug.Log("Chasing");
}
}
if (CurrentZombieHP <= 0)
{
Die();
}
if (chasing)
{
//rotate to look at the player
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), ZombieRotationSpeed * Time.deltaTime);
//move towards the player
transform.position += transform.forward * ZombieSpeed * Time.deltaTime;
Debug.Log("MovingToPlayer");
if (PlayerDistance > MaxDistzombie)
chasing = false;
//give up when out of range
}
else
{
//not currently chasing
//start chasing when player is close enough
}
void StopZombie()
{
chasing = false;
ZombieSpeed = 1;
Debug.Log("StoppedMoving");
}
void Die()
{
Alive = false;
ActivateRagdoll();
}
void ZombieAttack()
{
if (attackTime <= 0)
{
attackTime = attackRate;
target.GetComponent<PlayerHealth>().TakeDamage(8);
Debug.Log("AttackingPlayer");
}
}
void DeActivateRagDoll()
{
ZombieAnimator.enabled = true;
for (int i = 0; i < rigidBodies.Count; i++)
{
rigidBodies[i].useGravity = false;
rigidBodies[i].isKinematic = true;
}
}
void ActivateRagdoll()
{
ZombieAnimator.enabled = false;
for (int i = 0; i < rigidBodies.Count; i++)
{
rigidBodies[i].useGravity = true;
rigidBodies[i].isKinematic = false;
}
foreach (Rigidbody rb in rigidBodies)
{
rb.isKinematic = false;
}
}
}
}
the gun script:
using UnityEngine;
public class Gun : MonoBehaviour {
public float PistolDamage = 10f;
public float PistolRange = 100f;
public float impactForce = 15f;
public float fireRate = 3f;
public Camera fpsCam;
public ParticleSystem muzzleflash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
// Update is called once per frame
void Update(){
if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot ()
{
muzzleflash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, PistolRange))
//what's below only happens when a target is hit by the pistol's raycast
{
Debug.Log(hit.transform.name);
NewZombieScript target = hit.transform.GetComponent<NewZombieScript>();
if(target != null)
{
NewZombieScript.Damage();
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject ImpactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(ImpactGO, 2f);
}
}
}
Comment