Unity小游戏《飞机大战》实战代码学习

1.   飞机大战背景的处理方法

准备2个无缝背景贴图,位置设置好,连接在一起的。

每个背景都放同样的代码1个。

using UnityEngine;
using System.Collections;
public class move0 : MonoBehaviour {
    
void Start () {
}
void Update () {
        transform.Translate(Vector3.back * Time.deltaTime * 5,Space.World);           //背景图移动代码
        if (transform.position.z <= -30)                                     //设置一定移动范围
        {
            transform.position=new Vector3(transform.position.x, transform.position.y, 30);  //超过这个范围自动复位
        }
}
}

思路:想让背景一直给人向上移动的效果,先让他向下走,走过一个界限,向上移动一个身为就行了。

Z<=-30,可能不好理解,但是运行后看着,就懂了,就是移动了2个身位的距离(我设置Z=30),只有移动了2个Z的距离,其中一个的移动才从视线中消失,然后从上面重新开始。


2.子弹移动的方法

这个代码放在子弹身上

using UnityEngine;
using System.Collections;

public class zidanmove : MonoBehaviour {
    public float bulletspeed;                                     //设置变量:子弹移动速度
    public GameController g;                                   //设置g,跨脚本调用方法(调用谁就设置谁的类的g)                                                                 void Start()
    {
    Destroy(gameObject, 6.0f);                                    //设置子弹消失时间
    g = GameObject.Find("GameObject").GetComponent<GameController>();                                                        }
    void Update () {
        zdmove();                                                  //update调用子弹移动代码
    }    
    void zdmove()                                                  //定义子弹移动代码
    {
  transform.Translate(transform.forward*Time.deltaTime* bulletspeed,Space.World);//translate(方向,速度,时间)             
    }

     private void OnTriggerEnter(Collider other) {          //使用OnTriggerEnter方法,设置碰撞后使其他敌人消失
        if (other.tag != "Player") {                      //前台设置好自己的子弹的标签为"Player"
            Destroy(other.gameObject);                     
            Destroy(this.gameObject);                                                                                                 g.Ssss();
        } 
    }
}

3.player玩家自己的飞机代码

  玩家飞机的代码里,有1个是飞机自身移动的代码(活动范围限制),和子弹实例化复制的代码,第二个代码不能放在子弹上,所以在飞机上

using UnityEngine;
using System.Collections;

public class move : MonoBehaviour
{
    public float xMin, xMax, zMin, zMax;           //定义飞机移动的最大最小值的范围                                                                                                  
    public float startTime;                          
    private float CountTime;                       //使用startTime和CountTime组成一个沙漏计时器

    public GameObject zidan1;                      //设置好zidan1,前台把子弹的预制体拖进去

    GameController g;                              //跨类调用方法,需要先设置自己的类的g,
   
    void Start()
    {
        CountTime = startTime;                                       //计时器相等

       g = GameObject.Find("GameObject").GetComponent<GameController>();  //寻找另一个物体的脚本的方法名
    }

    void Update()
    {
        feijimove();                      //实时监测飞机移动代码
        zidanslh();                       //实时在鼠标左击时实例化复制生成子弹
    }

    void feijimove()
    {
        float MoveH = Input.GetAxis("Horizontal");               //水平输入读写
        float MoveV = Input.GetAxis("Vertical");                 //垂直输入读写 
        transform.Translate(new Vector3(MoveH, 0, MoveV) * Time.deltaTime * 5);   //玩家飞机移动代码
        if (transform.position.x <= xMin)      //如果x<=xMin,就是出game窗口中的背景了,他就在背景里,这样                                                                                       就限制了飞机的x范围,下面同理。
        {
            transform.position = new Vector3(xMin, 0, transform.position.z);
        }
        if (transform.position.x >= xMax)
        {
            transform.position = new Vector3(xMax, 0, transform.position.z);
        }
        if (transform.position.z <= zMin)
        {
            transform.position = new Vector3(transform.position.x, 0, zMin);
        }
        if (transform.position.z >= zMax)
        {
            transform.position = new Vector3(transform.position.x, 0, zMax);
        }
    }

    void zidanslh()                     //子弹实例化代码
    {
    CountTime = CountTime - Time.deltaTime;   // 如果沙漏计时器CountTime减去每一帧所用的时间=CountTime,也就是上半部的沙漏时间用完,上半部沙漏时间用完,就开始运行的
                                                                                                                                                                                                                          
    if (Input.GetButtonDown("Fire1") && CountTime <= 0)            //如果点击左键并且正在开始运行
        {
  Instantiate(zidan1, transform.position, zidan1.transform.rotation);          //实例化一个子弹
  CountTime = startTime;              //然后再把ST赋给CT,沙漏倒过来,重新开始计时,上半部漏完,继续运行,再倒过来                                                                                                                                                                              
        }
    }

private void OnTriggerEnter(Collider other) {         //碰撞效果代码
        if (other.tag != "Player") {
            Destroy(other.gameObject);
            Destroy(this.gameObject);
            g.JiaZai();               //调用另一个类的重新加载代码
        }
    }

}

