c# - Unity 2D 中敌人不会攻击

标签 c# unity-game-engine

我在 Unity 2D 平台游戏中设置了一个简单的敌人,但它应该会在一段时间后向玩家移动,但这种情况并没有发生。 我发布下面的代码;问题是“transform.position = Vector2.MoveTowards(transform.position,playerPosition,JumpAttackSpeed * Time.deltaTime);”不起作用,我不知道为什么。 我原以为敌人会在跳跃后移动并占据玩家当前的位置,但这并没有发生。 这是我的代码希望它有帮助。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyScript : MonoBehaviour
{
    public Rigidbody2D rb;
    public GameObject player;
    private Vector3 playerPosition;
    public float NormalSpeed;
    private float distance;
    private bool canAttack = true;
    public float AttackRange;
    public float JumpAttackRange;
    public float JumpForce;
    public float JumpAttackPreTime;
    public float JumpAttackTime;
    public float JumpAttackSpeed;
    public float AttackCooldawnTime;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        distance = Vector2.Distance(transform.position, player.transform.position);
        if (transform.position != playerPosition && !canAttack)
        {
            transform.position = Vector2.MoveTowards(transform.position, playerPosition, JumpAttackSpeed * Time.deltaTime);
        }
        else
        {
            StartCoroutine(AttackCooldawn());
        }
        if (distance < AttackRange || canAttack)
        {
            StartCoroutine(Attack());
        }
    }
    private IEnumerator Attack()
    {
        if (distance < JumpAttackRange)
        {
            canAttack = false;
            rb.velocity = new Vector2(rb.velocity.x, JumpForce);
            yield return new WaitForSeconds(JumpAttackPreTime);
            rb.velocity = new Vector2(0, 0);
            var normalGravity = rb.gravityScale;
            rb.gravityScale = 0;
            playerPosition = player.transform.position;
            yield return new WaitForSeconds(JumpAttackTime);
            rb.gravityScale = 1f;
        }
    }
    private IEnumerator AttackCooldawn()
    {
        yield return new WaitForSeconds(AttackCooldawnTime);
        canAttack = true;
    }
}

最佳答案

问题在于您使用异步协程的方式。当您的“AttackCooldawn()”协程执行时,因为它是异步的“Update()”方法,它将继续执行,因此当涉及到您的最后一个“if 语句”时,很可能您的“canAttack” bool 值将是 false,因为您的之前的协程会等待几秒钟,然后再将“canAttack”变量设置为 true。您需要一种更好的方法来实现这一点。

尝试使用如下计时器来控制冷却时间:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyScript : MonoBehaviour
{
    public Rigidbody2D rb;
    public GameObject player;
    private Vector3 playerPosition;
    public float NormalSpeed;
    private float distance;
    private bool canAttack = true;
    public float AttackRange;
    public float JumpAttackRange;
    public float JumpForce;
    public float JumpAttackPreTime;
    public float JumpAttackTime;
    public float JumpAttackSpeed;
    public float AttackCooldawnTime;

    public float maximumCooldownTime;
    private float currentCooldownTimer = 0f;

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    distance = Vector2.Distance(transform.position, player.transform.position);
    if (transform.position != playerPosition)
    {
        transform.position = Vector2.MoveTowards(transform.position, playerPosition, JumpAttackSpeed * Time.deltaTime);
    }

    if (distance < AttackRange && currentCooldownTimer > maximumCooldownTime)
    {
        StartCoroutine(Attack());
    }
    
    currentCooldownTimer += Time.deltaTime;

}
private IEnumerator Attack()
{
    if (distance < JumpAttackRange)
    {
        canAttack = false;
        rb.velocity = new Vector2(rb.velocity.x, JumpForce);
        yield return new WaitForSeconds(JumpAttackPreTime);
        rb.velocity = new Vector2(0, 0);
        var normalGravity = rb.gravityScale;
        rb.gravityScale = 0;
        playerPosition = player.transform.position;
        yield return new WaitForSeconds(JumpAttackTime);
        rb.gravityScale = 1f;
    }
    
    currentCooldownTimer = 0f;

}

}

通过 Unity 编辑器简单设置 maximumCooldownTime

关于c# - Unity 2D 中敌人不会攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75620018/

相关文章:

c# - C# 中的高效图像处理

c# - 读一个文件,谁的行改变类型

c# - 如何在实例化相机上应用渲染纹理(作为目标纹理)?

android - 在 Android Studio 中使用 Vuforia SDK

c# - SignalR Core 2.2 CORS AllowAnyOrigin() 重大变更

c# - 为什么 C# 中的 DateTime 不允许为 null?

android - Unity 初学者 - 2d 游戏 : detect position of tap on quad

objective-c - ios 中的 UIButton AddTarget(不工作-崩溃)

c# - 统一四元数,保持 x 和 z 旋转不变

javascript - 如何使用javascript在点击后调用方法