吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3542|回复: 9
收起左侧

[其他转载] 新人骗分系列 Unity打飞机二及三四五六

[复制链接]
浯茗仔仔 发表于 2018-11-26 14:25
本帖最后由 浯茗仔仔 于 2018-11-26 14:34 编辑

Unity(二)子弹的移动与销毁

一个小时只能发一篇贴,这分骗的不连贯啊    合成一个贴子发得了
[C#] 纯文本查看 复制代码
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);
        }
}


Unity打飞机(四)敌机移动与触碰效果,和爆炸音效

用的是触发器
敌机和空投加了Rigidbody2D,设置Gravity Scale 为零,
飞机,子弹,空投和敌机都有Collider,并勾选Is Trigger

飞机碰到的是空投还是敌机时用枚举判断的

[C#] 纯文本查看 复制代码
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 [bt];
                        }

                } else {
                        //判断是否为中和大飞机,实现被打图片切换
                        if ((type == DownType.Middle || type == DownType.Big ) && hitTime > 0) {
                                hitTime -= Time.deltaTime;
                                int hs = (int)(hitTime / (1f / 10)) % 2;
                                reder.sprite = hitSprite [hs];
                        }
                }
        }

        //触发器
        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轴哪里上调请按自己的分辨率调节

[C#] 纯文本查看 复制代码
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;

        }
}



应该没什么难点了,所有代码基本都有注释


完结,撒花&#10048;&#10048;&#10048;&#10048;&#10048;&#10048;

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

IsKev 发表于 2018-11-26 14:47
Emmmm小白还是有压力
 楼主| 浯茗仔仔 发表于 2018-11-26 14:51
hgfty1 发表于 2018-11-26 15:07
陌小全 发表于 2018-11-26 15:23
没有熟悉的Hello world
wapj0123 发表于 2018-11-27 08:55
这不是酷安里的嘛
头像被屏蔽
zimuxiaosheng 发表于 2018-11-27 22:38 来自手机
提示: 作者被禁止或删除 内容自动屏蔽
tzhtzhtzh 发表于 2018-11-28 01:06
学习了,感谢分享!
唯一de小黯 发表于 2018-12-13 16:18
额,这个我在达内参观学习的时候学过。。。
天罪 发表于 2018-12-13 16:49 来自手机
我只是看看反正不懂
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-20 11:17

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表