U3D 2D游戏之黑暗纪元 2D游戏基础入门开发全(1)
第一个U3D 2D游戏的例子,全部自己编写,算是入门用,这里记录一下。
1.首先游戏把层次布置好,这里分为 背景层,游戏层,UI层
背景层 (Background-1):就是单纯的背景显示作用。
游戏层 (Background-2): 主角和障碍物。
UI层 (Canvas):存放UI相关的东西。

背景层 :这里可以随便布置一些背景,就算不布置也无所谓,我这里就随便找了几个图布置了一下。
不过这里需要注意的就是背景层和游戏层的层次关系一定要分好,因为后面的射线需要去区分。

游戏层:游戏层的主要就是障碍物和主角,障碍物分很多,不一定就是单纯的阻碍寻找的,这里指的障碍物是所以能够碰撞产生效果的。
先讲解主角:主角要添加一个Animator,用来控制主角的动画状态。

然后给主角添加一些组件。

主角的运动脚本:
NarutoMove.cs
using UnityEngine;
using System.Collections; public class NarutoMove : MonoBehaviour {
private float moveSpeed;
private bool grounded = false;
public Transform Line_floor;
private bool jump = false; //角色的跳起
private float jumpy = 200f;
private Animator Nurutoanimator;
public AudioClip JumpMusic;//跳跃音频
private AudioSource audio;
// Use this for initialization
void Start () {
Nurutoanimator = this.GetComponent<Animator>();
audio = Camera.main.GetComponent<AudioSource>();
} // Update is called once per frame
void Update () {
float inputx = Input.GetAxis("Horizontal"); //获得水平移动
float inputy = Input.GetAxis("Vertical"); //获得垂直移动 if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)))
{
//为反反向
if (inputx <)
{
this.transform.rotation = Quaternion.Euler(, -, );
moveSpeed = -2f;
}else {
this.transform.rotation = Quaternion.Euler(, , );
moveSpeed = 2f;
}
// this.transform.Translate(new Vector3(1, 0, 0)* moveSpeed * Time.deltaTime);
this.transform.Translate(inputx * moveSpeed * Time.deltaTime, inputy * moveSpeed * Time.deltaTime, );
//this.transform.Rotate(new Vector3(inputx * 180, 0, 0));
Nurutoanimator.SetBool("RunFlg", true);
}
else
{
Nurutoanimator.SetBool("RunFlg", false);
} Debug.DrawLine(transform.position, Line_floor.transform.position, Color.red, 1f);
//与地面接触为真,才能够进行跳跃
grounded = Physics2D.Linecast(transform.position, Line_floor.transform.position, << LayerMask.NameToLayer("Ground")); if (grounded && Input.GetKeyDown(KeyCode.J))
{
jump = true; //设置角色的起跳功能
if (jump == true)
{
Musiplay(audio,JumpMusic);
Nurutoanimator.SetBool("RunJumpFlg", true);
// AudioSource.PlayClipAtPoint(jumpclips, gameObject.transform.position); 角色跳跃音效
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(1f, jumpy)); //添加一个向上Y的力
//hero.AddForce(new Vector2(0f,1000f));
jump = false;
grounded = false;
}
}
else {
Nurutoanimator.SetBool("RunJumpFlg", false);
} }
//播放跳跃音乐
void Musiplay(AudioSource audio, AudioClip clip) {
audio.volume = 1f;
audio.PlayOneShot(clip);
audio.volume = 0.2f;
}
}
Health.cs主角的血量控制脚本
using UnityEngine;
using System.Collections;
using UnityEngine.UI; public class Health : MonoBehaviour { public int Hp;
private Slider slider;
private Text time;
private int HpCnt = ;
// Use this for initialization public float fireRate = 3f;//冷却时间
private float nextFire = 0.0f;
void Start () {
//this.GetComponent<text
slider = GameObject.FindGameObjectWithTag("Hp").GetComponent<Slider>();
time = GameObject.FindGameObjectWithTag("Time").GetComponent<Text>(); } // Update is called once per frame
void Update () {
time.text = Time.time.ToString(); CutHp();
}
//每隔一定的时间2s会减少2滴血
public void CutHp() {
//冷却时间
//最开始Time.time ==0 nextFire==3 所以3S过后,Time.time > nextFire
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Hp = Hp - ;
slider.value = Hp;
if (Hp <= )
{
Application.LoadLevel("GameOver");
Destroy(this.gameObject);
}
}
}
public void AddHP(int damageCount,int HPCount) { Hp += damageCount;
if(Hp >= HPCount){
Hp = HPCount;
}
slider.value = Hp;
if (Hp <=)
{
Destroy(this.gameObject);
}
} void OnCollisionEnter2D(Collision2D collision)
{
AddHealth foodHealth = collision.collider.gameObject.GetComponent<AddHealth>();
if (foodHealth != null)
{
AddHP(foodHealth.AddTime, HpCnt);
Destroy(foodHealth.gameObject);
} } }
游戏层中的障碍物:

