Unity2D 制作小游戏FlappyBird心得—Unity5学习笔记
来源:互联网 发布:mysql nvl 函数 编辑:程序博客网 时间:2024/06/14 11:17
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
1.At the initial stage of the project ,set the default external tools .
I’m used to VS 2013:
Edit->Preferences->External Tools
2.Collect some assets including audios and pictures .
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 .
3. create layers:Edit->project settings->Tags and layers
Like the picture shows below ,in “inspector” view on the right ,click sorting layers and add two layers “back ”and “fore ”.
4.drag the picture of background into the Hierarchy and rename it and set the sorting layer as back.
We are supposed to use the background with the size matching the screen .Here I use 640*480
5.make an animation
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
6.set rigidbody2D and circlebody
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 .
7. set tubes
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 .
8.Scripts about birds with Rigidbody 2D and Collider 2D (with all details in the end)
In class
public Rigidbody2D rigid;
when you want to use it :
rigid = GetComponent<Rigidbody2D>();rigid.gravityScale = 0;rigid.velocity = new Vector3(0, speed, 0);float newx=rigid.position.x;
</pre><p><span style="font-size:18px;">These are different from Unity4</span></p><p><span style="font-size:18px;"></span></p><p><span style="font-size:18px;">Judge the collision :</span></p><p><pre name="code" class="csharp">void OnTriggerEnter2D(Collider2D col){ if (col.name == "score"){ }}
9. create tubes randomly
public class tube : MonoBehaviour { public GameObject tubes; private GameObject newtubes; private float time = 0; // Use this for initialization <span style="white-space:pre"></span>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); }<span style="white-space:pre"></span>}}
Then drag the C#script to a new empty GameObject and select the “Tubes” in inspector
10.show numbers with pictures
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; } } }}
11.create audio
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<AudioSource>();}void Update(){ audio_source.PlayOneShot(crash,1f);}
Then attach the music to the public Audio Clip:
12.change scenes
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
13.button and text-lable
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"); } }
C#script of one Bird
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<AudioSource>(); 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<Rigidbody2D>(); 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<Rigidbody2D>(); rigid.velocity = new Vector3(0, speed, 0); } } else{ if (Input.GetKeyDown(KeyCode.DownArrow)){ rigid = GetComponent<Rigidbody2D>(); float newx=rigid.position.x; float newy=rigid.position.y; transform.Translate(0,-0.3f,0); } else if(Input.GetKeyDown(KeyCode.UpArrow)){ rigid = GetComponent<Rigidbody2D>(); 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 : qdbszsj@163.com
- Unity2D 制作小游戏FlappyBird心得—Unity5学习笔记
- 应用unity5.3.4制作flappybird小游戏(学习笔记 )
- android小游戏制作心得(三)——游戏线程
- Unity学习笔记 [1st - 3rd] —— 小游戏制作完成
- 线程小游戏制作的心得与体会
- u3d制作多米诺骨牌小游戏心得1
- 使用Construct2制作小游戏的心得
- Unity2D学习
- unity2D小鸟飞过柱子小游戏
- 我的Unity3D学习日记-06(自己动手制作FlappyBird)
- 学习制作拼图小游戏总结
- android小游戏制作心得(一)——Activity的编写
- android小游戏制作心得(二)——状态机和常数类
- android小游戏制作心得(四)——GameView的实现
- 【cocos2dx-3.0beta-制作flappybird】——背景介绍
- Unity2D学习笔记(一):sprite的基础操作
- Unity2D学习笔记(二):sprite动画编辑
- Python小游戏学习制作《兔子和獾》
- Socket多客户端简易签到系统
- 卡片游戏
- Getting Started with the Vulkan SDK
- redis主从
- Java命令--javap
- Unity2D 制作小游戏FlappyBird心得—Unity5学习笔记
- Coding 代码管理快速入门(转)
- 在模拟器上成功运行的代码在真机上出现FragmentManager对象为空的问题
- js 数组随机排序
- 全局标签
- 最长递增子序列 O(NlogN)算法
- Java单例模式的七种写法
- JBullet开发环境搭建
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)