Enemy Boss运动AI

时间:2014-07-16 01:48:55

标签: unityscript

对所有人来说,美好的一天,我试图让敌人的老板船出现在屏幕上攻击我的船并且有时还躲避我的攻击。到目前为止,我让他攻击我的船,但他仍然停留在屏幕的边缘。这是我的代码:

#pragma strict

// here are public variables for the enemy ship that can be accessed in the inspector
var health:int = 2;
var explosion:GameObject;
var expOrb:GameObject;
var enemyBullet:GameObject;
var expDrop:int = 3;
var hitSound:AudioClip;
var fireRate:float = 2.0;

//heres the private variable counter to keep track of time for fire rate.
private var counter:float = 0.0;

function Update () {
   //here we make counter count based on time for the fire rate
   counter += Time.deltaTime;

   //if the ship goes too far left, we destroy it.
   if(transform.position.x < -12){
      Destroy(gameObject);
   }

   //here we shoot 4 bullets if the counter counts higher than the fire rate.
   if(counter > fireRate){
      var custom1 = Instantiate(enemyBullet, transform.position - Vector3(0.5,0.1,0), Quaternion.Euler(-90,0,0));
      var custom2 = Instantiate(enemyBullet, transform.position - Vector3(0.5,0.1,0), Quaternion.Euler(-90,0,0));
      var custom3 = Instantiate(enemyBullet, transform.position- Vector3(0.5,0.1,0), Quaternion.Euler(-90,0,0));
      var custom4 = Instantiate(enemyBullet, transform.position- Vector3(0.5,0.1,0), Quaternion.Euler(-90,0,0));
      //to make the bullets spread, we add extra z velocity to each one to they all move on their own path.
      custom1.rigidbody.velocity.z = 3;
      custom2.rigidbody.velocity.z = 1;
      custom3.rigidbody.velocity.z = -1;
      custom4.rigidbody.velocity.z = -3;
      counter = 0.0;
   }

   //end of function update
}

//if a bullet hits the ship, the bullets sends us the hit message to trigger this function to bring down the ships health
function hit () {
   health -= 1;
   if(health != 0){
      if(audio.enabled == true){
         audio.PlayOneShot(hitSound);
      }
   }
   if(health <= 0){
      onDeath();
   }
}

//if health is 0, then this function is triggered to spawn some orbs, spawn the explosion animation object, and destroy itself
function onDeath () {
   Instantiate(expOrb,transform.position,Quaternion.Euler(-90,0,0));
   expDrop -= 1;
   if(expDrop <= 0){


       Instantiate(explosion,transform.position,Quaternion.Euler(-90,Random.Range(-180,180),0));
       Destroy(gameObject);
    }
    if(expDrop > 0){
       onDeath();
    }
 }

如何添加运动方面?

1 个答案:

答案 0 :(得分:0)

移动对象有很多种方法,您可以尝试:

  1. Transform.translatehttp://docs.unity3d.com/ScriptReference/Transform.Translate.html

  2. 修改transform.positionhttp://answers.unity3d.com/questions/188998/transformposition.html

  3. 向其添加力量(http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

  4. 还有很多其他方法我还不知道。

  5. 对于这种游戏中的老板,你可能想要的运动是随机移动或跟随玩家。两者都不难实现。

    • 随机移动:只需做Random.Range(-1, 1) * bossSpeed * time.deltaTime并将其应用于老板&#39; x位置。
    • 跟随玩家:获得玩家的x位置然后让老板调整到该位置。

    然后如何让老板有时躲闪玩家的子弹?你可以让老板通过添加一个位于其前面的对手来检测玩家的子弹是否进入。如果玩家的子弹与其发生碰撞,则随机另一个数字(例如从0到1)。之后,如果随机数为1,你只需要移走老板,当随机数为0时,不要移动老板。