Unity2D 制作小游戏FlappyBird心得—Unity5学习笔记
前些天用Unity2D制作了一个小游戏FlappyBird,算作是计算机图形学这门课的一个小作业了,我用的Unity开发引擎版本是5.4.1_64-bit的,其中有些控件的脚本语法跟Unity4的不太一样,这也给我的自学过程造成了很多困扰,网上的代码很多都是Unity4版本的,没法在5上实现。
我写这篇文章希望能记录一些用Unity5做游戏的心得和技巧以及一些基本功能的实现方法(比如声音、动画、捆绑物体、随机生成物体、用图形显示得分数字等等),便于给自己和今后的Unity游戏开发初学者以参考。
用英文写了,就当练一下英语写作了,speaking of writing 都是泪啊T_T,我们说好的要分手呢,雅思?总分够了,写作只有5.5分 T_T……………..Zhazha English ,couhe look look
目录:
1.设置默认开发工具
2.素材收集
3.分层
4.拖入素材,设置背景图片层次
5.动画设计
6.给物体设置物理属性(刚体和球体)
7.合并柱子并添加碰撞边缘
8.脚本中调用并修改刚体属性及碰撞检测
9.随机生成柱子
10.用图形显示数字
11.添加音效
12.切换场景
13.添加按钮和文本框
附录I——C#script of one bird
I’m used to VS 2013:
Edit->Preferences->External Tools
Just drag the pictures into the Folder .
My friend helped me collect those .
And I know a website with lots of useful resources : itch.io
Some of them need payments ,but many of them are free to download with the source file of Unity .
Like the picture shows below ,in “inspector” view on the right ,click sorting layers and add two layers “back ”and “fore ”.
We are supposed to use the background with the size matching the screen .Here I use 640*480
Create a folder in the Assets named animation .
Drag a bird into the Hierarchy view and rename it .
Window ->Animation -> create -> Add property -> sprite renderer -> sprite
Set the samples to 10.
Delete keys.
And drag the pictures from the Assets
Select the item and do :
Component ->Physics 2D -> Rigidbody 2D and Circle Collider 2D
Then we can set the gravity scale and the Radius of the game object .
Drag in the two tubes named “1” and “2” and create two empty GameObject named “score” and “tubes”. Then ,drag these 3 items into the “tubes” .
Component->Physics 2D ->Edge Collider 2D
Inspector ->Edge Collider 2D ->Edit Collider
create Edge Collider 2D on “1” , ”2” and ”score”. Set the edge collider 2D along the edge of “1” and “2” . The “edge” of the “score” is in the middle of the two tubes.
Select “Is Trigger” so that we can judge the collision .
Then create anew folder named “prefab” and drag the “tubes” to this new folder for furtherpurpose .
In class
public Rigidbody2D rigid;
when you want to use it :
rigid = GetComponent();
rigid.gravityScale = 0;
rigid.velocity = new Vector3(0, speed, 0);
float newx=rigid.position.x;
These are different from Unity4
Judge the collision :
void OnTriggerEnter2D(Collider2D col){ if (col.name == "score"){ } }
public class tube : MonoBehaviour {
public GameObject tubes;
private GameObject newtubes;
private float time = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
time -= Time.deltaTime;
if (time < 0.5f){
time = 2;
float position_y = Random.Range(-4f, -1.5f);
Vector3 newp = new Vector3(3.2f, position_y,0);
newtubes=(GameObject)Instantiate(tubes,newp,Quaternion.identity);
Destroy(newtubes, 3);
}
}
}
Then drag the C#script to a new empty GameObject and select the “Tubes” in inspector
Note: guarantee those pictures are in a folder named Resources under the Assets.
void OnGUI(){} is a function like Update , which will be executed in every frame .
public class shownumber : MonoBehaviour {
public static Object[] numbers=new Object[10];//save pictures of numbers
public int number=0;
// Use this for initialization
void Start () {
for (int i = 0; i < 10; i++){
numbers[i] = Resources.Load("font_0" + (i +48).ToString());
}
}
public void OnGUI(){
drawnum(record.score_bird1, 0, 0,1);
if (record.players == 2){
drawnum(record.score_bird2, 590,0,2);
}
}
// Update is called once per frame
void Update () {
}
public static void drawnum(int num,int x,int y,int bird_ID){
string a = num.ToString();
int l = a.Length;
if(bird_ID==2) x -= (l-1) * 50;
int w =50 ;//tex.width;
int h = 50;//tex.height;
int bigcount = 3;
for (int i = 0; i < bigcount;i++){
if (i + l >= bigcount){
int n = a[i - bigcount + l] - '0';
GUI.DrawTexture(new Rect(x,y,w,h),(Texture2D)numbers[n]);
x += w;
}
}
}
}
Create a new folder named audio and put in some music clips.
Select the bird and do :
Component ->Audio ->Audio Source
In the script of the bird:
public AudioClip crash;
public AudioClip pick1;
private AudioSource audio_source;
void Start(){
audio_source = GetComponent();
}
void Update(){
audio_source.PlayOneShot(crash,1f);
}
Then attach the music to the public Audio Clip:
Create and save the scenes .
Open the scene that you just finished .
File ->Build Settings ->Add open scenes
Use :
Application.LoadLevel("game_over");
To go to a new scene
void OnGUI()
{
GUI.skin.button.fontSize = 25;
GUI.skin.label.fontSize = 20;
GUI.Label(new Rect(220, 100, 200, 100),"Unity");
if (GUI.Button(new Rect(330, 200, 200, 100),"1Player"))
{
record.players = 1;
record.highest_score_bird1= 0;
Application.LoadLevel("ready");
}
if (GUI.Button(new Rect(330, 350, 200, 100),"2Players"))
{
record.players = 2;
record.highest_score_bird1= 0;
record.highest_score_bird2= 0;
Application.LoadLevel("ready");
}
}
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class bird : MonoBehaviour {
public AudioClip pick1;
public AudioClip crash;
private AudioSource audio_source;
private float speed = 4;
public Rigidbody2D rigid;
private int is_cheating = 0;
void Start(){
audio_source = GetComponent();
record.score_bird1 = 0;
record.alive1 = 0;
}
// Update is called once per frame
void Update(){
if (Input.GetKeyDown(KeyCode.A)){
is_cheating ^= 1;
rigid = GetComponent();
if (is_cheating == 1){
rigid.gravityScale = 0;
}
else{
rigid.gravityScale = 1;
}
rigid.velocity = new Vector3(0, 0, 0);
}
if (is_cheating == 0){
if (Input.GetKeyDown(KeyCode.Space)){
rigid = GetComponent();
rigid.velocity = new Vector3(0, speed, 0);
}
}
else{
if (Input.GetKeyDown(KeyCode.DownArrow)){
rigid = GetComponent();
float newx=rigid.position.x;
float newy=rigid.position.y;
transform.Translate(0,-0.3f,0);
}
else if(Input.GetKeyDown(KeyCode.UpArrow)){
rigid = GetComponent();
float newx = rigid.position.x;
float newy = rigid.position.y;
transform.Translate(0, 0.3f,0);
}
}
}
void OnTriggerEnter2D(Collider2D col){
if (col.name == "score"){
audio_source.PlayOneShot(pick1,1f);
record.score_bird1++;
}
else{
audio_source.PlayOneShot(crash,1f);
record.alive1 = 1;
transform.position = new Vector3(100, 100, 0);
print("gameover!");
record.highest_score_bird1= max(record.highest_score_bird1,record.score_bird1);
if (record.alive1 == 1&& record.alive2 == 1){
Application.LoadLevel("game_over");
}
}
}
int max(int a, int b){
return a > b ? a : b;
}
}
If you want the whole project , contact me by Email : [email protected]