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# 统一2D:简单的敌人射击_C#_Unity3d - Fatal编程技术网

C# 统一2D:简单的敌人射击

C# 统一2D:简单的敌人射击,c#,unity3d,C#,Unity3d,我正在做一个2D游戏。如果我想要一个简单的敌人大炮每5秒左右射击一次,我会怎么做呢 我知道我需要添加一个colider和rigidbody,但不太确定如何处理这个问题,因为我仍然掌握了整个想法 谢谢你你想要的是创建一种游戏对象作为“子弹”使用。此游戏对象在生成时有一个脚本,使其沿特定方向移动 你可以使用力物理来移动它们,或者你可以将它们从一个地方转移到另一个地方,这就是“绝对”地移动它们,而忽略环境中的物理 然后,您可以使用此对象上的碰撞器,使用OnCollisionCenter方法或OnTri

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

我知道我需要添加一个colider和rigidbody,但不太确定如何处理这个问题,因为我仍然掌握了整个想法


谢谢你

你想要的是创建一种游戏对象作为“子弹”使用。此游戏对象在生成时有一个脚本,使其沿特定方向移动

你可以使用力物理来移动它们,或者你可以将它们从一个地方转移到另一个地方,这就是“绝对”地移动它们,而忽略环境中的物理

然后,您可以使用此对象上的碰撞器,使用OnCollisionCenter方法或OnTriggerCenter方法检测碰撞播放器的时间

这里有一些关于它的教程,所以我希望它们能有所帮助。

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

我在unity网站上找到了这张纸条:

使用UnityEngine; 使用系统集合

公共类攻击:单一行为 { public float timeBetweenAttacks=0.5f;//每次攻击之间的时间(以秒为单位)。 public int attackDamage=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 ontriggenter对撞机其他 { //如果进入的碰撞器是玩家。。。 ifother.gameObject==玩家 { //…玩家在射程内。 playerlange=true; } }

}

您可以找到Unity教程。您将学习如何实例化对象并向其添加force。您还将了解碰撞回调。在以后的问题中,如果没有添加代码,请不要问更多的问题。
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);
    }
}