博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Space Shooter 学习
阅读量:5820 次
发布时间:2019-06-18

本文共 10168 字,大约阅读时间需要 33 分钟。

 

 

using UnityEngine;using System.Collections;/// /// 背景滚动/// public class Done_BGScroller : MonoBehaviour{    ///     /// 滚动速度    ///     public float scrollSpeed;    ///     /// z轴的长度    ///     public float tileSizeZ;    ///     /// 背景起始位置    ///     private Vector3 startPosition;    void Start ()    {        //缓存起始位置        startPosition = transform.position;    }    void Update ()    {        float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ);        transform.position = startPosition + Vector3.forward * newPosition;    }}
BGScroller
using UnityEngine;using System.Collections;/// /// 被边界摧毁/// public class Done_DestroyByBoundary : MonoBehaviour{    ///     /// 敌人,陨石等离开边界被摧毁    ///     void OnTriggerExit (Collider other)     {        Destroy(other.gameObject);    }}
DestroyByBoundary
using UnityEngine;using System.Collections;/// /// 主角被敌对物碰到后销毁/// public class Done_DestroyByContact : MonoBehaviour{    ///     /// 爆炸特效    ///     public GameObject explosion;    ///     /// 玩家爆炸特效    ///     public GameObject playerExplosion;    ///     /// 玩家得分    ///     public int scoreValue;    ///     /// 游戏控制器    ///     private Done_GameController gameController;    void Start ()    {        GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");        if (gameControllerObject != null)        {            gameController = gameControllerObject.GetComponent 
(); } if (gameController == null) { Debug.Log ("Cannot find 'GameController' script"); } } ///
/// 碰到触发器 /// ///
void OnTriggerEnter (Collider other) { //如果是边界或敌人则不处理 if (other.tag == "Boundary" || other.tag == "Enemy") { return; } if (explosion != null) { Instantiate(explosion, transform.position, transform.rotation); } //如果碰到玩家,实例化玩家爆炸特效 //游戏结束 if (other.tag == "Player") { Instantiate(playerExplosion, other.transform.position, other.transform.rotation); gameController.GameOver(); } // gameController.AddScore(scoreValue); //删除碰到的游戏物体 Destroy (other.gameObject); //删除自身游戏物体 Destroy (gameObject); }}
DestroyByContact
using UnityEngine;using System.Collections;/// /// 特效经过一段时间后销毁/// public class Done_DestroyByTime : MonoBehaviour{    ///     /// 生存周期    ///     public float lifetime;    void Start ()    {                Destroy (gameObject, lifetime);    }}
DestroyByTime
using UnityEngine;using System.Collections;/// /// 敌机的躲避策略/// public class Done_EvasiveManeuver : MonoBehaviour{    ///     /// 边界    ///     public Done_Boundary boundary;    ///     /// 倾斜    ///     public float tilt;    ///     /// 闪避    ///     public float dodge;    public float smoothing;    ///     ///     ///     public Vector2 startWait;    ///     ///     ///     public Vector2 maneuverTime;    ///     ///     ///     public Vector2 maneuverWait;    private float currentSpeed;    private float targetManeuver;    void Start ()    {        currentSpeed = GetComponent
().velocity.z; StartCoroutine(Evade()); } ///
/// 躲避协程 /// ///
IEnumerator Evade () { yield return new WaitForSeconds (Random.Range (startWait.x, startWait.y)); while (true) { targetManeuver = Random.Range (1, dodge) * -Mathf.Sign (transform.position.x); yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y)); targetManeuver = 0; yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y)); } } void FixedUpdate () { float newManeuver = Mathf.MoveTowards (GetComponent
().velocity.x, targetManeuver, smoothing * Time.deltaTime); GetComponent
().velocity = new Vector3 (newManeuver, 0.0f, currentSpeed); GetComponent
().position = new Vector3 ( Mathf.Clamp(GetComponent
().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp(GetComponent
().position.z, boundary.zMin, boundary.zMax) ); GetComponent
().rotation = Quaternion.Euler (0, 0, GetComponent
().velocity.x * -tilt); }}
EvasiveManeuver
using UnityEngine;using System.Collections;/// /// 游戏控制/// public class Done_GameController : MonoBehaviour{    ///     /// 敌人预设列表    ///     public GameObject[] hazards;    ///     /// 敌人孵化的范围    ///     public Vector3 spawnValues;    ///     /// 每波敌人的数量    ///     public int hazardCount;    ///     /// 生成每个敌人后的等待时间    ///     public float spawnWait;    ///     /// 第一次孵化敌人的等待时间    ///     public float startWait;    ///     /// 生成每波敌人后的生成时间    ///     public float waveWait;        ///     /// 分数文本    ///     public GUIText scoreText;    ///     /// 重新开始文本    ///     public GUIText restartText;    ///     /// 游戏结束文本    ///     public GUIText gameOverText;        ///     /// 游戏是否结束    ///     private bool gameOver;    ///     /// 是否重新开始    ///     private bool restart;    ///     /// 得分    ///     private int score;        void Start ()    {        gameOver = false;        restart = false;        restartText.text = "";        gameOverText.text = "";        score = 0;        UpdateScore ();        StartCoroutine (SpawnWaves ());    }        void Update ()    {        if (restart)        {            if (Input.GetKeyDown (KeyCode.R))            {                Application.LoadLevel (Application.loadedLevel);            }        }    }        ///     /// 孵化波    ///     /// 
IEnumerator SpawnWaves () { //等待 yield return new WaitForSeconds (startWait); //无限生成敌人 while (true) { //生成敌人 for (int i = 0; i < hazardCount; i++) { GameObject hazard = hazards [Random.Range (0, hazards.Length)]; Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate (hazard, spawnPosition, spawnRotation); yield return new WaitForSeconds (spawnWait); } //生成每波敌人后的等待时间 yield return new WaitForSeconds (waveWait); if (gameOver) { restartText.text = "Press 'R' for Restart"; restart = true; break; } } } /// /// 添加分数 /// /// public void AddScore (int newScoreValue) { score += newScoreValue; UpdateScore (); } /// /// 更新分数显示文本 /// void UpdateScore () { scoreText.text = "Score: " + score; } /// /// 游戏结束 /// public void GameOver () { gameOverText.text = "Game Over!"; gameOver = true; }}
GameController
using UnityEngine;using System.Collections;/// /// 移动类/// public class Done_Mover : MonoBehaviour{    ///     /// 移动速度    ///     public float speed;    void Start ()    {        //给刚体设置一个初始速度        GetComponent
().velocity = transform.forward * speed; }}
Mover
using UnityEngine;using System.Collections;/// /// 边界/// [System.Serializable]public class Done_Boundary {    public float xMin, xMax, zMin, zMax;}/// /// 玩家控制/// public class Done_PlayerController : MonoBehaviour{    ///     /// 移动速度    ///     public float speed;    ///     /// 倾斜    ///     public float tilt;    ///     /// 游戏边界    ///     public Done_Boundary boundary;    ///     /// 子弹预设    ///     public GameObject shot;    ///     /// 子弹射出的位置    ///     public Transform shotSpawn;    ///     /// 射击频率,每秒发射子弹数    ///     public float fireRate;        ///     /// 下一次子弹发射的时间    ///     private float nextFire;        void Update ()    {        //按下发射,且当前时间大于下一次子弹发射的时间        if (Input.GetButton("Fire1") && Time.time > nextFire)         {            //重新设置下一次发射子弹的时间            nextFire = Time.time + fireRate;            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);            GetComponent
().Play (); } } void FixedUpdate () { //获取水平和垂直方向的输入值,将其应用在刚体的速度上 float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); GetComponent
().velocity = movement * speed; //限制刚体在边界内移动 GetComponent
().position = new Vector3 ( Mathf.Clamp (GetComponent
().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (GetComponent
().position.z, boundary.zMin, boundary.zMax) ); //设置飞机在z轴的倾斜 GetComponent
().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent
().velocity.x * -tilt); }}
PlayerController
using UnityEngine;using System.Collections;/// /// 陨石随机移动/// public class Done_RandomRotator : MonoBehaviour {    ///     /// 翻滚速度    ///     public float tumble;        void Start ()    {        //设置陨石的角速度        GetComponent
().angularVelocity = Random.insideUnitSphere * tumble; }}
RandomRotator
using UnityEngine;using System.Collections;/// /// 敌人的武器控制/// public class Done_WeaponController : MonoBehaviour{    ///     /// 子弹预设    ///     public GameObject shot;    ///     /// 发射子弹的位置    ///     public Transform shotSpawn;    ///     /// 发射子弹频率,    ///     public float fireRate;    ///     /// 发射子弹延迟    ///     public float delay;    void Start ()    {        InvokeRepeating ("Fire", delay, fireRate);    }    void Fire ()    {        Instantiate(shot, shotSpawn.position, shotSpawn.rotation);        GetComponent
().Play(); }}
WeaponController

