新人骗分系列 Unity打飞机二及三四五六
本帖最后由 浯茗仔仔 于 2018-11-26 14:34 编辑Unity(二)子弹的移动与销毁
一个小时只能发一篇贴,这分骗的不连贯啊:'(weeqw 合成一个贴子发得了
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打飞机(三) 敌机和空投的生成
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);
}
}
Unity打飞机(四)敌机移动与触碰效果,和爆炸音效
用的是触发器
敌机和空投加了Rigidbody2D,设置Gravity Scale 为零,
飞机,子弹,空投和敌机都有Collider,并勾选Is Trigger
飞机碰到的是空投还是敌机时用枚举判断的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//枚举
public enum DownType{
Small,Middle,Big,SuperBullet,Bomb
}
//敌机移动
public class EnemyAndSuperMove : MonoBehaviour {
//设定枚举类型
public DownType type = DownType.Small;
//下落时间
private float downTime = 5f;
//血量
public int HP = 1;
//敌机是否爆炸
private bool isBomb = false;
//爆炸图片切换
private SpriteRenderer reder;
public Sprite[] EnemySprite;
//爆炸时间
private float bombTime;
//被打图片切换
public Sprite[] hitSprite;
//被打图片切换间隔
private float hitTime;
//分数
public static int num = 0;
//爆炸音效
private AudioSource audioSource;
// Use this for initialization
void Start () {
reder = GetComponent<SpriteRenderer> ();
audioSource = GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
//敌机移动并销毁超出屏幕的
transform.Translate (Vector3.down * downTime * Time.deltaTime);
if (transform.position.y < -4.5) {
Destroy (gameObject);
}
//判断敌机是否爆炸
if (isBomb) {
bombTime += Time.deltaTime;
int bt = (int)(bombTime / (1f / 10));
if (bt >= EnemySprite.Length) {
Destroy (gameObject);
num++;
ButtonAndText.num = num;
isBomb = false;
} else {
reder.sprite = EnemySprite ;
}
} else {
//判断是否为中和大飞机,实现被打图片切换
if ((type == DownType.Middle || type == DownType.Big ) && hitTime > 0) {
hitTime -= Time.deltaTime;
int hs = (int)(hitTime / (1f / 10)) % 2;
reder.sprite = hitSprite ;
}
}
}
//触发器
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Bullet" && (type != DownType.SuperBullet || type != DownType.Bomb)) { //判断是不是碰到子弹
SubtractHP ();
} else if (other.tag == "hero") { //判断是否触碰到飞机
if (type == DownType.SuperBullet || type == DownType.Bomb) {//判断是不是空投触碰到敌机
Destroy (gameObject);
}
}
}
//减血
void SubtractHP(){
HP--;
hitTime = 0.2f;
if (HP < 0) {
Destroy (gameObject.GetComponent<Rigidbody2D>());
//播放爆炸音效
audioSource.Play ();
EnemyBomb ();
}
}
//挂掉
public void EnemyBomb(){
isBomb = true;
}
}
Unity打飞机(五)背景移动
用的是两张图片交替下滑,我用的分辨率是320X480,图片下滑到Y轴哪里上调请按自己的分辨率调节
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打飞机(六)各种按钮的点击事件,文本框内容改变和最终分数的记录,背景音乐播放
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;
}
}
应该没什么难点了,所有代码基本都有注释
完结,撒花❀❀❀❀❀❀
Emmmm小白还是有压力 IsKev 发表于 2018-11-26 14:47
Emmmm小白还是有压力
可以留言提问,我有时间都会回复的{:1_921:} ;www可惜了,我不会。。 没有熟悉的Hello world 这不是酷安里的嘛 学习了,感谢分享! 额,这个我在达内参观学习的时候学过。。。{:301_977:} 我只是看看反正不懂
页:
[1]