当前位置: 首页 > news >正文

Unity3d开发google chrome的dinosaur游戏

游戏效果

游戏中:

游戏中止:

一、制作参考

如何制作游戏?【15分钟】教会你制作Unity小恐龙游戏!新手15分钟内马上学会!_ unity教学 _ 制作游戏 _ 游戏开发_哔哩哔哩_bilibili

二、图片资源

https://download.csdn.net/download/benben044/89522911?spm=1001.2014.3001.5501

 三、创建场景

1、将资源都拖到Assets目录下

2、调整编辑布局

将Scene、Hierarchy、Game这3个模块分开,方便可以同时观察到三个模块的信息。

同时将Main Camera的color设置为白色,方便看到ground的颜色。

3、放dinosaur到scene

将assets中的run-2放到Scene中,就可以在Game中看到dinosaur的图案,同时将恐龙rename为Dinosaur。

 4、添加cloud到scene

同时给cloud进行重命名。

5、给dinosaur和ground添加组件

(1)给dinosaur添加Rigidbody 2d组件,使其具有物理属性,设置Gravity Scale为2,重力大一点下降速度会更快一点。

(2)再给dinosaur添加Box Collider 2d组件,使其具有碰撞属性

(3)接着给ground添加Box Collider 2d组件,使其具有碰撞属性

通过以上操作,dinosaur因为有物理属性所以会自然掉落,但是因为dinosaur和ground都有碰撞属性,所以dinosaur掉到地面后就停止掉落了。

在测试中如果发现Dinosaur掉到地面后脚没有落地,可以编辑Box Collider 2D中的Edit Collider,调整碰撞的范围。

四、编辑C#脚本

1、创建恐龙脚本

public class Dinosaur : MonoBehaviour
{Rigidbody2D rb;bool isJumping;public float jump;  // 当为public时即可在Inspector面板看到属性信息// Start is called before the first frame updatevoid Start(){rb = GetComponent<Rigidbody2D>();isJumping = false;}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Space) && isJumping == false) { rb.velocity = new Vector2(0, jump);   isJumping = true;}}private void OnCollisionEnter2D(Collision2D collision){// 只有碰到底才能跳,否则在天空中就飞起来了isJumping = false;  }
}

在Dinosaur的Dinosaur组件中设置jump为8。

2、创建恐龙动画

首先,在Assets下创建Animations的目录。

在Window->Animation下创建动画组件,然后点击Scene下的Dinosaur,最后点击Create,在Assets下的Animations下创建DinosaurAnimation.anim文件,如下图所示:

然后将dinosaur_assets下的run-1和run-2拖入animation编辑器,第1个点是run-1,第2个点是run-2,第3个点是run-1,循环的动作。

3、创建Movement脚本

在Assets下创建Movement的脚本,该脚本的功能是:当角色在run时,天上的云、地面都在向左移动。即,角色不动,参照物动,从而造成角色向右移动的假象。

public class Movement : MonoBehaviour
{public float movementSpeed;public float startPosition;public float endPosition;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.position = new Vector2(transform.position.x - movementSpeed * Time.deltaTime, transform.position.y);// 到dinosaur快要越界掉下去时,从开始处重新开始runif (transform.position.x <= endPosition){transform.position = new Vector2(startPosition, transform.position.y);}}
}

给ground,所有的cloud都加载该脚本,并且设置3个变量分别为5,5,-15。

4、加入仙人掌

将仙人掌放到Scene中,并且添加Box Collider 2D的脚本,这样恐龙就不能穿透它了。同时加入Movement的脚本,参数设置也是5,5,-15。

5、创建GameManager

在Scene中Create Empty,名为GameManager。

然后在Assets下创建GameManager的脚本,并把该脚本赋予GameManager的物体上。

脚本内容如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : MonoBehaviour
{public GameObject cactus;  // 仙人掌public GameObject cactusSpawnPostition; //仙人掌出现的位置public float spawnTime; // 仙人掌出现的时间float timer; // 计时器// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){timer += Time.deltaTime;if (timer >= spawnTime){Instantiate(cactus, cactusSpawnPostition.transform);timer = 0;}}
}

意思是:当超过spawnTime时,就让cactus在cactusSpawnPosition出现。 

6、创建cactusSpawnPosition

这个就是上个脚本中仙人掌每次自动生成的位置。

修改该对象的tag为红色菱形,这样在scene中更容易被辨认出来。

然后在scene中确定位置,如下图所示:

接着拖动hierachy中的cactus到Assets成为prefab预制体,修改器transform中的的Position全是0。

然后,设置GameManager的参数如下:

7、给仙人掌添加tag

点击Asset中的cactus_single,然后在其Tag旁边的Untagged点击一下,选择Add Tag如下图:

添加Cacuts的tag如下:

在Tag栏位选择刚刚创建的Cactus,如下:

8、删除不用的仙人掌

如果仙人掌已经在movement中endPosition的左边,就可以destory它。

所以,如果越界后,是仙人掌就destroy它,否则类似ground、cloud就循环从头开始。

所以优化Movement脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Movement : MonoBehaviour
{public float movementSpeed;public float startPosition;public float endPosition;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.position = new Vector2(transform.position.x - movementSpeed * Time.deltaTime, transform.position.y);// 到dinosaur快要越界掉下去时,从开始处重新开始runif (transform.position.x <= endPosition){if(gameObject.tag == "Cactus"){Destroy(gameObject);}else{transform.position = new Vector2(startPosition, transform.position.y);}}}
}

9、创建GameOver和Restart

(1)GameOver

在Hierarchy中选择UI -> Legacy -> Text如下:

编写文字:GAME OVER

并且选择好FFT格式的字体。

(2)Restart