障碍物也就具有个重力和碰撞器属性,用来阻止主角通过。
游戏层中的水果:
用来给主角加血液,创建一个空物体。然后添加一个脚本,给里面指定一些预置体。

FoodColltor.cs
using UnityEngine;
using System.Collections; public class FoodColltor : MonoBehaviour { public GameObject[] Foods;
private int FoodType;
private float fireRate = 5.0f;//冷却时间
private float nextFire = 0.0f;//定位时间基准
private Transform Playertransform;
private float deathTime = 6.0f;
// Use this for initialization
void Start () {
//得到玩家的坐标
Playertransform = GameObject.FindGameObjectWithTag("Player").transform;
} // Update is called once per frame
void Update () {
//在一定的时间生成水果,和在一定的时间删除水果
FoodTimeColltor();
}
//每隔一定的时间,3个水果
void FoodTimeColltor() {
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
RandomCreateFood();
}
}
//随机生成水果
public void RandomCreateFood(int FoodNum) { for (int i = ; i < FoodNum; i++)
{
FoodType = Random.Range(, Foods.Length);
Vector2 foodPosition = new Vector2(Playertransform.position.x + Random.Range(, ), transform.position.y + Random.Range(,0.8f));
GameObject food = GameObject.Instantiate(Foods[FoodType], foodPosition, Quaternion.Euler(new Vector3(, , ))) as GameObject;
food.transform.parent = this.transform; //设定水果的父游戏对象
this.FooddDeath(food);
} } //水果的死亡,到一定的时间后对自动死亡
public void FooddDeath(GameObject food) {
Destroy(food, deathTime);
} }
最后给相机加一个跟随主角运动脚本。

