C# 产生的敌人只会移动,但不会统一射击

C# 产生的敌人只会移动,但不会统一射击,c#,unity3d,C#,Unity3d,我不熟悉团结。最近,我开始制作一个生存fps游戏,在那里,我有一个敌人,当我杀死他时,它会产生更多的敌人。但是,繁殖的只移动,而不是射击。尽管如此,我还是附加了一个脚本。这是我所有的代码 预制射击- using System.Collections; using System.Collections.Generic; using UnityEngine; public class prefab_shooting : MonoBehaviour {

我不熟悉团结。最近,我开始制作一个生存fps游戏,在那里,我有一个敌人,当我杀死他时,它会产生更多的敌人。但是,繁殖的只移动,而不是射击。尽管如此,我还是附加了一个脚本。这是我所有的代码

预制射击-

  using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class prefab_shooting : MonoBehaviour
    {
        public float damage = 1f;
        public float range = 100f;
    
        public GameObject player;
        public GameObject enemy;
    
        private void Start()
        {
            player = GameObject.FindWithTag("Player");
            enemy = GameObject.FindWithTag("PBR");
        }
    
        private void Update()
        {
            if (Vector3.Distance(transform.position, player.transform.position) < 25.0f)
            {
                Debug.Log(damage);
            }
        }
    
        void Shoot()
        {
            RaycastHit hit;
    
            if (Physics.Raycast(enemy.transform.position, enemy.transform.forward, out hit, range))
            {
                if (hit.transform.tag == "Player")
                {
                    swat_death swat = hit.transform.GetComponent<swat_death>();
                   // Debug.Log(damage);
                }
            }
        }
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PBR_shooting : MonoBehaviour
{
    public float damage = 1f;
    public float range = 100f;

    public GameObject player;
    public GameObject enemy;

    private void Start()
    {
        player = GameObject.FindWithTag("Player");
        enemy = GameObject.FindWithTag("PBR");
    }

    private void Update()
    {
        if (Vector3.Distance(transform.position, player.transform.position) < 25.0f)
        {
            Shoot();
        }
    }

    void Shoot()
    {
        RaycastHit hit;

        if(Physics.Raycast(enemy.transform.position, enemy.transform.forward, out hit, range))
        {
            if(hit.transform.tag == "Player")
            {
                swat_death swat = hit.transform.GetComponent<swat_death>();
              //  Debug.Log(swat.health);
                swat.TakeDamage(damage);
            }
        }
    }
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级预制房屋射击:单行为
{
公共浮子损坏=1f;
公共浮动范围=100f;
公共游戏对象玩家;
公众游戏对象敌人;
私有void Start()
{
player=GameObject.FindWithTag(“player”);
敌人=游戏对象。FindWithTag(“PBR”);
}
私有void更新()
{
if(矢量3.距离(变换.位置,播放器.变换.位置)<25.0f)
{
调试日志(损坏);
}
}
空射()
{
雷卡斯特击中;
if(物理射线投射(敌方.变换.位置,敌方.变换.向前,命中率,射程))
{
if(hit.transform.tag==“播放器”)
{
swat_death swat=hit.transform.GetComponent();
//调试日志(损坏);
}
}
}
}
PBR_射击(敌人射击)-

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类PBR_射击:单一行为
{
公共浮子损坏=1f;
公共浮动范围=100f;
公共游戏对象玩家;
公众游戏对象敌人;
私有void Start()
{
player=GameObject.FindWithTag(“player”);
敌人=游戏对象。FindWithTag(“PBR”);
}
私有void更新()
{
if(矢量3.距离(变换.位置,播放器.变换.位置)<25.0f)
{
射击();
}
}
空射()
{
雷卡斯特击中;
if(物理射线投射(敌方.变换.位置,敌方.变换.向前,命中率,射程))
{
if(hit.transform.tag==“播放器”)
{
swat_death swat=hit.transform.GetComponent();
//Debug.Log(swat.health);
斯瓦特。塔克损坏(损坏);
}
}
}
特警死亡(玩家死亡)-

使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类斯瓦特死亡:单一行为
{
静态浮动健康=250f;
公共游戏对象玩家;
公共损失(浮动金额)
{
健康-=数量;
Debug.Log(运行状况);
如果(健康)您没有调用该函数

在你的第一个脚本(预制射击-中,你没有调用
Shoot()
函数。

不确定你的脚本到底是如何组合在一起的,但是在
预制射击中,你从来没有调用
Shoot
。你只记录了一些东西。你也没有调用
swat。遭受伤害(damage)
…其余的代码看起来都一样,那么为什么会有一个基本相同的组件呢?哦,天哪!!!我真的很抱歉,我忘了调用这个函数。这是一个愚蠢的问题…非常感谢@derHugo:):DAlso概括地说:你真的想在射程内拍摄每一帧吗?我想你应该增加一些冷却时间;)好吧,你能简单地解释一下吗…好吧,我能问另一个问题吗???,虽然它正在繁殖和拍摄,但它不会停止。我如何限制它?好的,你做了…非常感谢你的帮助这是一个愚蠢的错误……这不是我已经说过的吗?……;)好吧,thnx对双方来说,从现在起我会更加注意,尽量不再犯这种愚蠢的错误……谢谢:):德威尔,我可以问另一个问题吗???,虽然它正在繁殖和拍摄,但它不会停止……我怎么能限制它呢?如果你知道的话,我可以解释整个过程让它成为一个新问题:>(关于格式和声誉)
using UnityEngine;
using UnityEngine.SceneManagement;

public class swat_death : MonoBehaviour
{
    static float health = 250f;
    public GameObject player;
 
    public void TakeDamage(float amount)
    {
        health -= amount;

        Debug.Log(health);

        if (health <= 0f)
        {
            Debug.Log("STOP");
            SceneManager.LoadScene("death_scene");
        }
    }

    void Die()
    {

    } 

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.transform.tag == "enemy_bullet")
        {
            //SceneManager.LoadScene("death_scene");
        }
    }


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

public class PBR_death : MonoBehaviour
{
    public GameObject player;
    Animator anim;

    public int XPos;
    public int ZPos;

    public GameObject TheEnemy;
    public int enemyCount = 0;

    public int points = 1;
    public GameObject enemyGameObject;

    void Start()
    {
        anim = GetComponent<Animator>();
        enemyGameObject = GameObject.FindWithTag("PBR");
    }

    // Update is called once per frame
    void OnCollisionEnter(Collision collision)
    {
        if (collision.transform.tag == "bullet")
        {
            anim.SetTrigger("isDying");
            enemy_movement enemy = GetComponent<enemy_movement>();
            enemy.enabled = false;
            PBR_shooting shoot = GetComponent<PBR_shooting>();
            shoot.enabled = false;
            scoreManager.score += points;

            GameObject go = Instantiate(enemyGameObject, new Vector3(Random.Range(34, 0), Random.Range(34, 0), 0), Quaternion.identity) as GameObject;
            go.AddComponent<prefab_movement>();
            go.AddComponent<prefab_death>();
            go.AddComponent<prefab_shooting>();

            // StartCoroutine(EnemySpawner());
            Destroy(collision.gameObject);
        }
    }