using UnityEngine;
using System.Collections;

/// <summary>
/// 背景滚动
/// </summary>
public class Done_BGScroller : MonoBehaviour
{
    /// <summary>
    /// 滚动速度
    /// </summary>
    public float scrollSpeed;
    /// <summary>
    /// z轴的长度
    /// </summary>
    public float tileSizeZ;

    /// <summary>
    /// 背景起始位置
    /// </summary>
    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;

/// <summary>
/// 被边界摧毁
/// </summary>
public class Done_DestroyByBoundary : MonoBehaviour
{
    /// <summary>
    /// 敌人,陨石等离开边界被摧毁
    /// </summary>
    void OnTriggerExit (Collider other)
    {
        Destroy(other.gameObject);
    }
}

DestroyByBoundary

using UnityEngine;
using System.Collections;

/// <summary>
/// 主角被敌对物碰到后销毁
/// </summary>
public class Done_DestroyByContact : MonoBehaviour
{
    /// <summary>
    /// 爆炸特效
    /// </summary>
    public GameObject explosion;
    /// <summary>
    /// 玩家爆炸特效
    /// </summary>
    public GameObject playerExplosion;
    /// <summary>
    /// 玩家得分
    /// </summary>
    public int scoreValue;
    /// <summary>
    /// 游戏控制器
    /// </summary>
    private Done_GameController gameController;

    void Start ()
    {
        GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent <Done_GameController>();
        }
        if (gameController == null)
        {
            Debug.Log ("Cannot find 'GameController' script");
        }
    }

    /// <summary>
    /// 碰到触发器
    /// </summary>
    /// <param name="other"></param>
    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;

/// <summary>
/// 特效经过一段时间后销毁
/// </summary>
public class Done_DestroyByTime : MonoBehaviour
{
    /// <summary>
    /// 生存周期
    /// </summary>
    public float lifetime;

    void Start ()
    {

        Destroy (gameObject, lifetime);
    }
}

DestroyByTime

using UnityEngine;
using System.Collections;

/// <summary>
/// 敌机的躲避策略
/// </summary>
public class Done_EvasiveManeuver : MonoBehaviour
{
    /// <summary>
    /// 边界
    /// </summary>
    public Done_Boundary boundary;
    /// <summary>
    /// 倾斜
    /// </summary>
    public float tilt;
    /// <summary>
    /// 闪避
    /// </summary>
    public float dodge;
    public float smoothing;
    /// <summary>
    ///
    /// </summary>
    public Vector2 startWait;
    /// <summary>
    ///
    /// </summary>
    public Vector2 maneuverTime;
    /// <summary>
    ///
    /// </summary>
    public Vector2 maneuverWait;

    private float currentSpeed;
    private float targetManeuver;

    void Start ()
    {
        currentSpeed = GetComponent<Rigidbody>().velocity.z;
        StartCoroutine(Evade());
    }

    /// <summary>
    /// 躲避协程
    /// </summary>
    /// <returns></returns>
    IEnumerator Evade ()
    {
        yield return new WaitForSeconds (Random.Range (startWait.x, startWait.y));
        while (true)
        {
            targetManeuver = Random.Range (, dodge) * -Mathf.Sign (transform.position.x);
            yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y));
            targetManeuver = ;
            yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y));
        }
    }

    void FixedUpdate ()
    {
        float newManeuver = Mathf.MoveTowards (GetComponent<Rigidbody>().velocity.x, targetManeuver, smoothing * Time.deltaTime);
        GetComponent<Rigidbody>().velocity = new Vector3 (newManeuver, 0.0f, currentSpeed);
        GetComponent<Rigidbody>().position = new Vector3
        (
            Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
        );

        GetComponent<Rigidbody>().rotation = Quaternion.Euler (, , GetComponent<Rigidbody>().velocity.x * -tilt);
    }
}

EvasiveManeuver

using UnityEngine;
using System.Collections;

/// <summary>
/// 游戏控制
/// </summary>
public class Done_GameController : MonoBehaviour
{
    /// <summary>
    /// 敌人预设列表
    /// </summary>
    public GameObject[] hazards;
    /// <summary>
    /// 敌人孵化的范围
    /// </summary>
    public Vector3 spawnValues;
    /// <summary>
    /// 每波敌人的数量
    /// </summary>
    public int hazardCount;

