using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletMove : MonoBehaviour {
//子弹移动速度
private float bulletTime = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (Vector3.up * bulletTime * Time.deltaTime);
//当子弹超出屏幕后销毁
if (transform.position.y > 5f) {
Destroy (gameObject);
}
}
//触碰到敌机以后销毁子弹
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Enemy") {
Destroy (gameObject);
}
}
}
Unity打飞机(三) 敌机和空投的生成
[C#] 纯文本查看复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InEnemy : MonoBehaviour {
//实例化的位置
public Transform enemyLocation;
//实例敌机
public Transform enemySmall;
public Transform enemyMiddle;
public Transform enemyBig;
//空投
public Transform superBullet;
public Transform bomb;
// Use this for initialization
void Start () {
//循环调用某个方法 方法名 开始后多长时间开始调用 调用间隔
InvokeRepeating ("CreateEnemySamll", 1f, 0.2f);
InvokeRepeating ("CreateEnemyMiddle", 2f, 2f);
InvokeRepeating ("CreateEnemyBig", 3f, 4f);
InvokeRepeating ("CreateSuperButtle", 5f, 5.5f);
InvokeRepeating ("CreateBomb", 5.5f, 6f);
}
// Update is called once per frame
void Update () {
}
//小飞机
void CreateEnemySamll(){
var en = Instantiate (enemySmall);
en.position = new Vector3 (Random.Range(-2.8f, 2.8f), enemyLocation.position.y, 0f);
}
//中飞机
void CreateEnemyMiddle(){
var en = Instantiate (enemyMiddle);
en.position = new Vector3 (Random.Range(-2.8f, 2.8f), enemyLocation.position.y + 1f, 0f);
}
//大飞机
void CreateEnemyBig(){
var en = Instantiate (enemyBig);
en.position = new Vector3 (Random.Range(-2.8f, 2.8f), enemyLocation.position.y + 2f, 0f);
}
//超级子弹
void CreateSuperButtle(){
var sb = Instantiate (superBullet);
sb.position = new Vector3 (Random.Range(-2.8f, 2.8f), enemyLocation.position.y + 1.5f , 0f);
}
//炸弹
void CreateBomb(){
var b = Instantiate (bomb);
b.position = new Vector3 (Random.Range(-2.8f, 2.8f), enemyLocation.position.y + 2.5f , 0f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackgroundMove : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//背景移动
transform.Translate(Vector2.down * 0.09f);
if (transform.position.y < -9.9f) {
transform.position = new Vector3(0,9.9f,0);
}
}
}
Unity打飞机(六)各种按钮的点击事件,文本框内容改变和最终分数的记录,背景音乐播放
[C#] 纯文本查看复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ButtonAndText : MonoBehaviour {
public Image LogoImg;
public Button StartBtn, ExitBtn, pauseButton, beginButton, bombButton, pause;
public Text numText, bombText, maxText, maxNumText, newNum, newNumText;
//背景音乐
private AudioSource ads;
//得分和最高分
public static int num = 0;
private int maxNum = 0;
//炸弹数量
public static int bombNum = 0;
//是否有些结束
public static bool isOver = false;
// Use this for initialization
void Start () {
Time.timeScale = 0;
ads = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
numText.text = "当前得分:" + num;
bombText.text = "x" + bombNum;
//如果有些结束存取最高分
if (isOver) {
maxNum = PlayerPrefs.GetInt ("maxNum");
if (maxNum < num) {
maxNum = num;
PlayerPrefs.SetInt ("maxNum", maxNum);
}
maxNumText.text = "" + maxNum;
newNum.text = "" + num;
maxText.gameObject.SetActive (true);
maxNumText.gameObject.SetActive (true);
newNum.gameObject.SetActive (true);
newNumText.gameObject.SetActive (true);
pause.gameObject.SetActive (true);
pauseButton.gameObject.SetActive (false);
bombButton.gameObject.SetActive (false);
numText.gameObject.SetActive (false);
Time.timeScale = 0;
} else {
maxText.gameObject.SetActive (false);
maxNumText.gameObject.SetActive (false);
newNum.gameObject.SetActive (false);
newNumText.gameObject.SetActive (false);
pause.gameObject.SetActive (false);
}
}
//开始游戏 让帧数开始刷新 隐藏标题,开始和结束按钮
public void OnClickStart(){
Time.timeScale = 1;
LogoImg.gameObject.SetActive (false);
StartBtn.gameObject.SetActive (false);
ExitBtn.gameObject.SetActive (false);
pauseButton.gameObject.SetActive (true);
bombButton.gameObject.SetActive (true);
numText.gameObject.SetActive (true);
}
//暂停或者开始
public void OnClickPause(){
if (Time.timeScale == 1) {
ads.Stop ();
Time.timeScale = 0;
beginButton.gameObject.SetActive (true);
pauseButton.gameObject.SetActive (false);
} else {
ads.Play ();
Time.timeScale = 1;
beginButton.gameObject.SetActive (false);
pauseButton.gameObject.SetActive (true);
}
}
//结束游戏
public void OnClickExit(){
Application.Quit ();
}
//重新开始
public void OnClickOver(){
num = 0;
isOver = false;
EnemyAndSuperMove.num = 0;
SceneManager.LoadScene ("Airplane");
Time.timeScale = 1;
}
}