My goal is to create a system where a player can shoot different body parts of an enemy and deal different amounts of damage depending on which body part they hit. Enemies are instantiated from a prefab. The enemy’s health is stored as an int value in the parent game object. Here is the enemy code I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;
public class Enemy : MonoBehaviour
{
NavMeshAgent agent;
Transform objectToChase;
public GameObject ZombieRenderObject;
Animator ZombieAnimator;
//reference to the players gun
GunData gundata = new GunData();
// [SerializeField] Transform spawnPoints;
int spawnPoint;
public int zombieHealth = 100;
public bool tracking = true;
//references (player, scoreboard)
private GameObject player;
//functions for zombie health
public int ZombieHealth
{
get
{
return zombieHealth;
}
set
{
zombieHealth = value;
}
}
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player");
ZombieAnimator = ZombieRenderObject.GetComponent<Animator>();
objectToChase = player.transform;
agent = GetComponent<NavMeshAgent>();
//dev tool to make them not chase you while testing
if(tracking == true)
agent.SetDestination(objectToChase.position);
}
// Update is called once per frame
void Update () {
//dev tool to make them not chase you while testing
if (tracking == true)
{ agent.SetDestination(objectToChase.position); }
ZombieAnimator.SetFloat("zombieSpeed", agent.velocity.magnitude / agent.speed);
CheckDeath();
}
//finds the player and sets a destination towards them using the navmesh
void SetDestination()
{
agent.SetDestination(objectToChase.position);
}
//method to damage zombie
void OnCollisionEnter(Collision collision)
{
//Check for a match with the specified name on any GameObject that collides with your GameObject
if (collision.gameObject.name == "Bullet")
{
Debug.Log("I got shot");
//zombieHealth -= 50;
Debug.Log("Damage Dealt = ");
ScoreTotal.scoreTotal += 10;
HighScore.highScore += 10;
}
else
return;
}
void CheckDeath()
{
if(this.zombieHealth <= 0)
{
Destroy(gameObject);
}
}
}
Ideally I could put a collider on each limb with each limb having a unique script. The limbs are child game objects of the Enemy game object. What I don’t know how to is manipulate the zombieHealth variable in the Enemy script from the unique limb scripts. Here is what I have so far for the limb scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeadShot : MonoBehaviour
{
public int zombieHealth;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter(Collision collision)
{
//Check for a match with the specified name on any GameObject that collides with your GameObject
if (collision.gameObject.name == "Bullet")
{
Debug.Log("I got shot in the head");
zombieHealth -= 50;
Debug.Log("Damage Dealt = ");
ScoreTotal.scoreTotal += 10;
HighScore.highScore += 10;
}
else
return;
}
}
I hope this is question makes sense. Any advice would be appreciated.