Unity-飞⾏射击实例的代码
花了⼀天的时间,照着官⽅的说明将spaceshoot这个DEMO撸了⼀遍,实现了⼀个简单飞⾏射击游戏应有的⼩功能,本想着⽤这个组件把这个游戏做成⼀个联⽹的,⽆奈只能实现飞机运动之间的同步,其余部分画⾯都是不同步的。等经过进⼀步的学习之后我会再把这个DEMO 进⾏完善。
接下来就分析⼀下这个DEMO的各个游戏对象与组件的关系。
1.场景:这个飞⾏射击游戏只有需要⼀个场景,⼀张⽤图⽚来做背景的星空图,⼀些粒⼦效果来模拟星星。
2.游戏对象:1.1玩家飞机:⽤WASD来进⾏控制,移动有范围限制,⿏标左键开⽕,撞到⼦弹,陨⽯,敌机会爆炸并附带⾳效;
1.2 陨⽯:在玩家上⽅随机⽣成并向下移动并具有旋转,离开边界范围会⾃动销毁,被⼦弹击中会爆炸,撞击到玩家飞机会爆炸并附带⾳效 1.3 敌机:在玩家上⽅随机⽣成并向下移动,并且可以开⽕,被⼦弹击中会爆炸,与玩家飞机相撞会爆炸,超出边界范围会销毁
1.4 ⼦弹:可以由玩家和敌机⽣成,在Z轴上具有⼀定的速度,遇见玩家,陨⽯,敌机,进⾏爆炸并附带⾳效,超过⽣命周期会销毁
3.UI界⾯:需要⼀个计分板,玩家死亡后的结束游戏提⽰,和按R重新开始提⽰
脚本:
3.1玩家控制脚本
3.2敌对物体移动与旋转位置随机化脚本using UnityEngine;using System.Collections;using UnityEngine.Networking;[System.Serializable]public class Boundary { public float xMin,zMin,xMax,zMax;//这是设定玩家运动的边界范围}public class playercontroller : MonoBehaviour { public float speed;//移动速度 public Boundary boundary;//边界 public float tilt = 4;//旋转速度 public float fireRate = 0.2f;//开或延迟 public GameObject shot;//⼦弹对象 public Transform shotspawn;//⼦弹⽣成地点 private float nextfire = 0;//开⽕间隔 // U this for initialization void Update () { //if (isLocalPlayer) { if (Input.GetButton ("Fire1") && Time.time > nextfire) {//按下⿏标左键并且时间⼤于开⽕间隔 nextfire = Time.time + fireRate;//更新最近开⽕时间 Instantiate (shot, shotspawn.position, ation); //⽣成⼦弹对象 GetComponent<AudioSource> ().Play ();//播放射击⾳效 } //} } // Update is called once per frame void FixedUpdate () { //if (isLocalPlayer) { float moveHorizontal = Input.GetAxis ("Horizontal");//获得⽔平输⼊值 float moveVertical = Input.G
etAxis ("Vertical");//获得竖直⽅向上的输⼊值 Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);//定义⼀个三维变量⽤于储存玩家进⾏的移动操作 Rigidbody rb = GetComponent<Rigidbody> (); if (rb != null ) { rb.velocity = movement * speed;//设定刚体在某⽅向上的速度 rb.position = new Vector3 (Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax), 0, Mathf.Clamp (rb.position.z, boundary.zMin, boundary. rb.rotation = Quaternion.Euler (0, 0, rb.velocity.x * -tilt);//计算旋转,因为Z 轴上的旋转逆时针为正值,所以假设向左移动,X 轴上的速度为负值,这时 } } //}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36上海越剧院
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
3.3销毁游戏对象的⽅法脚本:超出边界,碰撞,超过⽣命周期//设定移动速度using UnityEngine;using System.Collections;public class Mover : MonoBehaviour { public float speed; void Start () { GetComponent<Rigidbody>().velocity = transform.forward * speed;//设定Z 轴上的速度 }}//随机旋转速度using UnityEngine;using System.Collections;public class RandomRatator : MonoBehaviour { public float tumble;//旋转速度 void Start () { GetComponent<Rigidbody> ().angularVelocity = Random.insideUnitSphere * tumble;//随机化欧拉⾓的速度 }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19wps搜索
20
21
22
23
24//按时销毁using UnityEngine;using System.Collections;public class DestroyByTime : MonoBehaviour { public float lifeTime=2.0f;//设定⽣命周期为2S void Start () { Destroy (gameObject, lifeTime);//指定时间后销毁游戏对象 }}//超过边界销毁using UnityEngine;using System.Collections;public class DestoyByBoundary : MonoBehaviour { void OnTriggerExit (Collider other) //离开触发器 { Destroy(other.gameObject);//销毁游戏对象 }}//碰撞销毁using UnityEngine;using System.Collections;using UnityEngine.Networking;public class DestroyByContact : MonoBehaviour { public int scoreValues;//本物体被击杀分数 private GameController gameController1;//游戏控制对象 public GameObject explosion;//爆炸粒⼦效果 p
ublic GameObject playerExplosion;//玩家爆炸粒⼦效果 void OnTriggerEnter(Collider other)//进⼊触发器 {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
if (other.tag == "Boundary" || other.tag == "Enemy")//如果是边界或敌机就不爆炸,避免误伤 return ; Instantiate (explosion, transform.position, ation); if (other.tag == "Player") {//撞到玩家 Instantiate (playerExplosion, ansform.position, ation);//播放玩家的爆炸动画 gameController1.gameOver ();//游戏结束,进⼊重置画⾯ } gameController1.addScore (scoreValues);//增加分数 Destroy (other.gameObject);//删除对⽅游戏对象 Destroy (gameObject);//⾃我删除 } void Start () { GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");//获得游戏控制器对象 if (gameControllerObject != null ) { gameC
ontroller1 = gameControllerObject.GetComponent <GameController>();//初始化游戏控制组件 } if (gameController1 == null ) { Debug.Log ("Cannot find 'GameController' script"); } }}//玩家爆炸脚本using UnityEngine;using System.Collections;public class player_explosion : MonoBehaviour { private GameController gameController1; public GameObject playerExplosion; // U this for initialization void Start () { GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController"); if (gameControllerObject != null ) { gameController1 = gameControllerObject.GetComponent <GameController>(); } if (gameController1 == null ) { Debug.Log ("Cannot find 'GameController' script"); } } void OnTriggerEnter(Collider other) { if (other.tag == "Boundary"||other.tag=="Player")//遇到边界或其他玩家不爆炸 return ; if (other.tag == "Bullet") {//遇见⼦弹,因为陨⽯和敌机已经写了遇见玩家爆炸两者⼀起销毁的代码,所以只需要写⼦弹的 Instantiate (playerExplosion, ansform.position, ation);//播放爆炸动画 gameController1.gameOver();//游戏结束 } Destroy (other.gameObject); Destroy (gameObject); }}
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
安静的音乐
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
3.4游戏控制脚本public class GameController : NetworkBehaviour { public Text scoreText;//计分板 public Text gameOverText;//游戏结束提⽰ public Text restartText;//重新开始提⽰ public bool restart;//是否重置 public bool gameover;//是否结束 public int score;//初始分数 public float starWait;//开始之前的等待时间 public float waveWait;//每⼀波敌⼈之间的间隔 public float
spawnwait;//每个物体⽣成之间的间隔 public float hazardCount;//⼀波敌⼈的数量 public GameObject[] hazards;//敌对物体游戏对象数组 public Vector3 spawnValues;//出⽣地点 private Vector3 spawnPosition = ; private Quaternion spawnRotation;//出⽣旋转速度 public void addScore (int newScoreValues){ score += newScoreValues; UpdateScore ();//更新分数 } public void gameOver () { gameover = true ; ="游戏结束"; } public void UpdateScore () { = "得分:" + score; } IEnumerator spawnWaves()//使⽤了协程的函数写法 { yield return new WaitForSeconds (starWait);//游戏开始前的间隔 while (true ){ if (gameover) { ="按R 键重新开始游戏"; restart = true ; break ; } 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);//随机化物体X 轴上的位 spawnRotation = Quaternion.identity;//没旋转之前的初始⾓度 Instantiate (hazard, spawnPosition, spawnRotation);//⽣成物体 yield return new WaitForSeconds (spawnwait);//⽣成下个物体之间的间隔 } yield return new WaitForSeconds (waveWait);//每波敌⼈之间的间隔 } } void Start () { = ""; = ""; gameover = fal ; restart = fal ; score = 0; UpdateScore ();//初始化各项数据1
2
3
4
5
6
7
上海景色
8
9
10狐狸怎么写
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
美文60053
54
55
56
57
58
59
60
读后感八百字>高中生减肥61
62
63