    /// <summary>
    /// 生成每个敌人后的等待时间
    /// </summary>
    public float spawnWait;

    /// <summary>
    /// 第一次孵化敌人的等待时间
    /// </summary>
    public float startWait;
    /// <summary>
    /// 生成每波敌人后的生成时间
    /// </summary>
    public float waveWait;

    /// <summary>
    /// 分数文本
    /// </summary>
    public GUIText scoreText;
    /// <summary>
    /// 重新开始文本
    /// </summary>
    public GUIText restartText;
    /// <summary>
    /// 游戏结束文本
    /// </summary>
    public GUIText gameOverText;

    /// <summary>
    /// 游戏是否结束
    /// </summary>
    private bool gameOver;
    /// <summary>
    /// 是否重新开始
    /// </summary>
    private bool restart;
    /// <summary>
    /// 得分
    /// </summary>
    private int score;

    void Start ()
    {
        gameOver = false;
        restart = false;
        restartText.text = "";
        gameOverText.text = "";
        score = ;
        UpdateScore ();
        StartCoroutine (SpawnWaves ());
    }

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

    /// <summary>
    /// 孵化波
    /// </summary>
    /// <returns></returns>
    IEnumerator SpawnWaves ()
    {
        //等待
        yield return new WaitForSeconds (startWait);
        //无限生成敌人
        while (true)
        {
            //生成敌人
            ; i < hazardCount; i++)
            {
                GameObject hazard = hazards [Random.Range (, 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;
            }
        }
    }

    /// <summary>
    /// 添加分数
    /// </summary>
    /// <param name="newScoreValue"></param>
    public void AddScore (int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore ();
    }

    /// <summary>
    /// 更新分数显示文本
    /// </summary>
    void UpdateScore ()
    {
        scoreText.text = "Score: " + score;
    }

    /// <summary>
    /// 游戏结束
    /// </summary>
    public void GameOver ()
    {
        gameOverText.text = "Game Over!";
        gameOver = true;
    }
}

GameController

using UnityEngine;
using System.Collections;

/// <summary>
/// 移动类
/// </summary>
public class Done_Mover : MonoBehaviour
{
    /// <summary>
    /// 移动速度
    /// </summary>
    public float speed;

    void Start ()
    {
        //给刚体设置一个初始速度
        GetComponent<Rigidbody>().velocity = transform.forward * speed;
    }
}

Mover

using UnityEngine;
using System.Collections;

/// <summary>
/// 边界
/// </summary>
[System.Serializable]
public class Done_Boundary
{
    public float xMin, xMax, zMin, zMax;
}

/// <summary>
/// 玩家控制
/// </summary>
public class Done_PlayerController : MonoBehaviour
{
    /// <summary>
    /// 移动速度
    /// </summary>
    public float speed;

    /// <summary>
    /// 倾斜
    /// </summary>
    public float tilt;
    /// <summary>
    /// 游戏边界
    /// </summary>
    public Done_Boundary boundary;

    /// <summary>
    /// 子弹预设
    /// </summary>
    public GameObject shot;
    /// <summary>
    /// 子弹射出的位置
    /// </summary>
    public Transform shotSpawn;
    /// <summary>
    /// 射击频率,每秒发射子弹数
    /// </summary>
    public float fireRate;

    /// <summary>
    /// 下一次子弹发射的时间
    /// </summary>
    private float nextFire;

    void Update ()
    {
        //按下发射,且当前时间大于下一次子弹发射的时间
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            //重新设置下一次发射子弹的时间
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
            GetComponent<AudioSource>().Play ();
        }
    }

    void FixedUpdate ()
    {
        //获取水平和垂直方向的输入值,将其应用在刚体的速度上
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        GetComponent<Rigidbody>().velocity = movement * speed;

        //限制刚体在边界内移动
        GetComponent<Rigidbody>().position = new Vector3
        (
            Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
        );

        //设置飞机在z轴的倾斜
        GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    }
}

PlayerController

using UnityEngine;
using System.Collections;

/// <summary>
/// 陨石随机移动
/// </summary>
public class Done_RandomRotator : MonoBehaviour
{
    /// <summary>
    /// 翻滚速度
    /// </summary>
    public float tumble;

    void Start ()
    {
        //设置陨石的角速度
        GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble;
    }
}

