Header Ads Widget

Responsive Advertisement

Ai bot

AI Enemy Behavior Script

AI Enemy Behavior Script


using UnityEngine;
using UnityEngine.AI;

public class AIEnemyBehavior : MonoBehaviour
{
    [Header("AI Settings")]
    public float patrolSpeed = 2f;
    public float chaseSpeed = 4f;
    public float attackRange = 2f;
    public float detectionRange = 10f;
    public Transform[] patrolPoints;

    private NavMeshAgent agent;
    private Transform player;
    private int currentPatrolIndex;
    private bool isChasing = false;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        player = GameObject.FindGameObjectWithTag("Player").transform;
        currentPatrolIndex = 0;
        
        if (patrolPoints.Length > 0)
        {
            agent.speed = patrolSpeed;
            agent.destination = patrolPoints[currentPatrolIndex].position;
        }
    }

    void Update()
    {
        if (isChasing)
        {
            ChasePlayer();
        }
        else
        {
            Patrol();
            DetectPlayer();
        }
    }

    private void Patrol()
    {
        if (agent.remainingDistance < 0.5f)
        {
            currentPatrolIndex = (currentPatrolIndex + 1) % patrolPoints.Length;
            agent.destination = patrolPoints[currentPatrolIndex].position;
        }
    }

    private void DetectPlayer()
    {
        float distanceToPlayer = Vector3.Distance(transform.position, player.position);
        if (distanceToPlayer <= detectionRange)
        {
            isChasing = true;
            agent.speed = chaseSpeed;
        }
    }

    private void ChasePlayer()
    {
        agent.destination = player.position;

        float distanceToPlayer = Vector3.Distance(transform.position, player.position);
        if (distanceToPlayer <= attackRange)
        {
            AttackPlayer();
        }
        else if (distanceToPlayer > detectionRange)
        {
            isChasing = false;
            agent.speed = patrolSpeed;
            agent.destination = patrolPoints[currentPatrolIndex].position;
        }
    }

    private void AttackPlayer()
    {
        // Implement attack logic here (e.g., reduce player health)
        Debug.Log("Attacking Player!");
        // Example: player.GetComponent<PlayerHealth>().TakeDamage(damageAmount);
    }
}

Setup Instructions

  1. Attach the Script: Create a new GameObject for your enemy in the Unity scene and attach this script to it.
  2. Add NavMeshAgent Component: Ensure that the enemy GameObject has a NavMeshAgent component attached. Adjust the agent's properties (like speed and stopping distance) as needed.
  3. Set Patrol Points: Create empty GameObjects as patrol points in your scene. Assign them to the patrolPoints array in the inspector.
  4. Player Tag: Make sure your player GameObject is tagged as "Player" for the detection logic to work.
  5. Customize Settings: Adjust the patrolSpeed, chaseSpeed, attackRange, and detectionRange in the inspector to fit your game design.

Notes

  • This script uses Unity's NavMesh system for pathfinding. Ensure you bake a NavMesh for your scene.
  • The attack logic is simplified; you can expand it by implementing damage and health systems as needed.
  • You can further modularize the attack behavior by adding specific attack methods or animations.

Post a Comment

0 Comments