【问题标题】:unity 2D: simple enemy shootingunity 2D:简单的敌人射击
【发布时间】:2019-11-07 00:37:41
【问题描述】:

我正在开发一款 2D 游戏。如果我想要一个简单的敌方大炮每 5 秒左右射击一次,我会怎么做?

我知道我需要添加一个 colider 和刚体,但不太确定如何解决这个问题,因为我仍然掌握整个想法

Red = Enemy/ Rough idea/Sketch

谢谢

【问题讨论】:

  • Here 你可以找到 Unity 教程。您将学习如何实例化一个对象并对其addforce。您还将了解碰撞回调。请不要在将来在您的问题中添加代码的情况下提出更多问题。

标签: c# unity3d


【解决方案1】:

您想要创建一种游戏对象以用作“子弹”。这个游戏对象在生成时有一个脚本,可以让它向某个方向移动。

您可以使用力(物理)移动它们,或者您可以将它们从一个地方转移到另一个地方,即“绝对”移动它们并忽略环境中的物理。

然后,您可以使用 OnCollisionEnter 方法或 OnTriggerEnter 方法对该对象使用碰撞器来检测它何时击中玩家。

这里有一些教程,希望对您有所帮助。 CreatingShooting

【讨论】:

    【解决方案2】:

    首先你需要考虑你的敌人应该如何行动,只是并排走或找到敌人作为导航。

    我在统一网站上找到了这个脚本:

    使用 UnityEngine; 使用 System.Collections;

    公共类 EnemyAttack : MonoBehaviour { 公共浮动时间BetweenAttacks = 0.5f; // 每次攻击之间的时间(以秒为单位)。 公共 int 攻击伤害 = 10; // 每次攻击带走的生命值。

    Animator anim;                              // Reference to the animator component.
    GameObject player;                          // Reference to the player GameObject.
    PlayerHealth playerHealth;                  // Reference to the player's health.
    EnemyHealth enemyHealth;                    // Reference to this enemy's health.
    bool playerInRange;                         // Whether player is within the trigger collider and can be attacked.
    float timer;                                // Timer for counting up to the next attack.
    
    
    void Awake ()
    {
    
        player = GameObject.FindGameObjectWithTag ("Player");
        playerHealth = player.GetComponent <PlayerHealth> ();
        enemyHealth = GetComponent<EnemyHealth>();
        anim = GetComponent <Animator> ();
    }
    

    //OntriggerEnter 和 On triggerExit 每次玩家进入时敌人都会跟随敌人碰撞并在玩家退出时停止 void OnTriggerEnter(对撞机其他) { // 如果进入的对撞机是玩家... if(other.gameObject == 玩家) { // ... 玩家在范围内。 playerInRange = true; } }

    void OnTriggerExit (Collider other)
    {
        // If the exiting collider is the player...
        if(other.gameObject == player)
        {
            // ... the player is no longer in range.
            playerInRange = false;
        }
    }
    
    
    void Update ()
    {
        // Add the time since Update was last called to the timer.
        timer += Time.deltaTime;
    
        // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
        if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
        {
            // ... attack.
            Attack ();
        }
    
        // If the player has zero or less health...
        if(playerHealth.currentHealth <= 0)
        {
            // ... tell the animator the player is dead.
            anim.SetTrigger ("PlayerDead");
        }
    }
    
    
    void Attack ()
    {
        // Reset the timer.
        timer = 0f;
    
        // If the player has health to lose...
        if(playerHealth.currentHealth > 0)
        {
            // ... damage the player.
            playerHealth.TakeDamage (attackDamage);
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多