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);
}
}
}
}
}