一、写在前面
本文提供常见2.5D游戏创建地图的方式,实现思路来源于Google以及个人的分析。如有解释不妥当之处,还请各位及时指出。
下面会留一个本文配套工程的运行截图,过后将开始我们的正文。
二、2.5D游戏的实现思路
2.5d游戏介于2d游戏和3d游戏之间,是一种特别的游戏呈现方式。常见实现思路分为两种:
-
一种是利用3d的场景和资产,但是玩家只能在固定的两个轴上移动,代表作有:LOL , 超级马里奥2等
《超级马里奥2》 -
第二种是利用2d资产和知识,利用第三维的视差效应,实现相应的效果。代表作有饥荒,The Wild and Heart
《饥荒》
三、思路一:利用3d资产实现2.5D效果
1.分析
常规的3d物体的移动是建立在三个轴上的
如果我们只提取其中一个轴会是什么样的?
- 提取X—Y轴,物体呈现出一种被压缩在视角的正前方的感觉。
《超级马里奥2》
- 提取X—Z轴,物体呈现出一种被俯视的感觉。
《八方旅人》
2.Unity中的实现方法
- 首先新建一个2D项目
- 然后绘制一个2D地图,为树木等添加碰撞器
我这里用的是瓦片地图和平铺调色板来创建的
- 创建角色sprite并添加碰撞器,动画器,刚体,在动画器中编辑人物往不同方向行走的动画
- 编写PlayerMovement.cs脚本,控制人物移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class PlayMovement1 : MonoBehaviour
{
public float speed = 5;
private float inputX;
private float inputY;
private float stopX, stopY;
private Animator animator;
new private Rigidbody2D rigidbody;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = Camera.main.transform.position - transform.position;
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
inputX = Input.GetAxisRaw("Horizontal");
inputY = Input.GetAxisRaw("Vertical");
Vector2 input = new Vector2(inputX, inputY).normalized;
rigidbody.velocity = input * speed;
if(input != Vector2.zero)
{
animator.SetBool("isMoving", true);
stopX = inputX;
stopY = inputY;
}
else
{
animator.SetBool("isMoving", false);
}
animator.SetFloat("InputX", stopX);
animator.SetFloat("InputY", stopY);
Camera.main.transform.position = transform.position + offset;
}
}
-
调整视角,将2d视图变为3d视图,摄像机变为45°俯视视角。
-
此时场景物体并没有立起来,需要为场景物体和角色设置一个脚本,对其和相机旋转角度相同以及设置相机与人物的移动同步。
//FaceCamera.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FacingCamrea : MonoBehaviour
{
Transform[] childs = null;
// Start is called before the first frame update
void Start()
{
childs = new Transform[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
{
childs[i] = transform.GetChild(i);
}
}
// Update is called once per frame
void Update()
{
for(int j = 0; j < childs.Length; j++)
{
childs[j].rotation = Camera.main.transform.rotation;
}
}
}
3.展示
Unity2.5d游戏实现
4.工程文件
https://download.csdn.net/download/Tom870223050/85550510