简体   繁体   English

unity 2D:简单的敌人射击

[英]unity 2D: simple enemy shooting

I'm working on a 2D game.我正在开发一个 2D 游戏。 If I wanted a simple enemy canon shooting side to side every 5 seconds, how would I go about it ?如果我想要一个简单的敌人大炮每 5 秒左右射击一次,我该怎么做?

I know that I would need to add a colider and rigidbody but not quite sure how to approach this, since am still grasping the whole idea我知道我需要添加一个 colider 和刚体,但不太确定如何解决这个问题,因为我仍然掌握整个想法

Red = Enemy/ Rough idea/Sketch红色 = 敌人/粗略的想法/草图

Thank you谢谢

What you want is to create a type of gameobject to be used as a 'bullet'.您想要的是创建一种用作“子弹”的游戏对象。 This gameobject when spawned has a script on it to make it travel in a certain direction.这个游戏对象在生成时有一个脚本,可以让它朝某个方向移动。

You can move them using force (physics) or you can translate them from one place to another which is to move them 'absolutely' and ignore physics in the environment.您可以使用力(物理)移动它们,或者您可以将它们从一个地方转移到另一个地方,即“绝对”移动它们并忽略环境中的物理。

Then you can use a collider on this object to detect when it hits the player using either the OnCollisionEnter method or the OnTriggerEnter method.然后,您可以使用此对象上的碰撞器来检测它何时使用 OnCollisionEnter 方法或 OnTriggerEnter 方法击中玩家。

There are some tutorials on it here so I hope they help.这里有一些关于它的教程,所以我希望它们有所帮助。 Creating Shooting 创作拍摄

First you need to think how your enemy should behavior, Just walk side by side or find the enemy as Navmesh.首先,您需要考虑您的敌人应该如何行为,只需并排走或以导航网格的形式找到敌人。

I found this scritpt on unity website:我在统一网站上找到了这个脚本:

using UnityEngine;使用 UnityEngine; using System.Collections;使用 System.Collections;

public class EnemyAttack : MonoBehaviour { public float timeBetweenAttacks = 0.5f;公共类 EnemyAttack : MonoBehaviour { public float timeBetweenAttacks = 0.5f; // The time in seconds between each attack. // 每次攻击之间的时间(以秒为单位)。 public int attackDamage = 10; public int attackDamage = 10; // The amount of health taken away per attack. // 每次攻击带走的生命值。

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 and On triggerExit the enemy will follow enemy everytime the player enter on collide and stop if player exit void OnTriggerEnter (Collider other) { // If the entering collider is the player... if(other.gameObject == player) { // ... the player is in range. // OntriggerEnter 和 On triggerExit 敌人会在每次玩家进入碰撞时跟随敌人,如果玩家退出则停止 void OnTriggerEnter (Collider other) { // 如果进入的碰撞器是玩家... if(other.gameObject == player) { // ... 玩家在范围内。 playerInRange = true; playerInRange = 真; } } } }

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);
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM