I need help with EnemyAI script

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

You probably want to ask about this in the Non Commercial Collaboration section. To share projects you might want to setup a github account. It’s normally difficult to start a collaboration without doing a lot of the work yourself. You’ll need something like a working prototype, GDD, clear details etc.

I feel like it’s probably a bit early for you to be working on a collaboration. I’d suggest focusing on the project on your own for now and if you get stuck you can ask for help in the scripting section.

2 Likes

Yeah set up a GitHub account and host it there. If you need any help am here.

1 Like

Why would anyone steal your code, especially if it doesn’t properly work?

2 Likes

Sorry, I didn’t realize this wasn’t the scripting section.

1 Like

I downloaded it, but I’m getting lost. I don’t know where to upload it there. All these sites are new to me, and I’m 13.

Can you help me or provide a video to use?

You need to learn posting in right place.
As you have already noticed, it should be scripting forum, or as other posted alternatively collaboration.

Then need to learn proper code formatting on Unity forum. Technically discourse forums.

Github is way to go for you any share / collab work.

Next thing, is actual proper description of the problem. You just dumped the code and expect someone to guess what it does and do it work for you. That is not respectful to the dev community.

If there is source code, always provide it.

And always describe what script or mechanic should do, what you know, what you tried and what doesn’t work.

2 Likes

I fixed the OP’s code formatting and moved the thread to Getting Started category, where it belongs.

1 Like

Go to the GitHub website and create an account then a repo

You should then be able to drag and drop the files there then. This should be the easiest way to get started, you could also (which is recommended) clone the repo on your local machine as well