Hey guys.
This script is my personal property and does not belong to anyone else. I would like to publish it in a place where everyone can access it, whether it’s a general commercial post or other categories, as I am looking for help in developing and improving it. Please guide me to the appropriate category for this type of content, as I aim to make it available for sharing and collaboration.
Thank you and best regards.
Sorry, there is an issue with the function of writing the code in the form of a script, if you have noticed.
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
[Header("References")]
public Transform player; // Reference to the player
private NavMeshAgent agent;
private Rigidbody rb;
private Animator animator; // Animator controller
[Header("Attack Settings")]
public int damage = 10; // Damage value
public float attackCooldown = 2f; // Delay between attacks
private float lastAttackTime = 0f;
[Header("Movement Settings")]
public float jumpForce = 5f; // Jump force
private bool isGrounded;
[Header("Colliders")]
public BoxCollider chaseCollider; // Collider for chasing
public SphereCollider attackCollider; // Collider for attacking
[Header("Patrol Settings")]
public float patrolRadius = 10f; // Patrol area radius
public float patrolDelay = 3f; // Delay between patrol movements
private Vector3 patrolTarget;
private float lastPatrolTime = 0f;
void Start()
{
agent = GetComponent<NavMeshAgent>();
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>(); // Get the animator controller
// Set the player reference if it's not assigned
if (player == null)
{
player = GameObject.FindWithTag("Player").transform;
}
}
void Update()
{
// Check if the player is within the chase range
if (chaseCollider.bounds.Contains(player.position))
{
agent.SetDestination(player.position); // Chase the player
animator.SetFloat("Speed", 1f); // Play the running animation
// If the player is within the attack range
if (attackCollider.bounds.Contains(player.position) && Time.time > lastAttackTime + attackCooldown)
{
AttackPlayer();
lastAttackTime = Time.time;
}
}
else
{
Patrol(); // Perform random patrol
}
CheckIfGrounded(); // Check if the enemy is on the ground
}
// Attack the player
void AttackPlayer()
{
Debug.Log("Enemy is attacking the player!");
animator.SetTrigger("Attack"); // Play the attack animation
}
// Check if the enemy is grounded
void CheckIfGrounded()
{
isGrounded = Physics.Raycast(transform.position, Vector3.down, 1f); // Check using Raycast
}
// Patrol randomly
void Patrol()
{
if (Time.time > lastPatrolTime + patrolDelay || Vector3.Distance(transform.position, patrolTarget) < 1f)
{
patrolTarget = GetRandomPoint(); // Select a new patrol point
agent.SetDestination(patrolTarget);
lastPatrolTime = Time.time;
}
animator.SetFloat("Speed", 0.5f); // Play the walking animation
}
// Get a random point within the patrol radius
Vector3 GetRandomPoint()
{
Vector3 randomDirection = Random.insideUnitSphere * patrolRadius;
randomDirection += transform.position;
NavMeshHit hit;
NavMesh.SamplePosition(randomDirection, out hit, patrolRadius, 1);
return hit.position;
}
// Jump over obstacles
void JumpOverObstacle()
{
if (isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
animator.SetTrigger("Jump"); // Play the jump animation
Debug.Log("Enemy jumped over an obstacle");
}
}
// Add animation events
public void DealDamage()
{
if (player != null)
{
player.GetComponent<PlayerHealth>().TakeDamage(damage);
Debug.Log("Player takes damage");
}
}
// Display Gizmos to visualize collider ranges
private void OnDrawGizmos()
{
if (chaseCollider != null)
{
Gizmos.color = Color.blue; // Color for chase collider
Gizmos.DrawWireCube(chaseCollider.bounds.center, chaseCollider.bounds.size);
}
if (attackCollider != null)
{
Gizmos.color = Color.red; // Color for attack collider
Gizmos.DrawWireSphere(attackCollider.bounds.center, attackCollider.radius);
}
}
}