Hi, sorry ill paste the code now but from what I can tell, its the code shown in the videos with some added comments for my own use
Enemy AI script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
[SerializeField] Transform Target;//player
[SerializeField] float turnspeed = 5;
[SerializeField] float chaserange = 8f;
[SerializeField]bool isprovoked = false;
NavMeshAgent navMeshAgent;
[SerializeField] float distancetotarget = Mathf.Infinity;//set to infinate to avoid enemy chase at start
// Start is called before the first frame update
void Start()
{
Target = FindObjectOfType<PlayerHealth>().GetComponent<Transform>();//find player objet based on playerhealth - no need to drag in inspector
navMeshAgent = GetComponent<NavMeshAgent>();//get nav mesh for use
}
// Update is called once per frame
void Update()
{
distancetotarget = Vector2.Distance(Target.position, transform.position);
if(isprovoked)//check bool
{
EngageTarget();
}
else if (distancetotarget <= chaserange)//if player is close-set bool
{
isprovoked = true;
}
}
public void onDamageTaken()//call via broadcast
{
isprovoked = true;
}
void EngageTarget()
{
faceTarget();
if (distancetotarget >= navMeshAgent.stoppingDistance)
{
ChaseTarget();//follow target - close distance
}
if(distancetotarget < navMeshAgent.stoppingDistance)
{
attackTarget();
}
}
void ChaseTarget()
{
GetComponent<Animator>().SetBool("Attack", false);//set bool for animator
GetComponent<Animator>().SetTrigger("Move");//set trigger animation
navMeshAgent.SetDestination(Target.position);//follow target
}
void attackTarget()
{
GetComponent<Animator>().SetBool("Attack", true);//set bool for animator
Debug.Log("witty " + name + " " + distancetotarget);//attack target
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 1, 0, 0.75f);//show range of chase
//Gizmos.DrawSphere(transform.position, chaserange);//useful in scene view for seeing object view
Gizmos.DrawWireSphere(transform.position, chaserange);
}
void faceTarget()
{
Vector3 direction = (Target.position - transform.position).normalized; //get vector between enemy and target
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));//set new look to be target
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * turnspeed); //rotate to face target with a speed
}
}
enemy attack script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour
{
PlayerHealth target;
[SerializeField] float damage = 40f;
// Start is called before the first frame update
void Start()
{
target = FindObjectOfType<PlayerHealth>();//find player for this script in scene
}
public void AttackHitEvent()//use event flags in animaton window
{
if(target == null)
{ return; }
target.takeDamage(damage);//call function on player refrnced in inspector
}
}
enemy health script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
[SerializeField] float hitpoints = 100;
public void takeDamage(float damage)
{
//GetComponent<EnemyAI>().onDamageTaken();//possable way - keep here
BroadcastMessage("onDamageTaken");//calls on gameobject or children - calls all named functions
hitpoints -= damage;// = hitpoints - damage;
if(hitpoints <= 0 )
{
Destroy(gameObject);
}
}
}
I’m not sure if there is anything else that would affect this issue, i don’t want to full the feed with code that wont be useful so please let me know if I should show anything else