游戏视频:https://pan.baidu.com/s/1o87Anwe

游戏项目:https://pan.baidu.com/s/1c246OQs

转载于:https://www.cnblogs.com/revoid/p/6431729.html

你可能感兴趣的文章
linux网络
查看>>
Sublime Text 之 Package Control 镜像
查看>>
Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map
查看>>
linux中ctrl+z和ctrl+c的区别
查看>>
【ASP.NET 类库】当你懒得用 Json+Ajax 时,可以试试 AjaxPro
查看>>
Http get方式url参数长度以及大小
查看>>
css3动画简介以及动画库animate.css的使用
查看>>
6个超炫酷的HTML5电子书翻页动画
查看>>
Mina框架断包、粘包问题解决方式
查看>>
PHPCMS V9 环境搭建
查看>>
ResourceBundle的使用
查看>>
Javascript 严格模式use strict详解
查看>>
数据源架构模式之活动记录
查看>>
[原创]Android Handler使用Message的一个注意事项
查看>>
Svn Patch 中文乱码
查看>>
关于actionbar
查看>>
mysql实战优化之二:limit优化(大表翻页查询时) sql优化
查看>>
DJANGO和UIKIT结合,作一个有进度条的无刷新上传功能
查看>>
HTML学习笔记之中的一个(input文件选择框的封装)
查看>>
Threejs 官网 - Three.js 的图形用户界面工具(GUI Tools with Three.js)
查看>>