RandomRotator

using UnityEngine;
using System.Collections;

/// <summary>
/// 敌人的武器控制
/// </summary>
public class Done_WeaponController : MonoBehaviour
{
    /// <summary>
    /// 子弹预设
    /// </summary>
    public GameObject shot;
    /// <summary>
    /// 发射子弹的位置
    /// </summary>
    public Transform shotSpawn;
    /// <summary>
    /// 发射子弹频率,
    /// </summary>
    public float fireRate;
    /// <summary>
    /// 发射子弹延迟
    /// </summary>
    public float delay;

    void Start ()
    {
        InvokeRepeating ("Fire", delay, fireRate);
    }

    void Fire ()
    {
        Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
        GetComponent<AudioSource>().Play();
    }
}

WeaponController

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

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

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

    项目:https://pan.baidu.com/s/1jIDe2H4 using UnityEngine; using System.Collections; namespace VoidGame ...

  4. Survival Shooter 学习

    using UnityEngine; using System.Collections; namespace CompleteProject { /// <summary> /// 摄像机 ...

  5. Space Shooter 太空射击

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

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

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

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

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

  8. 非常优秀的iphone学习文章总结!

    This site contains a ton of fun tutorials – so many that they were becoming hard to find! So I put t ...

  9. Unity3d学习日记(二)

      跟着教程做让背景可以滚动起来并添加了背景的粒子特效,加入了敌机.   ctrl攻击,↑↓←→移动,Game Over后按R重新开始游戏.   Space Shooter游戏地址:http://ya ...

随机推荐

  1. 准备下上机考试,各种排序!!以后再添加和仿真像wiki上那样!

    #include <stdio.h> #include <string.h> #define N 6 typedef struct { ]; int score; }stude ...

  2. 深入理解java虚拟机----java技术体系(一)

    1.java技术体系 举例: class文件格式:如下图所示,java源代码可以根据不同的编译器可以编译成不同的代码.即可以自定义语言规范比如beanshell,并编写代码; 然后自己编写java编译 ...

  3. 异步设备IO OVERLAPPED结构(设备内核对象 事件内核对象 可提醒IO)

    同步IO是指:线程在发起IO请求后会被挂起,IO完成后继续执行. 异步IO是指:线程发起IO请求后并不会挂起而是继续执行.IO完毕后会得到设备驱动程序的通知. 一.异步准备与OVERLAPPED结构 ...

  4. CAN总线(1)--初探(更新中)

    前言: CAN总线可以控制可以使用Xilinx中IP核来直接实现,也可以使用专用的CAN芯片(例如:SJA1000)通过单片机和FPGA驱动控制来实现: 目前是使用控制器SJA1000来进行实现: C ...

  5. 对弈的Python学习笔记

    #主要序列类型 str list tuple #列表 list ls=[1,2,3,4]#末尾追加ls.append(5) #添加多个,扩展ls.extend([5,6,7]) #在某个位置插入一个值 ...

  6. Problem C: 默认参数:求圆面积

    Description 编写一个带默认值的函数,用于求圆面积.其原型为: double area(double r=1.0); 当调用函数时指定参数r,则求半径为r的圆的面积:否则求半径为1的圆面积. ...

  7. git 命令篇

    *利用命令在仓库新建文件 *远程克隆到本地 *查看子文件 *创建新的分支  合并分支 删除分支  *合并分支 冲突 当Git无法自动合并分支时,就必须首先解决冲突.解决冲突后,再提交,合并完成. 用g ...

  8. python3.6 安装第三方库 pyCryptodome 实现AES加密

    起因 前端日子写完的Python入库脚本,通过直接读取配置文件的内容(包含了数据库的ip,数据库的用户名,数据库的密码),因为配置文件中的数据库密码是明文显示的,所以不太安全,由此对其进行加密. 编码 ...

  9. python-局部变量和全局变量

    name = "feifei" def change_name(name): print("before change name:%s" % name) nam ...

  10. Python 网络通信协议 tcp udp区别

    网络通信的整个流程 在这一节就给大家讲解,有些同学对网络是既熟悉又陌生,熟悉是因为我们都知道,我们安装一个路由器,拉一个网线,或者用无限路由器,连上网线或者连上wifi就能够上网购物.看片片.吃鸡了, ...