Home > Enterprise >  How to Make Enemy Attack When In Range of The Player?
How to Make Enemy Attack When In Range of The Player?

Time:11-11

So my game is a 2D top down movement game and my script does make my enemy attack but it constantly loops the attack animation because I obviously don't know what EXACTLY to put code wise to make the enemy attack when in range of the player to do damage instead of letting him constantly loop. Also i seem to be getting an error when i get close to my enemy as of right now it says

NullReferenceException: Object reference not set to an instance of an object EnemyCombat.Attack () (at Assets/EnemyCombat.cs:36) EnemyCombat.Update () (at Assets/EnemyCombat.cs:25)

Also, Here is the EnemyCombat script

    enemy attausing System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyCombat : MonoBehaviour
    {
        public Animator animator;

        public Transform AttackPoint;

        public float attackRange = 0.5f;

        public LayerMask enemyLayers;

        public int attackDamage = 5;

        public float attackRate = 2f;
        float nextAttackTime = 0f;

        // Update is called once per frame
        void Update()
        {
            if (Time.time >= nextAttackTime)
            {    
                Attack();
                nextAttackTime = Time.time   1f / attackRate;    
            }
        }

        void Attack()
        {
            animator.SetTrigger("Attack");
            Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayers);
            foreach (Collider2D enemy in hitEnemies)
            {
                enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
            }
        }

        void OnDrawGizmosSelected()
        {
            if (AttackPoint == null)
                return;

            Gizmos.DrawWireSphere(AttackPoint.position, attackRange);
        }
    }

CodePudding user response:

From that NullReference Error it looks like the major problem you're having is that there is no actual point in the code or in the game hierarchy that you are telling your script which gameObject it is referring to.

Line 36 is trying to retrieve that information with .GetComponent<Enemy()>, so you need to provide that reference.

You could do this in the script fairly easily by creating a public variable that you can drag the enemy gameObject into in your hierarchy in Unity.

Try something like: public GameObject enemyObject;

This will create a variable in the script visible in the hierarchy when you select the script which you can drag the appropriate gameObject into.

There might need to be some adjustments to it because I can't see the rest of your code, but this seems to be the issue.

Another option would be trying to manually adding it in:

void Start()
    {
        GameObject enemyObject = gameObject.GetComponent<GameObject>();
    }

this is taken from Unity Scripting Documentation

CodePudding user response:

To fix your endless attack loop:

// Update is called once per frame
void Update()
{
    if (attackRate >= nextAttackTime) /* checks that nextAttackTime is less than or equal to the attackRate */
    {    
        Attack();
        nextAttackTime = Time.deltaTime * 5f; // adds 5 seconds to nextAttackTime   
    }
    else
    {
        nextAttackTime -= Time.deltaTime; /* if nextAttackTime is greater than the attackRate, subtract one from nextAttackTime. this only happens once per second because you use Time.deltaTime */
    }
}
  • Related