Unity经典游戏教程之:是男人就下100层

版权声明:

  • 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客"(微信号:unitymaker)
  • 您可以自由转载,但必须加入完整的版权声明!

是男人就下一百层

一、游戏介绍

是男人就下100层这是一个很受欢迎的小游戏。设计者所取的游戏名称以及它特有的挑战性令几乎所有的男同胞们都一如既往的去暴机,这个游戏同样也吸引了广大的女性,相当有挑战性。

  • 中文名 是男人就下100层
  • 原版名称 man down
  • 游戏类型 休闲游戏

使用方向键左右控制小人,要防止被托上去挤死,防止掉下去摔死以及防止踩到陷阱。

二、场景搭建

  • 导入游戏资源,确保Sprites文件夹里的资源的Texture Type为Sprite

1

  • 打开Sprite Mode的Sprite Editor,对图片进行切割,点击Apply进行保存。

image

image

  • 将云彩(cloud、darkcloud)、背景图(game background)拖进场景,其中背景图为背景(Background),其他作为前景(Foreground)。

image

image

image

三、主角设置

1.拖入主角

将主角拖入场景之中并将其命名为Player,将主角Player设置在Foreground前景层。 image

2.主角动画

打开动画控制器,创建主角的动画walk动画和Idel动画。把主角图片拖到时间轴上其中walk动画每0.01秒一张。Idel动画只需要拖一张。 image

image

3.主角移动的实现

  • 给主角添加刚体(Rigidbody 2D)、添加碰撞器(box Collider 2D)

image

  • 给主角添加脚本Player。给角色设置一个最大速度maxSpeed。设置一个力量force。保存Rigidbody 2D 通过Horizontal取得水平上的按键0=什么也没按。1=右键。2=左键.

    1public float MaxSpeed = 4; 2public float Force = 4; 3public Rigidbody2D mybody; 4 5 6 7 void PlayerMove() 8 { 9 var x = Input.GetAxis("Horizontal"); 10 11 var xforce = 0.0f; 12 13 if(x > 0) 14 { 15 xforce = Force*x; 16 myAnimator.SetBool("walk", true); 17 this.gameObject.transform.localScale = new Vector3(1, 1, 1); 18 } 19 else if(x < 0) 20 { 21 xforce = Force*x; 22 myAnimator.SetBool("walk", true); 23 this.gameObject.transform.localScale = new Vector3(-1, 1, 1); 24 } 25 else 26 { 27 xforce = 0; 28 myAnimator.SetBool("walk", false); 29 } 30 31 var xspeed = mybody.velocity.x; 32 if(Mathf.Abs(xspeed) >= MaxSpeed) { 33 if(xforce * xspeed > 0) { 34 xforce = 0; 35 } 36 } 37 38 mybody.AddForce(new Vector2(xforce, 0)); 39 }

四、云彩设置

1.将云彩拖入场景

将云彩拖入场景之中并将其命名为cloud和darkcloud将云彩cloud和darkcloud设置在Background背景层。 image

2.设置云彩的生成

给云彩添加碰撞体Box Collider 2D,添加脚本CloudSpawn,建立cloud预制体和主角预制体player_0。将预制体Cloud和player_0拖入,Cloud 、player中。确保min x、max x、Distance Y、LastCloud Y的值。

image

1 void CreateClouds() 2 { 3 var worldSize = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0)); 4 5 var worldSize2 = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0)); 6 7 minX = worldSize2.x + 1f; 8 maxX = worldSize.x - 1f; 9 10 lastCloudY = 0; 11 for (var i = 0; i < clouds.Count; i++) 12 { 13 var cloud = clouds [i]; 14 Vector3 pos; 15 float x = 0; 16 if (controlCloud == 0) 17 { 18 x = Random.Range(0, maxX); 19 controlCloud = 1; 20 } 21 else if (controlCloud == 1) 22 { 23 x = Random.Range(minX, 0); 24 controlCloud = 2; 25 } 26 else if (controlCloud == 2) 27 { 28 x = Random.Range(1, maxX); 29 controlCloud = 3; 30 } 31 else if (controlCloud == 3) 32 { 33 x = Random.Range(minX, -1); 34 controlCloud = 0; 35 } 36 pos = new Vector3(x, lastCloudY, 0); 37 cloud.transform.localPosition = pos; 38 39 lastCloudY -= distanceY; 40 41 } 42 43 }

3.设置云彩的特殊功能

设置游戏角色player碰到darkCloud就会死亡

image

五、设置背景的滚动

游戏背景需要不断滚动,先把游戏背景图片再复制两份再把复制的背景拼接好注意不要留缝隙,再给图片添加碰撞体(Box Collider 2D)和脚本BG Scaler

image

image

1void Start () { 2 SpriteRenderer sp = this.gameObject.GetComponent<SpriteRenderer>(); 3 float width = sp.bounds.size.x; 4 5 6 float swidth = Screen.width; 7 float sheight = Screen.height; 8 Debug.Log(swidth+" : "+sheight); 9 10 float sz = Camera.main.orthographicSize; 11 float sc = sheight/swidth; 12 13 14 float cameraHeight = sz*2; 15 float cameraWidth = cameraHeight/(sc); 16 17 float spriteScale = cameraWidth/width; 18 this.transform.localScale = new Vector3(spriteScale, 2, 0); 19 }

五、角色死亡

设置游戏角色player碰到darkcloud就死亡和碰到游戏上下bound死亡。

1 if (other.gameObject.tag == "darkCloud") 2 { 3 4 var cs = Camera.main.GetComponent<CameraScript> (); 5 cs.enabled = false; 6 transform.localPosition = new Vector3 (1000, 1000, 0); 7 //GameController.Instance.ShowGameOver (score, CoinCount); 8 9 AudioSource.PlayClipAtPoint(dealSound, transform.position); 10 11 LifeCount--; 12 13 GameManager.Instance.CheckGameState (LifeCount, CoinCount, score); 14 } 15 16 else if (other.gameObject.tag == "bound") 17 { 18 var cs = Camera.main.GetComponent<CameraScript>(); 19 cs.enabled = false; 20 transform.localPosition = new Vector3(1000, 1000, 0); 21 //GameController.Instance.ShowGameOver(score, CoinCount); 22 23 AudioSource.PlayClipAtPoint(dealSound, transform.position); 24 25 LifeCount--; 26 27 GameManager.Instance.CheckGameState(LifeCount, CoinCount, score); 28 }

六、背景音乐

先新建一个空节点命名为Music,在文件夹中找到音乐天空之城将其拉入Music选择循环播放(Loop),适当选择音量(Volime)。

image

image

七、扩展方向

  • 增加血量扣血
  • 增加道具吃道具加分
  • 添加敌人
  • 当层数越来越多时候速度越来越快
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

00:Java简单了解

浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。