在靠近敌人时,玩家不会受到伤害

时间:2018-04-15 15:44:08

标签: c# unity3d

我希望玩家在敌人附近时受到伤害,敌人会受到伤害但是玩家没有受到伤害(我试图夺走杀死敌人的功能以检查它是否太快)。我是团结的新手,这是我的第一个“独立”脚本之一,所以这可能是一个非常明显的错误。但是没有错误或警告。

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

public class AttackableScript : MonoBehaviour {
    public float health;
    public float attackSpeed;
    public float damage;
    public float radious = 3f;
    public Transform enemyArea;
    bool dead = false;
    public GameObject player;

    public float KillCount;

    public void Die()
    {
        Debug.Log(health);
        health = health - 0.04f;
        if (health == 0f)
        {
            dead = true;
        }
    }

    IEnumerator Attack()
    {
         GameObject thePlayer = GameObject.Find("player");
         PlayerMovement playerScript = thePlayer.GetComponent<PlayerMovement>();

         playerScript.playerHealth = playerScript.playerHealth - damage;
         yield return new WaitForSeconds(attackSpeed);
         Debug.Log("player health = " + playerScript.playerHealth);
    }

    void Update() {
        float distance = Vector3.Distance(player.transform.position, enemyArea.position);
        if (distance <= radious && dead != true)
        {
            Attack();
        }

        if (health <= 0)
        {
            health = 0;
            Destroy(gameObject);
            Destroy(this);
        }
    }
}

游戏是2d。

1 个答案:

答案 0 :(得分:0)

我问了一个不和谐,他们指出我滥用协同程序,当我调用攻击功能时,我忘了启动它。因此,不是Attack(),而是StartCoroutine(Attack());

相关问题