2D横板跑酷游戏Boxman
一、游戏介绍:
二、游戏场景:
Start场景:
Game场景:
using UnityEngine;public class CamerSet : MonoBehaviour {public Camera camera;public GameObject player;public float cameraChangeSize = 5f;public float sizeChangeSpeed = 2f;public float maxSize = 10f;public float minSize = -5f;void Start () {}//保证摄像头跟随主人公且保持在一定范围内void Update () {cameraChangeSize = player.transform.position.y + 5f;cameraChangeSize = cameraChangeSize >= maxSize ? maxSize : cameraChangeSize;cameraChangeSize = cameraChangeSize <= minSize ? minSize : cameraChangeSize;camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, cameraChangeSize, Time.deltaTime * sizeChangeSpeed);}
}
Shy搭载脚本
ScrollBG:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ScrollBG : MonoBehaviour {public float scrollSpeed = 0.5f;private float targetOffset;void Start () {}// Update is called once per framevoid Update () {targetOffset += Time.deltaTime * scrollSpeed;//主纹理偏移 完成背景的自动滚动GetComponent<Renderer>().material.mainTextureOffset = new Vector2(targetOffset, 0);}
}
Cube_A、Cube_B:初始舞台
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerControll : MonoBehaviour {//枚举主人公状态public enum PlayerState {Run,Jump,DoubleJump,Die}public PlayerState playerState;public float jumpPower = 200f;public AudioClip[] Sound;public Animator animator;public GameManger gameManger;public bool isDie = false;void Start () {// Debug.Log(gameManger.coins);}// Update is called once per framevoid Update () {//检测主人公是否跳跃和双跳if (Input.GetKeyDown(KeyCode.Space)) {if (playerState == PlayerState.DoubleJump||playerState==PlayerState.Die) ;else if (playerState == PlayerState.Jump)DoubleJump();elseJump();}}void SoundPlayer(int Index) {GetComponent<AudioSource>().clip = Sound[Index];GetComponent<AudioSource>().Play();}private void OnTriggerEnter(Collider other) {switch (other.gameObject.name) {case "Coin":Destroy(other.gameObject);GetCion();break;case "DiezoneA":if (playerState != PlayerState.Die)GameOver();break;case "DiezoneB":if (playerState != PlayerState.Die)GameOver();break;}}void GetCion() {SoundPlayer(0);gameManger.GetCoins();} private void OnCollisionEnter(Collision collision) {if (playerState != PlayerState.Run&&playerState!=PlayerState.Die) Run();}void Run() {playerState = PlayerState.Run;animator.SetBool("isInGroud", true);}void Jump() {playerState = PlayerState.Jump;this.GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpPower, 0));SoundPlayer(1);animator.SetTrigger("Jump");animator.SetBool("isInGroud", false);}void DoubleJump() {playerState = PlayerState.DoubleJump;this.GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpPower, 0));SoundPlayer(1);animator.SetTrigger("DoubleJump");animator.SetBool("isInGroud", false);}void GameOver() {playerState = PlayerState.Die;isDie = true;SoundPlayer(2);}public string GetPlayerState (){return playerState.ToString();}
}
主人公动画控制器:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class BlockLoop : MonoBehaviour {public float speed = 3f;public GameObject aZone;public GameObject bZone;public GameObject[] blockGameObjects;//制作好的各种地形带金币void Start () {}// Update is called once per framevoid Update () {MoveBlock();}//舞台的转动void MoveBlock() {aZone.transform.Translate(Vector3.left * speed * Time.deltaTime);bZone.transform.Translate(Vector3.left * speed * Time.deltaTime);if (bZone.transform.position.x <= 0) {Destroy(aZone);aZone = bZone;MakeNewBlock();}}//创建新舞台void MakeNewBlock() {int blockIndex = Random.Range(0, blockGameObjects.Length);bZone = Instantiate(blockGameObjects[blockIndex], new Vector3(aZone.transform.position.x+30, -5, 0), transform.rotation) as GameObject;}
}
脚本GameManger :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class GameManger : MonoBehaviour {private float runSpeed = 10f;private float runMeter = 0f;private float coins = 0;private PlayerControll playerControll;private bool isPause;public Text coinText;public Text runText;public Text resultCoinText;public Text resultRunText;public Image resultImage; private void Awake() {//隐藏表示结果的uiresultImage.gameObject.SetActive(false);playerControll = GameObject.Find("Player").GetComponent<PlayerControll>();isPause = false;}void Update() {if (playerControll.isDie) {resultImage.gameObject.SetActive(true);return;}runMeter += Time.deltaTime * runSpeed;runText.text = "距离 " + (int)runMeter;resultRunText.text = "" + (int)runMeter;}public void GetCoins() {resultCoinText.text=coinText.text = ""+(++coins);}//replay加载到Start场景public void Replay() {Application.LoadLevel("start");}//游戏暂停的实现public void Pause() {if (Input.GetKeyDown(KeyCode.Space)) return;isPause = !isPause;if (isPause)Time.timeScale = 0;elseTime.timeScale = 1;}
}
UI设计:
三、运行截图:
四、源码:
点击打开链接
2D横板跑酷游戏Boxman相关推荐
- 2D横板动作游戏常用角色移动逻辑(转)
这里所说的横版动作游戏,包含且不限于2D横版动作游戏.横版跑酷游戏.横版格斗游戏,只要逻辑层是横板的,且对于判定需要及高精度的(比如在动作游戏中就是拳头命中身体才算中,而不是WoW中,虽然近战攻击有时 ...
- pygame做的著名游戏_用python写游戏之2D跑酷游戏(一)
2D的跑酷游戏有很多,著名的例如Chrome的彩蛋小游戏,手机上的天天酷跑等. 打开Chrome浏览器输入 chrome://dino/,按空格激活彩蛋 这篇文章来分析一下这类横版跑酷游戏的主角奔跑, ...
- python游戏彩蛋_用python写游戏之2D跑酷游戏(一)
2D的跑酷游戏有很多,著名的例如Chrome的彩蛋小游戏,手机上的天天酷跑等.打开Chrome浏览器输入 chrome://dino/,按空格激活彩蛋 这篇文章来分析一下这类横版跑酷游戏的主角奔跑,跳 ...
- 项目实训(九)——跑酷游戏金币的制作和拾取
一.前言 我与另外两个组员制作了项目的其中一个单机游戏,类型是2D横屏跑酷游戏.游戏中的角色可以在跳跃与奔跑的过程中收集金币,能够很好地考验和锻炼玩家的反应能力和观察能力.玩家坚持得越久.收集到的金币 ...
- 2021-9-4 爆肝一整天,关于2D横板游戏中攀爬楼梯的考虑
今日份关于2D横板游戏中攀爬楼梯的考虑 引言 关于攀爬楼梯考虑 总体考虑 玩家站在1处 玩家站在2处 玩家通过跳跃来到3处 1处升至3处 和 2处降至3处 3处升至2处 3处降至1处 其他考虑 解决方 ...
- 2D横板解谜类游戏开发日志-角色移动和动画
2D横板解谜类游戏开发日志-角色移动和动画 一.创建角色 1新建精灵 2拉入素材 3添加组件*2,方便后续使用 4创建动画文件夹,动画帧素材丢进去(方便以后管理 5创建代码文件,挂在player对象上 ...
- 用python写跑酷游戏脚本_用python写游戏之2D跑酷游戏(二)
这篇文章介绍2D跑酷游戏中,添加地板资源, 给主角附加重力作用,以及添加游戏控制器和游戏场景的切换. 废话不多说,直接开始. 添加地板资源 定义地板类,继承自pygame.sprite.Sprite ...
- 2021-9-2 Cinemachine 不需要写代码的高级跟随摄像机 2D横板游戏应用
Cinemachine在2D横版游戏中的使用方法简介 Cinemachine的下载及导入 Cinemachine基础 设置 简单用法的一些重要的属性(没讲到的暂时就保持默认设置,如果有补充,欢迎评论区 ...
- 小白零基础搞unity手机跑酷游戏-01-GettingStarted
小白零基础搞unity2D手机跑酷游戏-01-Getting Started 策划 键位 内容 准备工作 这是我第一次在CSDN上写博客,是想锻炼一下自己,也是对自己的一种激励吧,毕竟立了一个很大的f ...
最新文章
- 转:Android之 MTP框架和流程分析
- 深入浅出WPF开发下载
- vue refs v-for 使用注意
- python kill 进程,python 判断linux进程,并杀死进程的实现方法
- 【洛谷】【动态规划+单调队列】P1725 琪露诺
- 使用Excel调用ABAP系统的函数
- oracle唯一索引能删除吗,Oracle:ora-02429:无法用于删除强制唯一/主键的索引 解决...
- Linux-网络RAID技术DRBD
- 地理空间数据Geometry在MySQL中使用(二)
- 3.第一个HelloMaven/快速入门
- Emacs-224-彩虹猫的实现
- 医疗研讨会直播怎么做
- 提取多个文件夹图片到一个文件夹
- 微博上一些有用的话(六)
- 用styler更改样式后,解决数据框中数据无法保留四位小数
- combo接口(combo接口)
- SpringBoot项目没有@RunWith注解
- CF1000赛后总结
- html去除页面的滑动条
- 一次惨痛的线下机房上云的经历
热门文章
- Ubuntu 磁盘管理 命令行工具
- 视频画面有黑边怎么办?三招视频画面大小裁剪的方法教给你
- 【编程小技巧】实现弹窗、选项、关机(文件后缀改成.vbs)
- Linux基础(三):多重引导
- OllyDbg动态调试与逆向破解TraceMe.exe
- 小学计算机课的游戏橡皮小人,小学计算机科学课:两个女孩和男孩使用带增强现实软件的数码平板电脑,他们感到兴奋、充满惊奇、好奇。STEM 、游戏、学习中的儿童...
- 基于 springBoot 实现webSocket方式的扫码登录
- 安全漏洞中的倚天剑——XSS跨站脚本攻击
- Day1 - Python基础
- python批量建立文件夹