- Home /
How do I apply damage to enemy ai in unity c#
Okay . I don't have any errors . The problem I am having is the enemy ai wont take any damage when I shoot . I have a health script and a rifle script. I don't know what is going here are my scripts :
using UnityEngine; using Opsive.ThirdPersonController.Wrappers;
public class attack01 : MonoBehaviour { private health2016 health2016Script; public int damagePerShot = 2; public float timeBetweenBullet = 0.15f; public float range = 100f; public GameObject bear; float timer; Ray shootRay; RaycastHit shootHit; int shootableMask; LineRenderer gunLine; AudioSource gunAudio; Light gunLight; float effectsDisplayTime = 0.2f;
void Start () {
health2016Script = bear.GetComponent<health2016>();
shootableMask = LayerMask.GetMask ("Shootable");
gunLine = GetComponent <LineRenderer>();
gunAudio = GetComponent <AudioSource>();
gunLight = GetComponent <Light>();
}
public void Disable ()
{
gunLine.enabled = false;
gunLine.enabled = false;
}
void Shoot () { timer = 0f;
gunAudio.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
health2016 health2016Script = shootHit.collider.GetComponent <health2016> ();
if (health2016Script != null)
{
health2016Script.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
using UnityEngine; using Opsive.ThirdPersonController.Wrappers;
public class health2016 : MonoBehaviour { public int Health; private CapsuleCollider capsuleCollider; void Start () {
}
public void TakeDamage (int amount , Vector3 hitPoint) {
Health -= amount;
}
void Die()
{
capsuleCollider.isTrigger = true;
}
}