4.敌机代码

using UnityEngine;
using System.Collections;

public class enemyplane : MonoBehaviour {
    public float startTime;                    //计时器,同上
    private float CountTime;
    public GameObject zidan2;                  //定义子弹2,前台拖入
    public float speed2;                       //定义敌机飞行速度
    public GameObject explo;                   //定义”爆炸“,在素材里拖入爆炸效果
    void Start() {
        CountTime = startTime;
        Destroy(gameObject, 6.0f);
    }

    void Update() {
        zidanslh();                              //敌机子弹实例化
        transform.Translate(transform.forward * Time.deltaTime * speed2);    //敌机移动代码
    }


    private void OnTriggerEnter(UnityEngine.Collider other) {                 //敌机碰撞代码
        if (other.tag != "Enemy") { 
       Instantiate(explo,transform.position,Quaternion.identity);    //实例化碰撞效果!
            Destroy(this.gameObject);
            Destroy(other.gameObject);
        }
    }

    void zidanslh() {                                                //敌机子弹实例化代码
        CountTime = CountTime - Time.deltaTime;    
        if (CountTime <= 0) {
            Instantiate(zidan2, transform.position, zidan2.transform.rotation);
            CountTime = startTime;
        }
    }
}

5.敌机子弹移动代码

using UnityEngine;
using System.Collections;

public class Zidan2move : MonoBehaviour {
public float bulletspeed2;        //定义敌机子弹移动速度
public GameObject explo;          //定义并拖入碰撞效果包
void Start () { 
Destroy(gameObject, 6.0f);        //设置敌机子弹消失时间
}

void Update () {
        transform.Translate(Vector3.back * Time.deltaTime * bulletspeed2, Space.World);  //子弹移动
    }    

private void OnTriggerEnter(UnityEngine.Collider other) {     //碰撞触发
        if (other.tag != "Enemy") {
            Destroy(this.gameObject);
            Destroy(other.gameObject);
        }
    }
}

6.关于敌机的随机复制和敌方陨石的随机复制,我们新建一个空物体,GameObject,把实例化的代码放在这个空物体上,注意不要把名字写成”GameOBject“
代码:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;   导入场景的包,方便一下重新加载场景
public class GameController : MonoBehaviour {
    public GameObject[] enemy;        //定义一个数组,存在需要随机出现的敌机和陨石

    bool a = false;              //定义开关
    public Text GameOver;         //定义文本
    public Text Restart;
    public Text Score;

    public float fenzhi=0;            //设置分值为0
void Start () { 
     StartCoroutine("Spawn");    //开始运行随机生成的协程
}

void Update () {               
   Score.text = "得分:" + fenzhi.ToString();       //输出分数文本,变换数据类型                                                                       if (a == true) {              //实时监测a是不是等于0
   GameOver.text = "Game  Over!!";
   Restart.text = "Press R To Restart";                                                                                     if (Input.GetKeyDown("r")) {  //当a=0,并且输入r,重新加载场景
  SceneManager.LoadScene(0);
}
}
}

IEnumerator Spawn() {             //协程Spawn()
    while (true) {                //永真
   int i = Random.Range(0, enemy.Length);     //定义随机数i,范围0-随机数组的个数
   Instantiate(enemy[i], new Vector3(Random.Range(-4.3f, 4.3f),0,7.0f),enemy[i].transform.rotation);//实例化生成数组成员,和出现的距离大小
   yield return new WaitForSeconds(Random.Range(0.2f, 1.2f));   //设置随机生成的时间隔断
        }
    }

public void JiaZai() {             //public一个方法,用于跨脚本调用方法
   a = true;                       
        }
}                                                                                                                        public void  Ssss() {              //定义一个加分方法,在玩家子弹打掉后自动加分
        fenzhi = fenzhi +10;
    }

7 陨石移动代码

using UnityEngine;
using System.Collections;

public class StoneMove : MonoBehaviour {
public GameObject explo;
public float speed;
   void Start () {
Destroy(gameObject, 6.0f);
}

   void Update () {
        transform.Translate(Vector3.forward * Time.deltaTime * speed,Space.World);
    }

private void OnTriggerEnter(UnityEngine.Collider other) {
        if (other.tag != "Enemy") {
            Destroy(this.gameObject);
            Destroy(other.gameObject);
        }
    }
}

这样,一个简单的飞机大战游戏就做好了,记住给所有的物体加上碰撞器,(碰撞器根据形状设置),勾选IS Trigger,都加上刚体。

所有的源码在下面:

https://pan.lanzou.com/i0dxgob

带音效版

https://pan.lanzou.com/i0dxkyf

  • 20
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值