I would like to have an enemy with organs (ie. heart, lungs, liver etc…) How would I go about doing this? I tried to multiple colliders inside the enemy body but they don’t seem to get hit :S
I have a separate script that deals with deducting the enemy health, and the methods get called from this script.
using UnityEngine;
using System.Collections;
public class PlayerShoot : MonoBehaviour
{
EnemyScript enemyOrgans;
void Start()
{
enemyOrgans = GetComponent<EnemyScript>();
}
public void Update()
{
Debug.DrawRay(transform.position, Vector3.forward, Color.green);
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
public void Shoot()
{
RaycastHit _hit;
Physics.Raycast(transform.position, transform.forward, out _hit, 100);
Debug.Log(_hit + " was hit");
if (_hit.collider.tag == "Heart")
{
enemy.Heart();
}
else if (_hit.collider.tag == "Lungs")
{
enemy.Lungs();
}
}
}
Although it might be worth sticking with a raycast to detect if a specific enemies is hit, then check the ray against the organs once that has been established. It might save some physics checking.
You can also raycast against a specific collider using
From a design point of view. Don’t mix the “enemyOrgans” into the player shooting logic. Have the shot call a function on the thing it hits. Pass the ray information in that call, have the organ script handle the specifics about what did/didn’t get hit.
You can store the enemy colliders in your “Enemy” GameObject. Once you shoot and a collider gets hit, you can pass the hit collider itself to the Enemy you have hit and let it deal with the proper consequences.