Combat system with AI

Hello everyone!
So I made my character hurt my zombie with my pistol/weapon and when the health is 0 it is destroyed…
Also made my zombie follow me if I am in range…
But…my main problem is with enemy hurting me…I was watching a lot of tutorials and it seem I cant find really one…

The first code is script from AI… and the second one is Damage handler

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealth : BaseHittableObject
{
    public float PlayerGrenadeDamageMul = 2f;

    public override void TakeDamage(DamageData damage)
    {
        if(damage.HitType == DamageData.DamageType.Explosion)
        {
            damage.DamageAmount *= PlayerGrenadeDamageMul;
        }
        base.TakeDamage(damage);
    }

    public override void Die(DamageData damage)
    {
        base.Die(damage);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Chase : MonoBehaviour
{
    public float lookRadius = 10f;

    Transform target;
    NavMeshAgent agent;
 

    void Start()
    {
        target = PlayerManager.instance.player.transform;
        agent = GetComponent<NavMeshAgent>();
    }


     void Update()
    {
        float distance = Vector3.Distance(target.position, transform.position);

        if(distance <= lookRadius)
        {
            agent.SetDestination(target.position);
        
            if (distance <= agent.stoppingDistance)
            {
                FaceTarget();
            }
        }
    }

    void FaceTarget()
    {
        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 1F);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lookRadius);
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.Events;

namespace KnifePlayerController
{
    public class PlayerDamageHandler : MonoBehaviour
    {
        public DamageEvent DamagedEvent
        {
            get
            {
                return damagedEvent;
            }
        }

        public DamageEvent DieEvent
        {
            get
            {
                return dieEvent;
            }
        }

        public UnityEvent ResurrectEvent
        {
            get
            {
                return resurrectEvent;
            }
        }

        public string[] CameraDamageAnimations;
        public int CameraLayer = 3;
        public string PlayerDeathAnimation;
        public Animator HandsAnimator;
        public PlayerHands Hands;
        public PlayerController Controller;
        public TransformTranslater DefaultCameraAnimationTranslater;
        public TransformTranslater DeathCameraAnimationTranslater;
        public TransformStateSetupper CameraStateSetupper;

        public PlayerHealth Health
        {
            get
            {
                return health;
            }
        }

        DamageEvent damagedEvent = new DamageEvent();
        DamageEvent dieEvent = new DamageEvent();
        UnityEvent resurrectEvent = new UnityEvent();

        PlayerHealth health;

        private void Awake()
        {
            health = GetComponent<PlayerHealth>();
            health.DamagedEvent.AddListener(damaged);
            health.DieEvent.AddListener(die);
        }

        private void die(DamageData damage)
        {
            if (!health.IsAlive)
                return;

            if (dieEvent != null)
            {
                dieEvent.Invoke(damage);
            }
         
            HandsAnimator.CrossFadeInFixedTime(PlayerDeathAnimation, 0.05f, 0);

            Controller.Freeze(true);
            Controller.SetNoiseEnabled(false);
            DefaultCameraAnimationTranslater.enabled = false;
            DeathCameraAnimationTranslater.enabled = true;
        }

        public void Resurrect()
        {
            health.Heal(100);
            Controller.Freeze(false);
            Controller.SetNoiseEnabled(true);
            DefaultCameraAnimationTranslater.enabled = true;
            DeathCameraAnimationTranslater.enabled = false;
            HandsAnimator.Play("Default State", 0, 0);
            HandsAnimator.Play("Default State", CameraLayer, 0);
            CameraStateSetupper.LoadSavedState();
            if (resurrectEvent != null)
            {
                resurrectEvent.Invoke();
            }
        }

        private void Update()
        {
            if(Input.GetKeyDown(KeyCode.J))
            {
                Resurrect();
            }
        }

        private void FixedUpdate()
        {
            if (!health.IsAlive)
            {
                Controller.UpdateDefaultDeath();
            }
        }

        private void damaged(DamageData damage)
        {
            if (health.RealIsAlive)
            {
                HandsAnimator.Play(CameraDamageAnimations[Random.Range(0, CameraDamageAnimations.Length)], CameraLayer, 0f);

                if (damagedEvent != null)
                {
                    damagedEvent.Invoke(damage);
                }
            }
        }
    }
}

How are you not able to implement the zombie harming the player if you’ve already implemented the player harming the zombie? Surely it’s just the same thing, right?

buyed only the pistol harming…

So how does the pistol work? You detect if the shot collides with an enemy. If it does, you access the enemy’s damage-handling component and decrement the hitpoints. Something like that, I’m guessing? You would just do the exact same thing for the zombie.

Detect if the zombie is attacking the player and if he is, access the player’s damage handling script and decrement it.

How are you getting a reference to the zombie that the player shoots? Maybe a Raycast or some other physics collision check?

The only difference is that the zombie’s attack is not long range (I assume so). So you’d just need to use a slightly different collision check than the pistol. Something like an OverlapSphere, or even just a much-shorter Raycast might work.

Tnx I will check tomorrow

I managed to do it but now only once I get damaged when my zombie enters the collider and not e.g. 5 damage per second …I guess you know what I mean

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyScript : MonoBehaviour
{

    float damage = 5f;
   
   
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
   


    void OnTriggerEnter(Collider other)
    {
       
        other.gameObject.GetComponent<HealthScript>().TakeDamage(damage);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthScript : MonoBehaviour
{


   
    public float max_health = 100f;
    public float cur_health = 0f;
    public bool alive = true;


    // Start is called before the first frame update
    void Start()
    {

        alive = true;
        cur_health = max_health;
       
       
    }


    void DoDamage()
    {

        TakeDamage(10f);

    }

    public void TakeDamage(float amount)
    {
           
       
        if (!alive)
        {
            return;
        }

        if (cur_health <= 0)
        {
            cur_health = 0;
            alive = false;
            gameObject.SetActive(false);
        }


        cur_health -= amount;


    }
    // Update is called once per frame
 
}

OnTriggerEnter only fires off one time on the exact frame that an object moves from outside the collider to inside the collider. You can use OnTriggerStay instead, which fires-off every frame that the object is inside the collider. Remember that there many frames per second, so in that case the damage will be done very rapidly unless you do something to slow it down. For example, if you replace

other.gameObject.GetComponent<HealthScript>().TakeDamage(damage);

with

other.gameObject.GetComponent<HealthScript>().TakeDamage(damage*Time.deltaTime);

Then it will drain the 5 damage per second.

One other thing- OnTriggerEnter will occur for every rigidbody that enters the trigger, so before you attempt to call other.gameObject.GetComponent, you will want to make sure that “other” is actually the player, instead of some other game object like another zombie or something.

Man thank you so much!