【Unity学习心得】如何使用Unity制作“饥荒”风格的俯视角2.5D游戏

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

由于要找工作开始重新拾起学习Unity,在B站看了许多Unity基础视频后开始慢慢想起了之前学Unity的知识,今天就从简单的项目工程入手,讨论如何使用Unity制作"饥荒"风格的俯视角2.5D游戏


提示:以下是本篇文章正文内容,下面案例可供参考

一、需要导入的素材

这是B站教学Up主的链接:GitHub - RedFF0000/Don-t-Starve

如果上不去github,可以到Unity中打开Package Manager下载并导入tiny RPG - Forest,这就是本期要使用的素材,

接着我们用Tilemap来简单制作一张地图:

二、要实现的步骤

1.俯视角2D人物移动控制

控制人物移动不是随随便便?首先我们给Player新建一个脚本PlayerController.cs,代码内容如下:

代码如下(示例):

c 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed;
    private Rigidbody2D rb2d;
    private Animator animator;

    private float inputX;
    private float inputY;
    private float stopX;
    private float stopY;

    private void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    private void Update()
    {
        inputX = Input.GetAxisRaw("Horizontal");
        inputY = Input.GetAxisRaw("Vertical");
        Vector2 input = (transform.right * inputX + transform.up * inputY).normalized;
        rb2d.velocity = input * speed;
        if(input != new Vector2(0,0))
    {
        animator.SetBool("isMoving", true);
            stopX = inputX;
            stopY = inputY;
    }
    else
    {
            animator.SetBool("isMoving", false);
        }
        animator.SetFloat("InputX", stopX);
        animator.SetFloat("InputY", stopY);
    }
}

解释一下代码:通过给rb2d的速度赋值的方法来控制玩家移动,那这个stopX和stopY时什么东西呢?这个就是防止你键盘停止一个方向输入后你得让角色的朝向保持不变,因为我们用的是Animator Blend Tree来制作动画,把 stopX和stopY作为动画参数传入到动画器中,根据这两个参数决定混合哪个动画,当值为1的时候就是只有这个方向的动画。

你看InputX=1,InputY=0时,相当于 stopX=1,stopY=0,人物的动画就会保持向右站立,这就是为什么我们需要记录最后输入的XorY。

同理我们给Walk的动画系统也制作一个Blend Tree

整个Player动画关系如下所示:

2.2.5D风格的实现

虽然我没玩过饥荒,但2.5D风格很简单,只需要将场景的2D框取消,把mainCamera的rotation.x设置成-45°,Projection改成透视Perspective,马上就出味道来。

3.使用协程实现相机绕玩家旋转效果

首先我们要实现相机跟随玩家,传统派就是直接cinemachine,其实我们只需要一个脚本就能实现,创建CameraRotate.cs

代码如下(示例):

c 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotate : MonoBehaviour
{
    private Transform playerPosition;


    void Start()
    {
        playerPosition = GameObject.FindGameObjectWithTag("Player").transform;
    }

    
    void Update()
    {
    transform.position = playerPosition.position;

    }
}

别忘了给Player添加tag

最后实现通过按下Q和E键让视角旋转

cs 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotate : MonoBehaviour
{
    private Transform playerPosition;
    private bool isRotating;
    public float rotateTime = 0.2f;

    void Start()
    {
        playerPosition = GameObject.FindGameObjectWithTag("Player").transform;
    }

    
    void Update()
    {
	transform.position = playerPosition.position;
	Rotate();
    }

    private void Rotate()
    {
	if(Input.GetKeyDown(KeyCode.Q) && !isRotating)
	{
	    StartCoroutine(RotateRoutine(-45, rotateTime));
	}
	if (Input.GetKeyDown(KeyCode.E) && !isRotating)
	{
	    StartCoroutine(RotateRoutine(45, rotateTime));
	}
    }
    //为了防止旋转僵硬我们用协程渐变地使视角旋转45°
    private IEnumerator RotateRoutine(int angle, float timer)
    {
	float number = 60 * timer;
	float nextAngle = angle / number;
	isRotating = true;
	for (int i = 0; i < number; i++)
	{
	    transform.Rotate(0, 0, nextAngle);
	    yield return new WaitForFixedUpdate();
	}
	isRotating = false;
    }
}

为了防止旋转视角不对,我们可以给main Camera创建一个父对象CameraParent并把该脚本给它,这样它的旋转也会使子物体跟着旋转。


总结

最后成果如下所示:

相关推荐
咕白m62528 分钟前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech6 小时前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
2601_962072551 天前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos
两水先木示1 天前
【Unity3D】小游戏启动优化、发热优化、蒙皮网格优化
游戏
资源分享助手1 天前
杀戮尖塔2下载、Slay the Spire 2中文版、卡牌肉鸽游戏、杀戮尖塔2联机、杀戮尖塔2攻略
游戏
m0_547486661 天前
《C#语言程序设计与实践》 全套PPT课件
c语言·c#·c语言程序设计
叶帆1 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
IT方大同1 天前
(嵌入式操作系统)信号量
嵌入式硬件·c#
z落落1 天前
C# FileStream文件流读取文件
开发语言·c#
久数君1 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型