前言: 做的Unity的课程设计
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Start : MonoBehaviour
{
// To change the scene
public void startGame()
{
SceneManager.LoadScene("Plane");
}
public void startGame1()
{
SceneManager.LoadScene("Sence_2");
}
public void startGame2()
{
SceneManager.LoadScene("Sence_3");
}
// To restart the game
public void Restart()
{
SceneManager.LoadScene("Start");
}
// To quit the game
public void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour {
// 在控制面板自定义的速度
public float rotSpeed;
void Start () {
Rigidbody rbd = GetComponent ();
// .insideUnitSphere 以一个为球形,半径为1,经行旋转
rbd.angularVelocity = Random.insideUnitSphere * rotSpeed;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour {
public float speed;
// tilt是倾斜程度的变量
public float tilt;
private Rigidbody rbd;
public Boundary bound;
// 飞船的子弹
public GameObject bullet;
public GameObject bullet_2;
public GameObject bullet_3;
// 子弹的的发射位置
public Transform shotpos;
public Transform shotpos_2;
public Transform shotpos_3;
// 子弹发射的时间间隙
public float shottime;
// 子弹下次射击的时间
private float nextshot = 0f;
// 子弹发射的音效
private AudioSource shotAudio;
void Start () {
// 获取刚体组件,刚体组件,控制物体的运动
rbd = GetComponent ();
shotAudio = GetComponent ();
}
void Update () {
// 按下鼠标左键,创建子弹物体,攻击时间并不连续
if (Input.GetButton ("Fire1") && Time.time > nextshot) {
nextshot = Time.time + shottime;
// 实例化子弹物体
Instantiate (bullet, shotpos.position, shotpos.rotation);
Instantiate (bullet_2, shotpos_2.position, shotpos_2.rotation);
Instantiate (bullet_3, shotpos_3.position, shotpos_3.rotation);
// 每实例化一个子弹物体就会播放音效
shotAudio.Play();
}
}
private void FixedUpdate(){
// 获取水平输入
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis ("Vertical");
// 制作一个飞船位置的三维向量
Vector3 vel = new Vector3 (h, 0, v);
// 飞船运动的速度信息, .velocity代表飞船的速度信息
rbd.velocity = vel*speed;
// 产生偏转
rbd.rotation = Quaternion.Euler(0,0,rbd.velocity.x*(-1)*tilt);
// 限定飞船的运动位置
float posX = Mathf.Clamp(rbd.position.x, bound.Xmin, bound.Xmax);
float posZ = Mathf.Clamp(rbd.position.z, bound.Zmin, bound.Zmax);
rbd.position = new Vector3 (posX, 0, posZ);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Game_manage : MonoBehaviour {
// 定义的敌人们
public GameObject[] enemys;
// 每生成一个敌人的间隔
public float spawnWait;
// 每生成一波敌人的数量
public int waveCount;
// 生成每波敌人的等待时间
public float waveWait;
// 启动游戏开始生成敌人的等待时间
public float startWait;
// 累加的分数
private int Score_count;
// UI上的分数
public Text UIScore;
// UI上的显示面板
public GameObject End_game;
// 主角的存活状态,初始代表存活
private bool is_GameOver = false;
void Start () {
// startCoutine代表开启一个协程,固定的用法
StartCoroutine(SpawnWaves());
// 发布以后固定屏幕大小
Screen.SetResolution (540, 960, false);
}
// 随机生成敌人物体(协程类型的函数)
IEnumerator SpawnWaves(){
// 启动游戏等待一段时间开始掉落敌人
yield return new WaitForSeconds (startWait);
while (true) {
for (int i = 0; i < waveCount; ++i) {
int index = Random.Range (0, enemys.Length);
GameObject go = enemys [index];
// 随机生成的位置信息
Vector3 pos = new Vector3(Random.Range(-4,4), 0 ,8);
// 生成时候的旋转信息
Quaternion rot = Quaternion.identity;
// 对物体进行实例化
Instantiate (go, pos, rot);
// 需要等待的时间
yield return new WaitForSeconds (spawnWait);
}
// 当主角死亡的时候,不再生成敌人和陨石,跳出出生逻辑
if (is_GameOver) {
break;
}
// 生成每一波所需要的等待时间
yield return new WaitForSeconds (waveWait);
}
}
// 命中敌人增加分数
public void Add_score(int value){
Score_count += value;
Debug.Log ("Score" + Score_count);
// 更新界面分数信息
UIScore.text = "Score: " + Score_count.ToString();
}
public void Game_Over(){
// 激活UI界面
End_game.SetActive (true);
// 判断主角死亡
is_GameOver = true;
}
/*public void restart_Game(){
// 重新加载场景
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}*/
public void Restart()
{
SceneManager.LoadScene("Start");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 限定极限位置,中括号的内容表示其可序列化啦
[System.Serializable]
public class Boundary
{
public float Xmin,Xmax,Zmin,Zmax;
}
public class Escape : MonoBehaviour {
// tilt是倾斜程度的变量
public float tilt;
public Boundary bound;
// 最小的随机速度和最大的随机速度,用来使敌机看着更加的真实
public float Min_speed;
public float Max_speed;
// 为了避免飞船速度变化生硬,这里来定义有个加速度
public float Acceler_speed;
// 第一次开始闪避的时间的范围
public float Wait_min;
public float Wait_max;
// 闪避最短最长时间
public float dodgeMinTime;
public float dodgeMaxTime;
// 随机闪避目标速度
private float Mix_speed;
// 定义一个刚体组件,使得飞船可以真实的闪避
private Rigidbody rbd;
void Start () {
// 拿到这个组件
rbd = GetComponent ();
StartCoroutine(Calc_speed ());
}
IEnumerator Calc_speed(){
// while循环可以让飞船一直闪避
while (true) {
// 飞船出生后多少秒开始闪避
yield return new WaitForSeconds (Random.Range (Wait_min, Wait_max));
Mix_speed = Random.Range (Mix_speed, Max_speed);
// 规定飞船的闪避方向
if (transform.position.x > 0) {
// 飞船在右面,现在需要向左面走
Mix_speed = -Mix_speed;
}
// 闪避后多长时间开始重新闪避
yield return new WaitForSeconds (Random.Range (dodgeMinTime, dodgeMaxTime));
// 闪避完了以后,速度归零
Mix_speed = 0;
}
}
private void FixedUpdate () {
// 定义出来一个过渡的速度(通过加速度产生的渐变速度)
float divide_speed = Mathf.MoveTowards(rbd.velocity.x, Mix_speed, Time.deltaTime*Acceler_speed);
// .velocity方法是获取刚体组件的速度信息
rbd.velocity = new Vector3(divide_speed, 0, rbd.velocity.z);
// 产生偏转
rbd.rotation = Quaternion.Euler(0,0,rbd.velocity.x*(-1)*tilt);
// 限定飞船的运动位置
float posX = Mathf.Clamp(rbd.position.x, bound.Xmin, bound.Xmax);
float posZ = Mathf.Clamp(rbd.position.z, bound.Zmin, bound.Zmax);
rbd.position = new Vector3 (posX, 0, posZ);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_bullet : MonoBehaviour {
// 飞船的子弹
public GameObject bullet;
// 子弹的的发射位置
public Transform shotpos;
public Transform shotpos_1;
public Transform shotpos_2;
// 子弹发射的时间间隙
public float shottime;
// 等待多长时间会发射一个子弹
public float shotWait;
// 敌机发射子弹的音效
private AudioSource shotAudio;
void Start () {
InvokeRepeating ("EnemyShipFire", shotWait, shottime);
shotAudio = GetComponent ();
}
void EnemyShipFire(){
Instantiate (bullet, shotpos.position, shotpos.rotation);
Instantiate (bullet, shotpos_1.position, shotpos_1.rotation);
Instantiate (bullet, shotpos_2.position, shotpos_2.rotation);
shotAudio.Play ();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destory : MonoBehaviour {
// 刚体运动的函数
void FixedUpdate()
{
// 子弹物体飞出去之后,7秒以后销毁,避免消耗完内存
Destroy(gameObject,7);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Contact_check : MonoBehaviour {
// 敌人自身的爆炸效果
public GameObject enemy_Explosion;
// 主角飞船的爆炸效果
public GameObject player_Explosion;
// 跨脚本函数调用
private Game_manage Acompent;
// 物体被销毁时玩家获得的分数
public int score;
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Game_mgr");
Acompent = go.GetComponent ();
}
private void OnTriggerEnter(Collider other){
// 如果敌人之间发生碰撞,不发生爆炸效果
if (other.tag == "Enemy") {
return;
}
if (enemy_Explosion != null) {
// 实例化特效
Instantiate (enemy_Explosion, transform.position, transform.rotation);
}
if (other.tag == "Player") {
Instantiate (player_Explosion, other.transform.position, other.transform.rotation);
Acompent.Game_Over();
}
// 增加分数
Acompent.Add_score(score);
// 销毁陨石和飞船(需要先销毁陨石本身)
Destroy(other.gameObject);
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullt_move : MonoBehaviour {
public float flyspeed;
void Start () {
Rigidbody rbd = GetComponent ();
// 指定速度的运动方式,只是适用于
rbd.velocity = transform.forward * flyspeed;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BG : MonoBehaviour {
// 背景移动的速度
public float scrollSpeed;
private Vector3 startPos;
void Start () {
// 获取开始的时候背景的位置
startPos = transform.position;
}
void Update () {
// 负方向运动,并且用time做一个合适的速度
// 函数来控制循环的运动 取模运算 会造成0到30之间的往返运动
float dis = Mathf.Repeat(scrollSpeed * Time.time,30);
transform.position = startPos + dis * Vector3.forward*(-1);
}
}