在Cavas下再创建Button,位于UI -> Legacy -> Button,重命名为RestartButton。

然后将Button的图标替换为已有的素材,在调整下size。

 (3)创建Panel

将上面创建的2个UI组件都装到该Panel下,同时调整Panel->Color->A值为0。

这样,同时控制Panel的Active状态,就可以控制Panel的出现和消失。

只有当触发GameOver时该Panel才会出现,否则该Panel的Active为false。

10、 保存场景

在File->Save As中,在Assets->Scenes下保存场景为lv1,level1的意思。

11、在GameManager中加入GameOver和Restart

  • 创建GameOverScene变量,对应GameOver的Panel
  • Start()中加入控制速度的Time.scaleTime=1
  • 在多个函数中设置GameOver的Panel的Active为false
  • 在GameOver()中,设置Time.scaleTime=0,同时出现GameOver的Panel
  • 在Restart()中,重新加载lv1的scene,同时GameOver的Panel的Active为false
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class GameManager : MonoBehaviour
{public GameObject cactus;  // 仙人掌public GameObject cactusSpawnPostition; //仙人掌出现的位置public float spawnTime; // 仙人掌出现的时间float timer; // 计时器public GameObject GameOverScene;// Start is called before the first frame updatevoid Start(){Time.timeScale = 1.0f;GameOverScene.SetActive(false);}// Update is called once per framevoid Update(){timer += Time.deltaTime;if (timer >= spawnTime){Instantiate(cactus, cactusSpawnPostition.transform);timer = 0;}}public void GameOver(){Time.timeScale = 0; // 将游戏暂停GameOverScene.SetActive(true);}public void Restart(){SceneManager.LoadScene("lv1");GameOverScene.SetActive(false);}
}

编写完代码后,在Inspector中配置GameOverScene的值。

12、在Dinosaur脚本中加入游戏暂停的触发条件

主要是在OnCollisionEnter2D中加入触发条件。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Dinosaur : MonoBehaviour
{Rigidbody2D rb;bool isJumping;public float jump;  // 当为public时即可在Inspector面板看到属性信息public GameManager gm;// Start is called before the first frame updatevoid Start(){rb = GetComponent<Rigidbody2D>();isJumping = false;}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Space) && isJumping == false) { rb.velocity = new Vector2(0, jump);   isJumping = true;}}private void OnCollisionEnter2D(Collision2D collision){// 只有碰到底才能跳,否则在天空中就飞起来了isJumping = false;  if(collision.gameObject.tag == "Cactus"){// 撞到仙人掌,游戏暂停gm.GameOver();}}
}

编写完代码后,配置GameManager的变量。

至此,该游戏制作完成!

五、总结

1、检测碰撞:OnCollisionEnter2D,这个是和Update平级的函数

2、物体移动时,transform.position = new Vector2(x, y),其中x是transform.position.x - Time.deltaTime * speed.

3、时间是float类型的数据

4、实例化预设体用Instantiate函数,参数1是实例化的对象,参数2是transform信息,可以用某个物体的transform信息作为参数2

5、检测碰撞的物体时,可以给对方物体一个tag,然后通过collision.gameobject.tag == <tag>判断和该物体是否相撞

6、加载场景使用SceneManager.LoadScene("<场景名称>")

7、如果有多个UI需要一起控制,则统一放到Panel中,然后控制该Panel即可

8、控制暂停、重启,可使用时间变量Time.timeScale

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 数据可视化入门
  • 【CPO-TCN-BiGRU-Attention回归预测】基于冠豪猪算法CPO优化时间卷积双向门控循环单元融合注意力机制
  • https 单向认证和双向认证
  • Postfix+Dovecot+Roundcube开源邮件系统搭建系列1-2:系统搭建目标+MariaDB数据库配置(MySQL)
  • 网络规划设计师教程(第二版) pdf
  • 数据库系统概论:数据库完整性
  • Pytorch:显卡驱动版本、Pytorch版本的关系
  • vue中:class、watch、v-show使用
  • GEO数据挖掘从数据下载处理质控到差异分析全流程分析步骤指南
  • 小程序中用于跳转页面的5个api是什么和区别
  • 跨域的解决方案
  • SpringBoot集成MQTT实现交互服务通信
  • python 迭代器介绍 map() 函数
  • stm32入门-----EXTI外部中断(上 ——理论篇)
  • TDesign组件库日常应用的一些注意事项
  • 【RocksDB】TransactionDB源码分析
  • 2017前端实习生面试总结
  • css的样式优先级
  • download使用浅析
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • Nodejs和JavaWeb协助开发
  • PHP 小技巧
  • SpringCloud集成分布式事务LCN (一)
  • sublime配置文件
  • ViewService——一种保证客户端与服务端同步的方法
  • Vue.js源码(2):初探List Rendering
  • vue-router 实现分析
  • 从0实现一个tiny react(三)生命周期
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 买一台 iPhone X,还是创建一家未来的独角兽?
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 删除表内多余的重复数据
  • zabbix3.2监控linux磁盘IO
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ###项目技术发展史
  • #07【面试问题整理】嵌入式软件工程师
  • #includecmath
  • #微信小程序:微信小程序常见的配置传值
  • (11)MATLAB PCA+SVM 人脸识别
  • (12)Hive调优——count distinct去重优化
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (3) cmake编译多个cpp文件
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第2节(共同的基类)
  • (pojstep1.1.2)2654(直叙式模拟)
  • (rabbitmq的高级特性)消息可靠性
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置
  • ***监测系统的构建(chkrootkit )
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑
  • .NET Core 2.1路线图
  • .NET Core使用NPOI导出复杂,美观的Excel详解
  • .NET Core中Emit的使用