项目:https://pan.baidu.com/s/1jIDe2H4

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {
     public class Asteroid:MonoBehaviour {

         public GameObject m_explosionPrefab;    //爆炸Prefab
         public float m_moveSpeed = 5.0f;   //移动速度
         public float m_rotateSpeed = 1.0f;  //旋转速度

         private Rigidbody m_rigidbody;

         void Start() {
             m_rigidbody = GetComponent<Rigidbody>();
             m_rigidbody.velocity = Vector3.back * m_moveSpeed;  //速度
             m_rigidbody.angularVelocity = Random.insideUnitSphere * m_rotateSpeed;  //角速度
         }

         //小行星碰到玩家销毁自己
         void OnTriggerEnter(Collider other) {
             if(other.CompareTag("Player") || other.CompareTag("PlayerBullet")) {
                 Destroy(gameObject);
                 Explosion();
             }
         }

         //创建爆炸效果,2秒后删除
         void Explosion() {
             GameObject explosion = Instantiate(m_explosionPrefab,transform.position,transform.rotation) as GameObject;
             Destroy(explosion,2.0f);
         }
     }
 }

Asteroid

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {
     public class Background:MonoBehaviour {

         public float m_scrollSpeed = 1.0f; //滚动速度;

         void Update() {
             Scroll();
         }

         void Scroll() {
             if(transform.position.z <= -30.0f) {
                 transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z + 60.0f);
             } else {
                 transform.position += Vector3.back * m_scrollSpeed * Time.deltaTime;
             }

         }
     }
 }

Background

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {
     public class DestroyBoundary:MonoBehaviour {

         //任何物体超出边界范围摧毁
         void OnTriggerExit(Collider other) {
             Destroy(other.gameObject);
         }
     }
 }

DestroyBoundary

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;

 namespace VoidGame {
     public class GameManager:MonoBehaviour {

         public static GameManager m_instance;
         public GameObject[] m_harzardPrefabs;   //要生成的对象数组
         public Vector3 m_spawnRange;    //生成范围
         public bool m_isGameOver;   //游戏是否结束

         private Text m_scoreText;   //分数文本框
         private Text m_gameOverText;    //游戏结束文本框
         private Text m_restartText; //重来文本框
         private int m_score;    //分数

         void Awake() {
             m_instance = this;

         }

         void Start() {
             m_scoreText = GameObject.Find("ScoreText").GetComponent<Text>();
             m_scoreText.text = "得分:0";
             m_gameOverText = GameObject.Find("GameOverText").GetComponent<Text>();
             m_gameOverText.enabled = false;
             m_restartText = GameObject.Find("RestartText").GetComponent<Text>();
             m_restartText.enabled = false;
             StartCoroutine(Spawn());
         }

         void Update() {
             if(m_isGameOver) {
                 if(Input.GetKeyDown(KeyCode.R)) {
                     Application.LoadLevel(Application.loadedLevel);
                 }
             }
         }

         IEnumerator Spawn() {
             while(true) {
                 if(m_isGameOver) {
                     m_gameOverText.enabled = true;
                     m_restartText.enabled = true;
                     break;
                 }
                 ); //每波等待时间
                 ;i < m_harzardPrefabs.Length;i++) {
                     //在限定的x轴范围内随机生成
                     Vector3 spawnPosition = new Vector3(Random.Range(-m_spawnRange.x,m_spawnRange.x),m_spawnRange.y,m_spawnRange.z);
                     Instantiate(m_harzardPrefabs[i],spawnPosition,Quaternion.identity);
                     yield return new WaitForSeconds(0.5f);  //生成单个物体候等待的时间
                 }
             }
         }

         //添加分数并显示
         public void AddScore(int score) {
             m_score += score;
             m_scoreText.text = "得分:" + m_score.ToString();
         }
     }
 }

GameManager

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {

     [System.Serializable]
     public class MoveBoundary {
         public float m_minX;
         public float m_maxX;
         public float m_minZ;
         public float m_maxZ;
     }
 }

