How too do i trigger an AI too attack when im in range? this is the script i have at the moment.
Ive had a look around on youtube, Reddit and unity but im having trouble finding the right answers. So if you know of someone, or some place that can teach me, can you please let me know =).
Anyway heres my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Boss : MonoBehaviour
{
public float attackRange = 15f;
public float lookRadius = 15f;
Transform target;
NavMeshAgent agent;
Animator animator;
// Start is called before the first frame update
void Start()
{
animator = GetComponent();
target = PlayerManager.instance.player.transform;
agent = GetComponent();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
FaceTarget();
Attack();
}
}
}
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 * 5f);
}
void OnDrawGizmosSelected ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
void Attack()
{
float distance = Vector3.Distance(agent.transform.position, target.position);
if (distance < attackRange)
{
animator.SetTrigger(“WeakAttack”);
}
}
}