-1

I've been trying to make my enemy fall when it's health reaches zero. So I made a damage variable. The damage will subtract to the health and when it reaches zero or lower, he's supposed to turn into a ragdoll. But its seems as he's not dying in the first place..

In the gun script:

using UnityEngine;

public class gun : MonoBehaviour
{

    public float damage = 10f;
    public float range = 100f;

    public Camera Cam;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Fire();
        }
        
        void Fire()
        {
            RaycastHit hit;
            if(Physics.Raycast(Cam.transform.position, Cam.transform.forward, out hit, range))
            {
                Debug.Log(hit.transform.name);
                
                Enemy enemy = hit.transform.GetComponent<Enemy>();
                if (enemy != null)
                {
                    enemy.damage(damage);
                }
            }
        }
    }
}

Now in the enemy script:

using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    Animator myAnim;

    List<Rigidbody> ragdollRigids;



    public float health = 100f;
    public void damage (float amount)
    {
        health -= amount;
        if(health <= 0)
        {
            Death();
        }
    }

    void Death()
    {
        activateRagdoll();
    }

    //Update

    void Start()
    {
        myAnim = GetComponent<Animator>();
        ragdollRigids = new List<Rigidbody>(transform.GetComponentsInChildren<Rigidbody>());
        ragdollRigids.Remove(GetComponent<Rigidbody>());
        DeactivateRagdoll();
       
    }

    
    void Update()
    {
        
    }


    //Ragdoll dependencies.

    public void activateRagdoll()
    {
        myAnim.enabled = false;
        for (int i = 0; i < ragdollRigids.Count; i++)
        {
            ragdollRigids[i].useGravity = true;
            ragdollRigids[i].isKinematic = false;
        }
    }

    public void DeactivateRagdoll()
    {
        myAnim.enabled = true;
        for (int i = 0; i < ragdollRigids.Count; i++)
        {
            ragdollRigids[i].useGravity = false;
            ragdollRigids[i].isKinematic = true;
        }
    }
}

Thank you in advance. :)

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/220259/discussion-on-question-by-freddie-silver-my-raycast-is-not-activating-my-enemies). – Samuel Liew Aug 22 '20 at 11:28

1 Answers1

0

Solution

Your variables (damage and amount) are named differently but are used for the same purpose. I have edited your code. There should be no errors. If there are, contact me my commenting on my answer.

Code

Gun:

using UnityEngine;

public class gun : MonoBehaviour
{

    public float damage = 10f;
    public float range = 100f;

    public Camera Cam;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Fire();
        }
        
        void Fire()
        {
            RaycastHit hit;
            if(Physics.Raycast(Cam.transform.position, Cam.transform.forward, out hit, range))
            {
                Debug.Log(hit.transform.name);
                
                Enemy enemy = hit.transform.GetComponent<Enemy>();
                if (enemy != null)
                {
                    enemy.damage(damage);
                }
            }
        }
    }
}

Enemy:

using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    Animator myAnim;

    List<Rigidbody> ragdollRigids;



    public float health = 100f;
    public void damage (float damage)
    {
        health -= damage;
        if(health <= 0)
        {
            Death();
        }
    }

    void Death()
    {
        activateRagdoll();
    }

    //Update

    void Start()
    {
        myAnim = GetComponent<Animator>();
        ragdollRigids = new List<Rigidbody>(transform.GetComponentsInChildren<Rigidbody>());
        ragdollRigids.Remove(GetComponent<Rigidbody>());
        DeactivateRagdoll();
       
    }

    
    void Update()
    {
        
    }


    //Ragdoll dependencies.

    public void activateRagdoll()
    {
        myAnim.enabled = false;
        for (int i = 0; i < ragdollRigids.Count; i++)
        {
            ragdollRigids[i].useGravity = true;
            ragdollRigids[i].isKinematic = false;
        }
    }

    public void DeactivateRagdoll()
    {
        myAnim.enabled = true;
        for (int i = 0; i < ragdollRigids.Count; i++)
        {
            ragdollRigids[i].useGravity = false;
            ragdollRigids[i].isKinematic = true;
        }
    }
}