MoveBoundary

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {
     public class Enemy:MonoBehaviour {

         public GameObject m_bulletPrefab;   //子弹Prefab
         public GameObject m_explosionPrefab; //爆炸Prefab
         public float m_moveSpeed = 5.0f;    //移动速度

         private Transform m_bulletSpawnerTransform;
         private Rigidbody m_rigidbody;
         private AudioSource m_audioSource;

         void Start() {
             m_rigidbody = GetComponent<Rigidbody>();
             m_rigidbody.velocity = Vector3.back * m_moveSpeed;
             m_bulletSpawnerTransform = transform.Find("BulletSpawner");
             m_audioSource = GetComponent<AudioSource>();
             StartCoroutine(Shoot());
         }

         //敌人碰到玩家,玩家子弹销毁自己
         void OnTriggerEnter(Collider other) {
             if(other.CompareTag("Player") || other.CompareTag("PlayerBullet")) {
                 Destroy(gameObject);
                 Explosion();
             }
         }

         //射击
         IEnumerator Shoot() {
             while(true) {
                 yield return new WaitForSeconds(0.5f);
                 Instantiate(m_bulletPrefab,m_bulletSpawnerTransform.position,Quaternion.identity);
                 m_audioSource.Play();
             }
         }

         //爆炸
         void Explosion() {
             GameObject explosion = Instantiate(m_explosionPrefab,transform.position,transform.rotation) as GameObject;
             Destroy(explosion,2.0f);
         }
     }
 }

Enemy

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {
     public class EnemyBullet:MonoBehaviour {

         public float m_moveSpeed = 20.0f;   //移动速度

         private Rigidbody m_rigidBody;

         void Start() {
             m_rigidBody = GetComponent<Rigidbody>();
             m_rigidBody.velocity = Vector3.back * m_moveSpeed;   //给刚体设置速度
         }

         //敌人子弹碰到 玩家 销毁自己
         void OnTriggerEnter(Collider other) {
             if(other.CompareTag("Player")) {
                 Destroy(gameObject);
             }
         }
     }
 }

EnemyBullet

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {
     public class Player:MonoBehaviour {

         public GameObject m_explosionPrefab;    //爆炸Prefab
         public GameObject m_playerBulletPrefab;   //子弹Prefab
         public float m_fireRate = 0.25f;    //子弹射击频率
         public float m_moveSpeed = 10.0f;   //移动速度
         public MoveBoundary m_moveBoundary; //移动边界

         private float m_nextFire;   //下一次发射时间
         private Transform m_bulletSpawnerTransform; //子弹发射器
         private AudioSource m_audioSource;
         private Rigidbody m_rigidbody;

         void Start() {
             m_audioSource = GetComponent<AudioSource>();
             m_rigidbody = GetComponent<Rigidbody>();
             m_bulletSpawnerTransform = transform.Find("BulletSpawner");
         }

         void Update() {
             //大于下一次发射子弹的时间才能发射子弹
             if(Input.GetButton("Fire1") && Time.time > m_nextFire) {
                 Shoot();
             }
         }

         void FixedUpdate() {
             float moveHorizontal = Input.GetAxis("Horizontal"); //水平方向移动
             float moveVertical = Input.GetAxis("Vertical"); //垂直方向移动

             Vector3 movement = ,moveVertical);  //移动方向
             m_rigidbody.velocity = movement * m_moveSpeed;  //刚体速度 = 移动方向 * 移动速度
             //固定飞机的移动范围
             m_rigidbody.position = ,Mathf.Clamp(m_rigidbody.position.z,m_moveBoundary.m_minZ,m_moveBoundary.m_maxZ));
             m_rigidbody.rotation = Quaternion.Euler(0f,0f,m_rigidbody.velocity.x * -4.5f);  //旋转
         }

         //玩家碰到敌人,敌人子弹,行星爆炸并销毁自己
         void OnTriggerEnter(Collider other) {
             if(other.CompareTag("Enemy")|| other.CompareTag("EnemyBullet") || other.CompareTag("Asteroid")) {
                 Destroy(gameObject);
                 Explosion();
                 GameManager.m_instance.m_isGameOver = true;
             }
         }

         //射击
         void Shoot() {
             m_nextFire = Time.time + m_fireRate;
             Instantiate(m_playerBulletPrefab,m_bulletSpawnerTransform.position,Quaternion.identity);
             m_audioSource.Play();
         }

         //爆炸
         void Explosion() {
             GameObject explosion = Instantiate(m_explosionPrefab,transform.position,transform.rotation) as GameObject;
             Destroy(explosion,2.0f);
         }
     }
 }

Player

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {

     public class PlayerBullet:MonoBehaviour {

         public float m_moveSpeed = 20.0f;   //移动速度
         private Rigidbody m_rigidBody;

         void Start() {
             m_rigidBody = GetComponent<Rigidbody>();
             m_rigidBody.velocity = Vector3.forward * m_moveSpeed;    //给刚体设置速度
         }

         //玩家子弹碰到敌人,小行星销毁
         void OnTriggerEnter(Collider other) {
             if(other.CompareTag("Enemy") || other.CompareTag("Asteroid")) {
                 GameManager.m_instance.AddScore();
                 Destroy(gameObject);
             }
         }
     }
 }

