public void Shoot() { GameObject bullet = mF.GetBullet(tankType.Player); //获取子弹,传入的参数表示发出子弹的坦克类型 bullet.transform.position = new Vector3(player.transform.position.x, 1.5f, player.transform.position.z) + player.transform.forward * 1.5f; //设置子弹位置 bullet.transform.forward = player.transform.forward; //设置子弹方向 Rigidbody rb = bullet.GetComponent <Rigidbody>(); rb.AddForce(bullet.transform.forward * 20, ForceMode.Impulse); //发射子弹 }
IEnumerator Shoot() //协程实现npc坦克每隔1s进行射击 { while (!gameover) { for (float i = 1; i > 0; i -= Time.deltaTime) { yield return(0); } if (Vector3.Distance(transform.position, target) < 20) //和玩家坦克距离小于20,则射击 { MyFactory mF = Singleton <MyFactory> .Instance; GameObject bullet = mF.GetBullet(tankType.Enemy); //获取子弹,传入的参数表示发射子弹的坦克类型 bullet.transform.position = new Vector3(transform.position.x, 1.5f, transform.position.z) + transform.forward * 1.5f; //设置子弹 bullet.transform.forward = transform.forward; //设置子弹方向 Rigidbody rb = bullet.GetComponent <Rigidbody>(); rb.AddForce(bullet.transform.forward * 20, ForceMode.Impulse); //发射子弹 } } }