目录
学习参考
素材资源导入
1 创建项目
1.1 创建1个2D项目
1.2 导入素材
2 背景图bg
2.0 bg素材
2.1 创建背景
2.2 修改素材,且修改摄像机等
2.2.1 修改导入的原始prefab素材
2.2.2 对应调整摄像机
2.2.3 弄好背景
2.3 背景相关脚本实现
2.3.1 错误例子解析
2.3.2 bg缓慢移动的代码
2.3.3 到边界后bg重复迭代移动的代码
3 地面ground
3.0 地面的素材
3.1 创建好地面
3.1.1 地图层级
3.1.2 给地面增加碰撞
3.1.3 拼好2个地面
3.2 地面ground移动的代码
4 地面修改
4.1 地图尺寸缩小
4.1.1 屏幕宽大约2.8
4.2 设计3个不同地面
4.3 地面也做成prefab,记得选原生的,脱离其他地面的影响
4.4 定义地面的数组,把3个prefab拖进去
4.5 对应脚本
4.6 重大BUG和解决
5 音效的 (这后面都没写完,下次继续搞把)
学习参考
Unity小白超神教程:2D跑酷_哔哩哔哩_bilibili关注一下视频中的公众号,就可以获取资源哈!, 视频播放量 31145、弹幕量 109、点赞数 508、投硬币枚数 333、收藏人数 1004、转发人数 212, 视频作者 Gamer飞羽, 作者简介 玩游戏、做游戏的up一枚,《新印象 Unity 2020游戏开发基础与实战》与《新印象:Unity游戏开发实例教程》作者。,相关视频:【Unity3D经典案例】--天天酷跑,无限场景怎么来的???原来这么简单粗暴,使用Unity制作无限跑酷游戏,Unity 2D 搞定<跑酷超级马里奥>,「福利」「中英字幕」Udemy - Unity休闲3D跑酷游戏制作,Cocos Creator零基础小白超神教程,Unity小白超神教程:气球大战,Unity跑酷游戏开发UI篇 —mvc框架的游戏实战教程_Untiy3d开发新手教程学习#知识分享官#,unity横版2D游戏零基础开发教程A,史上最全Unity3D教程https://www.bilibili.com/video/BV16t4y1z7xw?spm_id_from=333.788.videopod.sections&vd_source=5fa6d2958ae880d9550a17f8050fd5ed
素材资源导入
- 想下载项目资源,结果需要用夸克,用了了,感觉比百度网盘还恶心
- 彷佛看到当年360的样子,赶紧给卸载了
- 直接用之前flappy bird的素材做这个跑酷吧
1 创建项目
1.1 创建1个2D项目
1.2 导入素材
虽然素材很不搭,反正瞎搞^ ^
2 背景图bg
2.0 bg素材
- 这个bg图是378*537,竖版的,
- 手机跑酷虽然竖版不少,但是这个可能横板的更好
- 先这样吧,无所谓了,以后有素材的也好替换吧
2.1 创建背景
- 用了一个竖版的分辨率 1080*1920
- 然后把摄像机的size拉大,仍然不去调整素材本身的尺寸
- 暂时看起来也没啥问题,先这样
2.2 修改素材,且修改摄像机等
2.2.1 修改导入的原始prefab素材
- 突发奇想,之前觉得这个小鸟的素材bg,16pixels/1米 很奇葩
- 然后我这强行给改了会怎么样
- 果然图片在unity直接变小了好多!!
- 我喜欢用标准的 100pixels/1米
2.2.2 对应调整摄像机
size只需要2.5倍差不多够了,之前都是16倍
2.2.3 弄好背景
- 弄好2张图片,都是 bg
- 图片横向378像素,因此x填入3.78即可, 手动改可弄不精确
2.3 背景相关脚本实现
2.3.1 错误例子解析
- Transform.position不能直接当作变量使用
- 必须先通过创建一个变量,获得其内容后,计算后中转,再赋值给Transform.position
如果这么写,会报错
trans1.position.x=trans1.position.x-2*bgSpeed1*Time.deltaTime;
Assets\Scripts\bgControl.cs(26,13): error CS1612: Cannot modify the return value of 'Transform.position' because it is not a variable
必须这么写
Vector2 v1=trans1.position;
v1.x=v1.x-2*bgSpeed1*Time.deltaTime;
trans1.position=v1;
2.3.2 bg缓慢移动的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bgControl : MonoBehaviour
{
public float bgWidth1;
public float bgSpeed1=0.2f;
//use this for initialization;
void Start()
{
}
//Update is called once per frame;
void Update()
{
//需要遍历其下每个bg,都需要这样移动
foreach(Transform trans1 in transform)
{
//每个背景自动移动
//向左边移动,x需要变小
Vector2 v1=trans1.position;
v1.x=v1.x-2*bgSpeed1*Time.deltaTime;
trans1.position=v1;
}
}
}
2.3.3 到边界后bg重复迭代移动的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bgControl : MonoBehaviour
{
public float bgWidth1;
public float bgSpeed1=0.2f;
//use this for initialization;
void Start()
{
}
//Update is called once per frame;
void Update()
{
//需要遍历其下每个bg,都需要这样移动
foreach(Transform trans1 in transform)
{
//每个背景自动移动
//向左边移动,x需要变小
Vector2 v1=trans1.position;
v1.x=v1.x-2*bgSpeed1*Time.deltaTime;
trans1.position=v1;
//背景更换位置
//如果移动距离超过了宽度,再给加2个宽度,放到另外1个图后面
if(v1.x<-bgWidth1)
{
v1.x=v1.x+2*bgWidth1;
trans1.position=v1;
}
}
}
}
3 地面ground
3.0 地面的素材
- 地面素材 800*392,横向的
- 这个bg图是378*537,竖版的,
- 导致地面比bg大一倍,有点不匹配啊。。。。
- 管他呢,就当只有400用 ^ ^ 反正都张一个样,叠起来无所谓
- 记得也把素材16比1 改成100比1
3.1 创建好地面
3.1.1 地图层级
- 因为默认层级0
- 所以bg -99
- ground就-9吧
- 这样新加的东西层级都会高于 bg和ground
3.1.2 给地面增加碰撞
3.1.3 拼好2个地面
- 因为地面本身很大,只要注意下花纹
- 重叠很大就行了,后面也需要按这个距离去更换地面
3.2 地面ground移动的代码
- 为什么不用之前bg相同的代码
- 因为地面有些代码内容会不同
- 新建 groundControl ,内容可以拷贝 bgControl的
- 但是要改一些变量名
- 记得把外面的初始速度,地面宽度要改
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class groundControl : MonoBehaviour
{
public float groundWidth1;
public float groundSpeed1=1f;
//use this for initialization;
void Start()
{
}
//Update is called once per frame;
void Update()
{
//需要遍历其下每个,都需要这样移动
foreach(Transform trans1 in transform)
{
//每个自动移动
//向左边移动,x需要变小
Vector2 v1=trans1.position;
v1.x=v1.x-2*groundSpeed1*Time.deltaTime;
trans1.position=v1;
//背景更换位置
//如果移动距离超过了宽度,再给加2个宽度,放到另外1个图后面
if(v1.x<-groundWidth1)
{
v1.x=v1.x+2*groundWidth1;
trans1.position=v1;
}
}
}
}
4 地面修改
- 跑酷的地图要有jump,地面不能无缝连接!而且需要是随机的!
4.1 地图尺寸缩小
- 地图尺寸也缩小了
- 比如关心X,原始800,这里0.3=240像素=2.4米
脚本的地面长度也修改为2.4
4.1.1 屏幕宽大约2.8
但是很显然我现在地面的宽度小于屏幕,因此
屏幕大概是3.5/3, 也就是地面2.4,屏幕宽2.8 所以2个地面移动出屏幕,实际得是2.8.否则地面会重叠
4.2 设计3个不同地面
- 前面的需要修改
- 新建3个ground
- 每个地面设计不同的分数,我这直接拿小鸟缩小点当分数
- 注意,因为小鸟分数是作为ground的子物体,前面ground缩放变小过,所以这里的小年分数,也做成prefab,然后统一把 birdScore 倍数搞大点
- 至少设计3个地面
- 每个地图设计不同的分数
- ground1: 0
- ground1: 3
- ground1: 2
4.3 地面也做成prefab,记得选原生的,脱离其他地面的影响
方便后面修改
4.4 定义地面的数组,把3个prefab拖进去
//定义一个需要随机生成的gb的数组
public GameObject[] GroundPrefabs;
4.5 对应脚本
- 可以实现
- 随机地面的高度不同
- 随机地面的 缺口不一样长
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class groundControl : MonoBehaviour
{
public float groundWidth1;
public float groundSpeed1=1f;
//定义一个需要随机生成的gb的数组
public GameObject[] GroundPrefabs;
//use this for initialization;
void Start()
{
}
//Update is called once per frame;
void Update()
{
//需要遍历其下每个,都需要这样移动
foreach(Transform trans1 in transform)
{
//每个自动移动
//向左边移动,x需要变小
Vector2 v1=trans1.position;
v1.x=v1.x-2*groundSpeed1*Time.deltaTime;
trans1.position=v1;
if(v1.x<-groundWidth1)
{
//创建新地面newTrans
//,transform)这是是设置为当前gB的子物体
Transform newTrans=Instantiate(GroundPrefabs[Random.Range(0,GroundPrefabs.Length)],transform).transform;
//获得新地面位置
Vector2 v2= newTrans.position;
//修改新地面位置
//为了避免2个地面叠一起,因此这个宽度只能加不能减小,或者减小很小
v2.x=v1.x+groundWidth1*3.5f/3f*2f+Random.Range(0,0.3f);
v2.y=v1.y+Random.Range(-0.4f,0.6f);
//赋值,更新位置
newTrans.position=v2;
//销毁出了屏幕的老地面trans1
Destroy(trans1.gameObject);
}
}
}
}
4.6 重大BUG和解决
就是地面高度,会读之前的那个,然后可能越随越高,越随越低啊
修改一句代码就可以
v2.y=v1.y+Random.Range(-0.4f,0.6f);
修改为
v2.y=v2.y+Random.Range(-0.4f,0.6f);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class groundControl : MonoBehaviour
{
public float groundWidth1;
public float groundSpeed1=1f;
//定义一个需要随机生成的gb的数组
public GameObject[] GroundPrefabs;
//use this for initialization;
void Start()
{
}
//Update is called once per frame;
void Update()
{
//需要遍历其下每个,都需要这样移动
foreach(Transform trans1 in transform)
{
//每个自动移动
//向左边移动,x需要变小
Vector2 v1=trans1.position;
v1.x=v1.x-2*groundSpeed1*Time.deltaTime;
trans1.position=v1;
if(v1.x<-groundWidth1)
{
//创建新地面newTrans
//,transform)这是是设置为当前gB的子物体
Transform newTrans=Instantiate(GroundPrefabs[Random.Range(0,GroundPrefabs.Length)],transform).transform;
//获得新地面位置
Vector2 v2= newTrans.position;
//修改新地面位置
//为了避免2个地面叠一起,因此这个宽度只能加不能减小,或者减小很小
v2.x=v1.x+groundWidth1*3.5f/3f*2f+Random.Range(0,0.3f);
v2.y=v2.y+Random.Range(-0.4f,0.6f);
//赋值,更新位置
newTrans.position=v2;
//销毁出了屏幕的老地面trans1
Destroy(trans1.gameObject);
}
}
}
}
5 音效的 (这后面都没写完,下次继续搞把)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioControl : MonoBehaviour
{
//单例
public static AudioControl Instance;
//播放组件
private AudioSource player;
//use this for initialization;
void Start()
{
//自己创建类的唯一单例
Instance=this;
//获得播放组件
player=GetComponent<AudioSource>();
//这里可以搞背景音乐
}
//Update is called once per frame;
void Update()
{
}
public void play(string name)
{
//通过名称获取音频clip
name="sfx_point";
AudioClip clip=Resources.Load<AudioClip>(name);
//播放
player.PlayOneShot(clip);
}
}