CameraMove.cs
using UnityEngine;
using System.Collections; public class CameraMove : MonoBehaviour { private Transform Playertransform;
// Use this for initialization
void Start () {
//获得游戏玩家对象
Playertransform = GameObject.FindGameObjectWithTag("Player").transform;
} // Update is called once per frame
void LateUpdate () {
//将游戏对象的坐标赋给相机的坐标
//transform.position = new Vector3(Playertransform.position.x, -1.0f, -3.5f); 会稍微出现卡顿的现象
// transform.position = Playertransform.position
transform.position = new Vector2(Playertransform.position.x+2f, -0.1f);
}
}
游戏:http://pan.baidu.com/s/1hsIxUug
源码:
U3D 2D游戏之黑暗纪元 2D游戏基础入门开发全(1)的更多相关文章
- Unity3D 之3D游戏SD快打 3D游戏基础入门开发全(1)
这里记录一个U3D游戏,3D游戏的基本开发. 导入素材 1.首先导入需要的素材.因为FBX格式的素材是通用的,所以尽量导入这样的资源使用 导入后的结果: 然后对人形骨骼进行设置. 看哪里没有映射到骨骼 ...
- JS写小游戏(一):游戏框架
前言 前一阵发现一个不错的网站,都是一些用html5+css+js写的小游戏,于是打算学习一番,写下这个系列博客主要是为了加深理解,当然也有一些个人感悟,如果英文好可以直接Click Here. 概述 ...
- 使用UIKit制作卡牌游戏(一)ios游戏篇
转自朋友Tommy 的翻译,自己只翻译了第三篇教程. 译者: Tommy | 原文作者: Matthijs Hollemans写于2012/06/29 原文地址: http://www.raywend ...
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程:简介及目录》(附上完整工程文件)
介绍:讲述如何使用Genesis-3D来制作一个横版格斗游戏,涉及如何制作连招系统,如何使用包围盒实现碰撞检测,软键盘的制作,场景切换,技能读表,简单怪物AI等等,并为您提供这个框架的全套资源,源码以 ...
- 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇03:暂停游戏》
3.暂停游戏 暂停游戏概述: 在游戏进行时,玩家有可能会遇到多种突发事件.在跑酷游戏中突发状况的发生对游戏的影响更甚,游戏进行时玩家死亡,游戏只能从头开始,那么如果因为外界因素而影响游戏的进行,显然是 ...
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程07:UI》
概述: UI即User Interface(用户界面)的简称.UI设计是指对软件的燃机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅可以让游戏变得更有品位,更吸引玩家,还能充分体现开发者对游戏整 ...
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程03:碰撞检测》
3.碰撞检测 碰撞检测的概述: 碰撞在物理学中表现为两粒子或物体间极端的相互作用.而在游戏世界中,游戏对象在游戏世界自身并不受物理左右,为了模拟真实世界的效果,需要开发者为其添加属性,以模拟真实事件的 ...
- 【用PS3手柄在安卓设备上玩游戏系列】谈安卓游戏对手柄的支持
不同的游戏对于手柄的支持程度是不一样的,对应所需要进行的手柄设置也不尽相同.我没有这样的时间和精力,针对每一款游戏去写博客,但找出不同游戏中的共同点,针对同一类的游戏去写博客,应该是可行的.我把安卓上 ...
- 斗牛app上架应用宝、牛牛手机游戏推广、百人牛牛app应用开发、棋牌游戏上传、手游APP优化
联系QQ:305-710439斗牛app上架应用宝.牛牛手机游戏推广.百人牛牛app应用开发.棋牌游戏上传.手游APP优化 iOS开发iPhone/iPad平台安卓手机软件开发机型覆盖范围 超过113 ...
随机推荐
- 浅谈 HTML5 的 DOM Storage 机制 (转)
在开发 Web 应用时,开发者有时需要在本地存储数据.当前浏览器支持 cookie 存储,但其大小有 4KB 的限制.这对于一些 Ajax 应用来说是不够的.更多的存储空间需要浏览器本身或是插件的支持 ...
- Android学习之路——简易版微信为例(一)
这是“Android学习之路”系列文章的开篇,可能会让大家有些失望——这篇文章中我们不介绍简易版微信的实现(不过不是标题党哦,我会在后续博文中一步步实现这个应用程序的).这里主要是和广大园友们聊聊一个 ...
- ArcGIS for Android 中MapView的地图背景设置
转自:http://blog.csdn.net/wozaifeiyang0/article/details/7535704 根据多方面测速,终于解决了一个蛋疼的问题,MapView的背景设置问题. 在 ...
- android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)
Android实现上拉加载更多功能以及下拉刷新功能, 采用了目前比较火的PullToRefresh,他是目前实现比较好的下拉刷新的类库. 目前他支持的控件有:ListView, ExpandableL ...
- 用FSM写Case,玩过没?
一.引言 测试工程师小新一是一名安卓客户端测试工程师,对于安卓客户端的功能测试.自动化测试和性能测试方面都有着非常丰富的经验.最近小新一被通知负责某二手交易APP的功能测试,在初步了解了该APP后,小 ...
- Nhibernate Icreteria 分页查询
1.创建查询条件,条件为一个ICreterion的列表 /// /// 创建Criteria(不含order,因为获取总数的时候,为了性能考虑,不加order) /// ...
- HDU 1117 免费馅饼 二维动态规划
思路:a[i][j]表示j秒在i位置的数目,dp[i][j]表示j秒在i位置最大可以收到的数目. 转移方程:d[i][j]=max(dp[i-1][j],dp[i-1][j-1],dp[i-1][j+ ...
- linux多线程大神博客网址
http://blog.csdn.net/zjf280441589/article/details/43883055
- openStack 使用public key登陆
- 大型网站应用中MySQL的架构演变史
没有什么东西是一成不变的,包含我们的理想和生活!MySQL作为一个免费的开源的关系型数据库,深受大家喜爱,从最初的无人问津到当下的去IOE,都体现出了MySQL举足轻重的作用.今天我们就从淘宝的发展来 ...