Unity3d实现Flappy Bird游戏
作者:qq402335257 发布时间:2023-09-17 20:54:29
标签:Unity,Flappy,Bird
本文实例为大家分享了Unity3d实现Flappy Bird的具体代码,供大家参考,具体内容如下
一、小鸟
在游戏中,小鸟并不做水平位移,而是通过障碍物的移动让小鸟有水平运动的感觉,小鸟只需要对鼠标的点击调整竖直加速度就可以了,同时加上水平旋转模仿原版的FlappyBird的运动。同时,还要对竖直位置进行判断,否则游戏不能正常结束。
这里贴上小鸟上附加的脚本代码
Player.cs
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private Rigidbody body;
public Vector3 jumpForce = new Vector3(0, 300, 0);
private bool state = true; //确保只执行一次
private int bestScore = 0;
// Use this for initialization
void Start () {
body = transform.GetComponent<Rigidbody>();
}
void OnCollisionEnter(Collision collisionInfo)
{
if (state)
{
//碰撞游戏结束
state = false;
Score.instance.state = false;
AudioManager.instance.PlayHit();
AudioManager.instance.PlayDie();
Invoke("EndGame", 0.4f);
}
}
// Update is called once per frame
void Update ()
{
//下限
if (transform.position.y < -20)
{
if (state)
{
state = false;
Score.instance.state = false;
AudioManager.instance.PlayDie();
Invoke("EndGame", 0.4f);
}
}
//上限
if (transform.position.y > 20)
{
if (state)
{
state = false;
Score.instance.state = false;
AudioManager.instance.PlayDie();
Invoke("EndGame", 0.4f);
}
}
//判断鼠标左键点击或者空格
if (Input.GetKeyDown(KeyCode.Space)||Input.GetMouseButtonDown(0))
{
AudioManager.instance.PlayFly();
body.velocity = Vector3.zero;
//加速度
body.AddForce(jumpForce);
//控制旋转量
this.transform.rotation = Quaternion.Euler(45, 270, 0);
}
else
{
//旋转
if (transform.rotation.eulerAngles.x >= 280||transform.rotation.eulerAngles.x<=50)
{
transform.Rotate(-150 * Time.deltaTime, 0, 0);
}
}
}
public void EndGame()
{
//保存最佳成绩
PlayerPrefs.SetInt("PlayerScore", Score.instance.score);
bestScore = PlayerPrefs.GetInt("PlayerBestScore");
if (Score.instance.score > bestScore)
bestScore = Score.instance.score;
PlayerPrefs.SetInt("PlayerBestScore", bestScore);
//跳转到结束场景
Application.LoadLevel("End");
}
}
二、障碍物
障碍物只要定时产生,随机设定偏移量,然后添加向左运动的速度就行了,同时要设定自动销毁的时间,回收障碍物,否则内存占用会越来越大。
这里用了三个脚本,分别是上下障碍物和障碍物生成脚本。附加到一个空物体上就行了。
GenerateObstacle.cs
using UnityEngine;
using System.Collections;
public class GenrateObstacle : MonoBehaviour {
public GameObject obstacle;
public GameObject obstacle1;
public float startTime = 1f;
public float gapTime=1.5f;
public float gapDistance = 13;
private Vector3 gapVector;
private Vector3 midVector;
// Use this for initialization
void Start () {
InvokeRepeating("InitiateObstacle", startTime, gapTime);
gapVector = new Vector3(0, gapDistance / 2, 0);
}
void InitiateObstacle()
{
midVector = new Vector3(8,Random.Range(-3.2f, 3.2f),0);
Instantiate(obstacle, midVector+gapVector,new Quaternion(0,0,180,0));
Instantiate(obstacle1, midVector-gapVector, Quaternion.identity);
}
}
Obstacle.cs
using UnityEngine;
using System.Collections;
public class Obstacle : MonoBehaviour {
public float speed = -8f;
private Rigidbody body;
private Transform player;
private bool isPassed = false;
// Use this for initialization
void Start () {
Destroy(this.gameObject, 4);
body = this.GetComponent<Rigidbody>();
body.velocity = new Vector3(speed, 0, 0);
player = GameObject.FindGameObjectWithTag("Bird").transform;
}
// Update is called once per frame
void Update () {
if (player.transform.position.x > transform.position.x && isPassed == false)
{
isPassed = true;
Score.instance.GetScore();
}
}
}
Obstacle1.cs
using UnityEngine;
using System.Collections;
public class Obstacle1 : MonoBehaviour {
public float speed = -8f;
private Rigidbody body;
private Transform player;
private bool isPassed = false;
// Use this for initialization
void Start()
{
Destroy(this.gameObject, 4);
body = this.GetComponent<Rigidbody>();
body.velocity = new Vector3(speed, 0, 0);
player = GameObject.FindGameObjectWithTag("Bird").transform;
}
}
此外还有分数显示,最佳分数显示,音效等等,都是细节。
来源:https://blog.csdn.net/qq402335257/article/details/51591001
0
投稿
猜你喜欢
- 下载maven 解压路径: 打开环境变量:右键此电脑-属性-高级系统设置-高级-环境变量添加以下系统变量:测试:win+
- renameTo方法public boolean renameTo(File dest),File类中的renameTo方法可以操作文件或目
- 一、前言Redis是一个NoSQL(非关系型数据库)数据库之一,key-value存储系统或者说是一个缓存键值对数据库,具有如下特性:基于内
- 本文实例讲述了C#将指定目录所有文件名转换成小写的方法。分享给大家供大家参考。具体如下:using System;using System.
- // 十进制转化为十六进制,结果为C8。 Integer.toHexString(200); //十六进制转化为十进制,
- Feign的作用是将Http请求抽象化为一个Interface客户端,可以调用接口的形式来执行Http请求,以达到简化Http调用的目的。F
- 1. 简介Jpa 是一套ORM 的规范hibernate 不就是一个 ORM 框架也提供了对于 JPA 的实现JPA(Java Persis
- 本文实例为大家分享了java连接SQL Server数据库的具体代码,供大家参考,具体内容如下操作系统:windows 10 64位java
- .c 源程序 ----- 编译 ----- 链接 ---- exe ----运行 -------->程序翻译环境和执行环境翻译环境:源
- 使用Spring Boot 与Dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解
- 一、流程图二、Token1、token是一种客户端认证机制,是一个经过加密的字符串,安全性强,支持跨域2、用户第一次登录,服务器通过数据库校
- 概述最近项目上反馈某个重要的定时任务突然不执行了,很头疼,开发环境和测试环境都没有出现过这个问题。定时任务采用的是ScheduledThre
- 在Android中使用SQLite数据库的入门指南,打算分下面几部分与大家一起分享, 1、什么是SQLite 2、Android中使用SQL
- class ftp{ private string host = null; &n
- 我们通过项目的reimport等众多办法无法解决之后:假设这个是爆红的,因为被我已经解决了。我们进入到我们的本地仓库, 新建包。在repos
- 分转元private String fenToYuan(String amount){ NumberFormat format
- 合并有序数组的实现java版本:实例代码public class Merge {//合并有序数组 public static v
- 文章来源:aspcn 作者:孙雯重复和并发服务器这个应用程序被当作一个重复的服务器.因为它只有在处理完一个进程以后才会接受另一个连接.更多的
- Spring AOP proxyTargetClass的行为要点列表形式proxyTargetClasstrue目标对象实现了接口 – 使用
- 一、介绍搜索了关于spring boot+vue的前后端分离实践项目很多,但是对于最基础登录功能的博客却是几年前的了。于是学习了好几个大神的