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

Unity教学 项目1 2D赛车小游戏

视频链接:

https://www.bilibili.com/video/BV1wT9rYZEKe?spm_id_from=333.788.videopod.sections&vd_source=25b783f5f945c4507229e9dec657b5bb

本教程涉及到 Unity 常用组件、常用方法等核心知识点,掌握本教程相关知识后你就就可以快速掌握一些 Unity2D 常用组件了

1.需求分析

  1. 玩家通过点击屏幕上的向左、向右移动按钮控制红色小车左右移动避让黄色小车
  2. 黄色小车在屏幕最上方随机生成后向下移动
  3. 屏幕右上方分数跟随时间变化而变化
  4. 红色小车与某一辆黄色小车碰撞则游戏结束,弹出游戏结束界面
  5. 游戏结束界面上有本局游戏分数以及重新开始的按钮

2.代码实现

2.1 创建项目目录

  • Imags:静态图片

  • Prefabs:预设物体

  • Resources:动态资源

    • Audio:音频
  • Scenes:场景

  • Scripts:脚本

2.2 创建面板、小车、按钮等

2.3 按钮控制红色小车左右移动

创建游戏管理脚本 GameManager.cs

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassGameManager:MonoBehaviour{/// <summary>/// 游戏管理器实例/// </summary>publicstaticGameManagerinsta;/// <summary>/// 主界面/// </summary>publicMainPanelmainPanel;privatevoidAwake(){insta=this;}// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){}}

红色小车挂载脚本 RedCar.cs

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassRedCar:MonoBehaviour{/// <summary>/// 移动速度/// </summary>privateintmoveSpeed=100;/// <summary>/// 移动方向/// </summary>publicintmoveDirection=0;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){//屏幕范围内左右移动if(moveDirection==-1&&transform.localPosition.x<=-490)return;if(moveDirection==1&&transform.localPosition.x>=490)return;transform.localPosition+=newVector3(moveDirection*moveSpeed*Time.deltaTime,0,0);}/// <summary>/// 碰撞显示结束界面/// </summary>/// <param name="collision"></param>privatevoidOnTriggerEnter2D(Collider2Dcollision){GameManager.insta.overPanel.ShowPanel();}}

主界面挂载脚本 MainPanel.cs,拖拽相应物体

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.UI;publicclassMainPanel:MonoBehaviour{/// <summary>/// 红色小车物体/// </summary>publicRedCarredCar;/// <summary>/// 分数文本/// </summary>publicTextscoreText;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){}/// <summary>/// 点击按钮向左移动/// </summary>publicvoidOnLeftMoveClick(){redCar.moveDirection=-1;}/// <summary>/// 点击按钮向右移动/// </summary>publicvoidOnRightMoveClick(){redCar.moveDirection=1;}}

2.4 黄色小车自动向下移动

黄色小车挂载脚本 YellowCar.cs

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassYellowCar:MonoBehaviour{/// <summary>/// 移动速度/// </summary>privateintmoveSpeed=100;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){transform.localPosition-=newVector3(0,moveSpeed*Time.deltaTime,0);//向下移动if(transform.localPosition.y<=-1060)Destroy(gameObject);//如果移动到屏幕最底端则自动销毁}}

2.5 红色小车与黄色小车碰撞则游戏结束

红色小车挂载组件 Box Collider 2D 和 Rigidbody 2D

黄色小车挂载组件 Box Collider 2D

结束界面挂载脚本 OverPanel.cs

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.SceneManagement;usingUnityEngine.UI;publicclassOverPanel:MonoBehaviour{/// <summary>/// 分数文本/// </summary>publicTextscoreText;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){}/// <summary>/// 显示面板/// </summary>publicvoidShowPanel(){Time.timeScale=0f;//游戏暂停gameObject.SetActive(true);}/// <summary>/// 点击按钮重新开始游戏/// </summary>publicvoidOnRestartClick(){Time.timeScale=1f;//游戏恢复gameObject.SetActive(false);SceneManager.LoadScene(0);}}

GameManager.cs 新增结束界面变量

publicclassGameManager:MonoBehaviour{/// <summary>/// 游戏管理器实例/// </summary>publicstaticGameManagerinsta;/// <summary>/// 主界面/// </summary>publicMainPanelmainPanel;/// <summary>/// 结束界面/// </summary>publicOverPaneloverPanel;...

2.6 更新界面分数

主界面

...publicclassMainPanel:MonoBehaviour{/// <summary>/// 红色小车物体/// </summary>publicRedCarredCar;/// <summary>/// 分数文本/// </summary>publicTextscoreText;/// <summary>/// 分数数值/// </summary>publicintscore;/// <summary>/// 开始时间/// </summary>privatefloatstartTime;// Start is called before the first frame updatevoidStart(){startTime=Time.time;}// Update is called once per framevoidUpdate(){//更新分数score=(int)(Time.time-startTime);scoreText.text="分数:"+score;}...

结束界面

...publicclassOverPanel:MonoBehaviour{/// <summary>/// 分数文本/// </summary>publicTextscoreText;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){scoreText.text="分数:"+GameManager.insta.mainPanel.score;}...

2.7 通过预设随机生成黄色小车

创建黄色小车根目录

.../// <summary>/// 创建黄色小车上一次时间/// </summary>privatefloatlastTime;/// <summary>/// 黄色小车物体预设/// </summary>publicGameObjectpreYellowCarGo;/// <summary>/// 黄色小车根目录/// </summary>publicGameObjectyellowCarRootGo;// Start is called before the first frame updatevoidStart(){startTime=Time.time;lastTime=Time.time;}// Update is called once per framevoidUpdate(){//更新分数score=(int)(Time.time-startTime);scoreText.text="分数:"+score;//每过3秒生成一辆黄色小车if(Time.time-lastTime>=3f){CreateYellowCar();lastTime=Time.time;}}/// <summary>/// 点击按钮向左移动/// </summary>publicvoidOnLeftMoveClick(){redCar.moveDirection=-1;}/// <summary>/// 点击按钮向右移动/// </summary>publicvoidOnRightMoveClick(){redCar.moveDirection=1;}/// <summary>/// 创建黄色小车/// </summary>privatevoidCreateYellowCar(){//在x坐标为-490到490之间随机生成黄色小车GameObjectyellowCarGo=Instantiate(preYellowCarGo,yellowCarRootGo.transform);intrandomInt=Random.Range(-490,490);yellowCarGo.transform.localPosition=newVector3(randomInt,1060,0);}}

2.8 添加音频

创建游戏中音频物体

.../// <summary>/// 黄色小车根目录/// </summary>publicGameObjectyellowCarRootGo;/// <summary>/// 游戏进行中音频/// </summary>publicAudioSourcegameInAudioSource;// Start is called before the first frame updatevoidStart(){startTime=Time.time;// 开始时间赋值lastTime=Time.time;// 创建黄色小车上一次时间赋值gameInAudioSource.Play();//播放游戏进行音乐}...

创建游戏结束音频物体

.../// <summary>/// 游戏技术音频/// </summary>publicAudioSourcegameOverAudioSource;.../// <summary>/// 显示面板/// </summary>publicvoidShowPanel(){Time.timeScale=0f;//游戏暂停gameObject.SetActive(true);//停止游戏进行音频,播放游戏结束音频if(GameManager.insta.mainPanel.gameInAudioSource.isPlaying){GameManager.insta.mainPanel.gameInAudioSource.Stop();}gameOverAudioSource.Play();}...

2.9 物体换皮

3.完整代码

GameManager

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassGameManager:MonoBehaviour{/// <summary>/// 游戏管理器实例/// </summary>publicstaticGameManagerinsta;/// <summary>/// 主界面/// </summary>publicMainPanelmainPanel;/// <summary>/// 结束界面/// </summary>publicOverPaneloverPanel;privatevoidAwake(){insta=this;}// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){}}

MainPanel

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.UI;publicclassMainPanel:MonoBehaviour{/// <summary>/// 红色小车物体/// </summary>publicRedCarredCar;/// <summary>/// 分数文本/// </summary>publicTextscoreText;/// <summary>/// 分数数值/// </summary>publicintscore;/// <summary>/// 开始时间/// </summary>privatefloatstartTime;/// <summary>/// 创建黄色小车上一次时间/// </summary>privatefloatlastTime;/// <summary>/// 黄色小车物体预设/// </summary>publicGameObjectpreYellowCarGo;/// <summary>/// 黄色小车根目录/// </summary>publicGameObjectyellowCarRootGo;/// <summary>/// 游戏进行中音频/// </summary>publicAudioSourcegameInAudioSource;// Start is called before the first frame updatevoidStart(){startTime=Time.time;// 开始时间赋值lastTime=Time.time;// 创建黄色小车上一次时间赋值gameInAudioSource.Play();//播放游戏进行音乐}// Update is called once per framevoidUpdate(){//更新分数score=(int)(Time.time-startTime);scoreText.text="分数:"+score;//每过3秒生成一辆黄色小车if(Time.time-lastTime>=3f){CreateYellowCar();lastTime=Time.time;}}/// <summary>/// 点击按钮向左移动/// </summary>publicvoidOnLeftMoveClick(){redCar.moveDirection=-1;}/// <summary>/// 点击按钮向右移动/// </summary>publicvoidOnRightMoveClick(){redCar.moveDirection=1;}/// <summary>/// 创建黄色小车/// </summary>privatevoidCreateYellowCar(){//在x坐标为-490到490之间随机生成黄色小车GameObjectyellowCarGo=Instantiate(preYellowCarGo,yellowCarRootGo.transform);intrandomInt=Random.Range(-490,490);yellowCarGo.transform.localPosition=newVector3(randomInt,1060,0);}}

OverPanel

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.SceneManagement;usingUnityEngine.UI;publicclassOverPanel:MonoBehaviour{/// <summary>/// 分数文本/// </summary>publicTextscoreText;/// <summary>/// 游戏技术音频/// </summary>publicAudioSourcegameOverAudioSource;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){scoreText.text="分数:"+GameManager.insta.mainPanel.score;}/// <summary>/// 显示面板/// </summary>publicvoidShowPanel(){Time.timeScale=0f;//游戏暂停gameObject.SetActive(true);//停止游戏进行音频,播放游戏结束音频if(GameManager.insta.mainPanel.gameInAudioSource.isPlaying){GameManager.insta.mainPanel.gameInAudioSource.Stop();}gameOverAudioSource.Play();}/// <summary>/// 点击按钮重新开始游戏/// </summary>publicvoidOnRestartClick(){Time.timeScale=1f;//游戏恢复gameObject.SetActive(false);SceneManager.LoadScene(0);}}

RedCar

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassRedCar:MonoBehaviour{/// <summary>/// 移动速度/// </summary>privateintmoveSpeed=100;/// <summary>/// 移动方向/// </summary>publicintmoveDirection=0;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){//屏幕范围内左右移动if(moveDirection==-1&&transform.localPosition.x<=-490)return;if(moveDirection==1&&transform.localPosition.x>=490)return;transform.localPosition+=newVector3(moveDirection*moveSpeed*Time.deltaTime,0,0);}/// <summary>/// 碰撞显示结束界面/// </summary>/// <param name="collision"></param>privatevoidOnTriggerEnter2D(Collider2Dcollision){GameManager.insta.overPanel.ShowPanel();}}

YellowCar

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassYellowCar:MonoBehaviour{/// <summary>/// 移动速度/// </summary>privateintmoveSpeed=100;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){transform.localPosition-=newVector3(0,moveSpeed*Time.deltaTime,0);//向下移动if(transform.localPosition.y<=-1060)Destroy(gameObject);//如果移动到屏幕最底端则自动销毁}}
http://www.cnnetsun.cn/news/49390.html

相关文章:

  • Neovim代码补全终极指南:极速配置与智能提示
  • 【Kubernetes】使用Helm简化k8s部署、管理
  • 零基础也能搭建企业官网:Halo开源建站工具实战指南
  • Open-SaaS邮件系统性能优化实战:构建高并发异步处理架构
  • 基于vue的考研信息共享平台_a5a399ip_springboot php python nodejs
  • ROAPI零代码API构建完整指南:从入门到实战
  • 基于vue的小明餐厅点餐平台的设计_9yzk5cgp_springboot php python nodejs
  • 35、掌握Bash脚本:提升Linux管理效率的秘诀
  • 软考 系统架构设计师系列知识点之面向服务架构设计理论与实践(13)
  • Proxy Audio Device:macOS虚拟音频驱动器的完整指南
  • 终极PHP调试解决方案:用symfony/debug实现高效错误处理
  • 智慧养老项目:当SpringBoot遇到硬件,如何优雅地处理异常与状态管理?
  • 5步轻松搞定AppSmith实时推送:告别消息延迟的终极指南
  • IOPaint终极指南:AI一键去除水印的完整解决方案
  • Windows更新后RDPWrap失效修复指南:快速恢复多用户远程桌面功能
  • GPU和TPU差异之联网
  • 解决 Oracle 监听外网 IP 及腾讯云防火墙配置
  • ORACLE解析游标生成JSON
  • AMD GPU并行通信技术:突破性性能优化实战指南
  • Everywhere AI助手:跨平台智能对话系统深度解析
  • 考古学开放数据中的Paradata研究——CAPTURE项目与文献综述解读
  • 论文解读|将1930年前所有阿拉伯期刊添加到Wikidata——学术众包项目Jarāʾid向数字公共领域的迁移
  • 5分钟掌握UpSetR:超越维恩图的集合交集可视化神器
  • 机构洗盘拼合指标绝无未来 源码分析
  • Android项目架构完整指南:模块化开发与Kotlin最佳实践
  • Horovod Process Sets:让千亿参数模型训练触手可及
  • 5步掌握Loco+Tauri:构建高性能跨平台桌面应用的终极指南
  • 如何在Zephyr RTOS中制定最佳编译策略?
  • 专业实验室改造,必须避开的5大坑
  • 千万注意!实验室装修这5个关键点不容忽视