第一个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)的更多相关文章

  1. Unity3D 之3D游戏SD快打 3D游戏基础入门开发全(1)

    这里记录一个U3D游戏,3D游戏的基本开发. 导入素材 1.首先导入需要的素材.因为FBX格式的素材是通用的,所以尽量导入这样的资源使用 导入后的结果: 然后对人形骨骼进行设置. 看哪里没有映射到骨骼 ...

  2. JS写小游戏(一):游戏框架

    前言 前一阵发现一个不错的网站,都是一些用html5+css+js写的小游戏,于是打算学习一番,写下这个系列博客主要是为了加深理解,当然也有一些个人感悟,如果英文好可以直接Click Here. 概述 ...

  3. 使用UIKit制作卡牌游戏(一)ios游戏篇

    转自朋友Tommy 的翻译,自己只翻译了第三篇教程. 译者: Tommy | 原文作者: Matthijs Hollemans写于2012/06/29 原文地址: http://www.raywend ...

  4. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程:简介及目录》(附上完整工程文件)

    介绍:讲述如何使用Genesis-3D来制作一个横版格斗游戏,涉及如何制作连招系统,如何使用包围盒实现碰撞检测,软键盘的制作,场景切换,技能读表,简单怪物AI等等,并为您提供这个框架的全套资源,源码以 ...

  5. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇03:暂停游戏》

    3.暂停游戏 暂停游戏概述: 在游戏进行时,玩家有可能会遇到多种突发事件.在跑酷游戏中突发状况的发生对游戏的影响更甚,游戏进行时玩家死亡,游戏只能从头开始,那么如果因为外界因素而影响游戏的进行,显然是 ...

  6. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程07:UI》

    概述: UI即User Interface(用户界面)的简称.UI设计是指对软件的燃机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅可以让游戏变得更有品位,更吸引玩家,还能充分体现开发者对游戏整 ...

  7. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程03:碰撞检测》

    3.碰撞检测 碰撞检测的概述: 碰撞在物理学中表现为两粒子或物体间极端的相互作用.而在游戏世界中,游戏对象在游戏世界自身并不受物理左右,为了模拟真实世界的效果,需要开发者为其添加属性,以模拟真实事件的 ...

  8. 【用PS3手柄在安卓设备上玩游戏系列】谈安卓游戏对手柄的支持

    不同的游戏对于手柄的支持程度是不一样的,对应所需要进行的手柄设置也不尽相同.我没有这样的时间和精力,针对每一款游戏去写博客,但找出不同游戏中的共同点,针对同一类的游戏去写博客,应该是可行的.我把安卓上 ...

  9. 斗牛app上架应用宝、牛牛手机游戏推广、百人牛牛app应用开发、棋牌游戏上传、手游APP优化

    联系QQ:305-710439斗牛app上架应用宝.牛牛手机游戏推广.百人牛牛app应用开发.棋牌游戏上传.手游APP优化 iOS开发iPhone/iPad平台安卓手机软件开发机型覆盖范围 超过113 ...

随机推荐

  1. Django学习随想(1)

    关于Django的模型部分: 模型操作实际上都是针对数据库的一系列操作. Django封装了底层的操作,给用户提供了一组非常python化的模型对象.让python开发者可以很方便.直观地进行数据库表 ...

  2. [JCWC2005]Draw

    Einstein学起了画画,此人比较懒--,他希望用最少的笔画画出一张画...给定一个无向图,包含 n 个顶点(编号1~n),m 条边,求最少用多少笔可以画出图中所有的边 Input (draw.in ...

  3. oracle 中的truncate 和delete

    一.查询表大小,块多少语句 Select SEGMENT_Name,BYTES,BLOCKS,Extents From dba_segments Where segment_name In('BAI_ ...

  4. sqlplus中常用设置参数

    一.各种设置参数解释 转自http://baike.baidu.com/view/1239908.htm Sql*plus是一个最常用的工具,具有很强的功能,主要有: 1. 数据库的维护,如启动,关闭 ...

  5. jsp网站与discuz论坛用户同步

    需求分析: 要想实现A(jsp网站)和B(discuz论坛)的同步,这里说的同步指的是 在AB网站任意一方注册之后在另一方都可以直接登录 AB两网站之间的用户登陆状态是同步的,在任意一方登录后,另一方 ...

  6. Amabri安装前的准备工作

    Ambari这个东东对操作系统的要求还是很高的,首先限制了要使用CentOS,RHEL,并且还是64bit 的,让我的Ubuntu泪奔啊---手头正好有RHEL6.x的盘,于是就用红帽吧.但是红帽是收 ...

  7. ios iphone 将log在终端输出

    对于模拟器,其在终端的log文件位于:   -/Library/Logs/CoreSimulator/C4B94BA6-EF08-4AD2-AE7D-1A3A2E2AC545/system.log 对 ...

  8. autoSvn

    #!/bin/bash    dir="/svndata" name="puppet" user="test" passwd="t ...

  9. 《算法:C语言实现》阅读笔记

    //从今天起准备认真看完这本书.本渣虽然笨,但是窝懒啊.... //今天开始看第一章.希望坚持下去. 第一章 引言 通过讨论连通问题的几种算法,来引出算法的重要性. 1.1 连通问题的快速查找算法 感 ...

  10. Sublime Text 2 使用心得

    一. 前言 作为一个前端,有一款好的开发利器是必不可少的,editplus.notepad++都是不错的工具,体积轻巧,启动迅速(dw太浮肿了).最近,又有一款新的编辑器诞生,席卷前端界,惹得无数喜爱 ...