简体   繁体   English

如何在玩家范围内进行敌人攻击?

[英]How to Make Enemy Attack When In Range of The Player?

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.所以我的游戏是一个 2D 自上而下的移动游戏,我的脚本确实让我的敌人进行攻击,但它不断循环攻击动画,因为我显然不知道在玩家的射程范围内如何正确地编写代码来使敌人进行攻击造成伤害而不是让他不断循环。 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) NullReferenceException:未将对象引用设置为对象 EnemyCombat.Attack ()(在 Assets/EnemyCombat.cs:36)EnemyCombat.Update()(在 Assets/EnemyCombat.cs:25)

Also, Here is the EnemyCombat script另外,这是 EnemyCombat 脚本

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

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.从 NullReference Error 看来,您遇到的主要问题是代码或游戏层次结构中没有实际点告诉脚本它指的是哪个游戏对象。

Line 36 is trying to retrieve that information with .GetComponent<Enemy()> , so you need to provide that reference.第 36 行尝试使用.GetComponent<Enemy()>检索该信息,因此您需要提供该引用。

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.通过创建一个公共变量,您可以在脚本中轻松完成此操作,您可以将敌人的游戏对象拖入 Unity 的层次结构中。

Try something like: public GameObject enemyObject;尝试类似: 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这是取自Unity Scripting Documentation

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 */
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM