Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 玩家在接近敌人时不会受到伤害_C#_Unity3d - Fatal编程技术网

C# 玩家在接近敌人时不会受到伤害

C# 玩家在接近敌人时不会受到伤害,c#,unity3d,C#,Unity3d,我希望玩家在敌人靠近的时候受到伤害,敌人会受到伤害,但是玩家没有(我试着去掉杀死敌人的功能,检查它是否太快)。我对unity非常陌生,这是我第一个“独立”脚本之一,所以这可能是一个非常明显的错误。但是,没有错误或警告 using System.Collections; using System.Collections.Generic; using UnityEngine; public class AttackableScript : MonoBehaviour { public fl

我希望玩家在敌人靠近的时候受到伤害,敌人会受到伤害,但是玩家没有(我试着去掉杀死敌人的功能,检查它是否太快)。我对unity非常陌生,这是我第一个“独立”脚本之一,所以这可能是一个非常明显的错误。但是,没有错误或警告

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);
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类AttackableScript:MonoBehavior{
公共卫生;
公众浮标攻击速度;
公众浮标损害;
公共浮球无线电=3f;
公共灌肠区;
布尔死=假;
公共游戏对象玩家;
公众浮点数;
公共空间()
{
Debug.Log(运行状况);
健康=健康-0.04f;
如果(运行状况==0f)
{
死=真;
}
}
IEnumerator攻击()
{
GameObject thePlayer=GameObject.Find(“玩家”);
PlayerMovement playerScript=thePlayer.GetComponent();
playerScript.playerHealth=playerScript.playerHealth-伤害;
产生返回新WaitForSeconds(攻击速度);
Debug.Log(“玩家健康=”+playerScript.playerHealth);
}
无效更新(){
浮动距离=矢量3.距离(player.transform.position,enemyArea.position);

如果(distance我问了一个不和谐的问题,他们指出我误用了协同程序,我在调用攻击函数时忘记启动一个。因此,不是attack(),而是start例程(attack())

Unity有一些处理对象冲突的方法:这不是问题检查顶部,当我试图编辑它时出现错误。你可以添加一些调试步骤吗?你看过它以查看状态和顺序吗?你是什么意思?