PlayerBullet

Space Shooter的更多相关文章

  1. 源于《Unity官方实例教程 “Space Shooter”》思路分析及相应扩展

    教程来源于:Unity官方实例教程 Space Shooter(一)-(五)       http://www.jianshu.com/p/8cc3a2109d3b 一.经验总结 教程中步骤清晰,并且 ...

  2. jspace2d——A free 2d multiplayer space shooter

    http://code.google.com/p/jspace2d/ —————————————————————————————————————————————————————————————— We ...

  3. Space Shooter 学习

    using UnityEngine; using System.Collections; /// <summary> /// 背景滚动 /// </summary> publi ...

  4. Space Shooter 太空射击

    1.控制玩家移动 public float speed = 10f; public float xMin = -6.5f; public float xMax = 6.5f; public float ...

  5. Unity 官方自带的例子笔记 - Space Shooter

    首先 买过一本叫 Unity3D开发的书,开篇第一个例子就是大家经常碰见的打飞机的例子,写完后我觉得不好玩.后来买了一本 Unity 官方例子说明的书,第一个例子也是打飞机,但是写完后发现蛮酷的,首先 ...

  6. unity官方案例精讲(第三章)--星际航行游戏Space Shooter

    案例中实现的功能包括: (1)键盘控制飞船的移动: (2)发射子弹射击目标 (3)随机生成大量障碍物 (4)计分 (5)实现游戏对象的生命周期管理 导入的工程包中,包含着一个完整的 _scene--- ...

  7. 绿色 或者 免安装 软件 PortableApps

    Refer to http://portableapps.com/apps for detail. Below is just a list at Jan-01-2017 for quick show ...

  8. Game Development Patterns and Best Practices (John P. Doran / Matt Casanova 著)

    https://github.com/PacktPublishing/Game-Development-Patterns-and-Best-Practices https://github.com/m ...

  9. Hands-On Unity 2018 x 移动游戏开发教程

    Hands-On Unity 2018 x Game Development for Mobile 使用Unity 2018.2创建具有出色游戏功能的精彩游戏   想学习在Unity制作游戏,但不知道 ...

随机推荐

  1. 了解JVM加载实例化类的原理

    class Singleton { private static Singleton instance = new Singleton(); public static int a; public s ...

  2. 微信企业号 JS-SDK:上传图片

    微信的JS-SDK提供了微信客户端相关的功能,如:拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享.扫一扫等微信特有的能力,为微信用户提供更优质的网页体验.这里将会介绍如何通过调用JS ...

  3. visual studio 2010 无法连接到ASP.NET Development Server

    在点击运行项目时,生成成功.但是页面没有弹出来,弹出个提示框,无法连接到 ASP.NET Development Server. 网上我看到说关闭掉防火墙,可是再关掉防火墙后还是不行.但是其他的项目又 ...

  4. python调用ansible api 2.0 运行playbook带callback返回

    # -*- coding:utf8 -*- ''' Created on 2017年1月13日 @author: qiancheng ''' import os import json from co ...

  5. java-并发解决方案

    并发产生数据不一致的原因:1.程序共享对象:2.多线程.3.基于1和2,取出来的数据可能不是最新的. 解决方案:只要是原子性操作,就不会出现问题.原子性操作,代表cpu会一直执行这个操作,知道结束. ...

  6. [日记&做题记录]-Noip2016提高组复赛 倒数十天

    写这篇博客的时候有点激动 为了让自己不颓 还是写写日记 存存模板 Nov.8 2016 今天早上买了两个蛋挞 吃了一个 然后就做数论(前天晚上还是想放弃数论 但是昨天被数论虐了 woc noip模拟赛 ...

  7. Heap Sort

    #include<iostream> using namespace std; const int MAX = 1001; int l[MAX]; //Heap Sort void Hea ...

  8. 运行机制和JVM

    Java代码编译后生成一种与平台无关的字节码(也就是class文件).当然,这种字节码不是可执行的,必须使用Java解释器来解释执行. 负责解释执行字节码文件的是Java虚拟机,即JVM.JVM是可运 ...

  9. linux下如何查询jdk安装路径

    1:echo $JAVA_HOME 使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$JAVA_HOME,否则如下所示,根本定位不到JDK的安装路径 [root@localho ...

  10. mysql 保存emoji时报,数据库报错:Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x82\xF0\x9F...' for column 'review' at row 1

    错误原因:我们可以看到错误提示中的字符0xF0 0x9F 0x98 0x84 ,这对应UTF-8编码格式中的4字节编码(UTF-8编码规范).正常的汉字一般不会超过3个字节,为什么为出现4个字节呢?实 ...