unity2D笔记-实现饥荒效果的2.5D游戏

2024-03-26 01:40

本文主要是介绍unity2D笔记-实现饥荒效果的2.5D游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

教程来自B站大佬:https://www.bilibili.com/video/BV1DT4y1A7DJ?spm_id_from=333.337.search-card.all.click&vd_source=19df42746a97e8a5f29ac78388f521d5
在这里主要有2点感悟:
1.对于混合树了解更深刻了
2.人物向量转换关系
3.协程的使用

1.混合树控制人物移动

通过控制输入的x,y向量来控制人物的动画
在这里插入图片描述

2.物体方向跟随镜头进行调整旋转角度

让子物体的旋转角度与相机旋转角度一致

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FacingCarmera : MonoBehaviour
{Transform[] childs;// Start is called before the first frame updatevoid Start(){childs = new Transform[transform.childCount];for (int i = 0; i < transform.childCount; i++){childs[i] = transform.GetChild(i);}}// Update is called once per framevoid Update(){for(int i = 0; i < childs.Length; i++){childs[i].rotation = Camera.main.transform.rotation;//让节点上的子物体与相机旋转角一致}}
}

3.通过手柄摇杆LB RB来转动视角

视角转动脚本

using System.Collections;
using System.Collections.Generic;
using SK.Framework;
using UnityEngine;public class RotateCarmera: MonoBehaviour
{public float rotateTime = 0.2f;//旋转所花费时间private Transform player;private bool isRotating = false;void Start(){player = GameObject.FindGameObjectWithTag("Player").transform;}// Update is called once per framevoid Update(){transform.position = player.position;Rotate();}void Rotate(){if (Input.GetKeyDown(KeyCode.Q) ||Input.GetKeyDown(XBox.LB) && !isRotating){StartCoroutine(RotateAround(-45, rotateTime));}if (Input.GetKeyDown(KeyCode.E)|| Input.GetKeyDown(XBox.RB) && !isRotating){StartCoroutine(RotateAround(45, rotateTime));}}//使用协程函数来更新镜头旋转角度 IEnumerator RotateAround(float angel,float time){float number = 60 * time;float nextAngel = angel / number;isRotating = true;for(int i = 0; i < number; i++){transform.Rotate(new Vector3(0, 0, nextAngel));yield return new WaitForFixedUpdate();//暂停执行 等到下一帧时继续执行下个循环//默认FixedUpdate()一秒更新60帧//使用其他频率 修改number前帧数 例如100 这里使用waitforseconds(0.01f)}isRotating = false;}
}

手柄摇杆对照脚本

using UnityEngine;namespace SK.Framework
{/// <summary>/// XBox按键/// </summary>public class XBox{/// <summary>/// 左侧摇杆水平轴/// X axis/// </summary>public const string LeftStickHorizontal = "LeftStickHorizontal";/// <summary>/// 左侧摇杆垂直轴/// Y axis/// </summary>public const string LeftStickVertical = "LeftStickVertical";/// <summary>/// 右侧摇杆水平轴/// 4th axis/// </summary>public const string RightStickHorizontal = "RightStickHorizontal";/// <summary>/// 右侧摇杆垂直轴/// 5th axis/// </summary>public const string RightStickVertical = "RightStickVertical";/// <summary>/// 十字方向盘水平轴/// 6th axis/// </summary>public const string DPadHorizontal = "DPadHorizontal";/// <summary>/// 十字方向盘垂直轴/// 7th axis/// </summary>public const string DPadVertical = "DPadVertical";/// <summary>/// LT/// 9th axis/// </summary>public const string LT = "LT";/// <summary>/// RT/// 10th axis/// </summary>public const string RT = "RT";/// <summary>/// 左侧摇杆按键/// joystick button 8/// </summary>public const KeyCode LeftStick = KeyCode.JoystickButton8;/// <summary>/// 右侧摇杆按键/// joystick button 9/// </summary>public const KeyCode RightStick = KeyCode.JoystickButton9;/// <summary>/// A键/// joystick button 0/// </summary>public const KeyCode A = KeyCode.JoystickButton0;/// <summary>/// B键/// joystick button 1/// </summary>public const KeyCode B = KeyCode.JoystickButton1;/// <summary>/// X键/// joystick button 2/// </summary>public const KeyCode X = KeyCode.JoystickButton2;/// <summary>/// Y键/// joystick button 3/// </summary>public const KeyCode Y = KeyCode.JoystickButton3;/// <summary>/// LB键/// joystick button 4/// </summary>public const KeyCode LB = KeyCode.JoystickButton4;/// <summary>/// RB键/// joystick button 5/// </summary>public const KeyCode RB = KeyCode.JoystickButton5;/// <summary>/// View视图键/// joystick button 6/// </summary>public const KeyCode View = KeyCode.JoystickButton6;/// <summary>/// Menu菜单键/// joystick button 7/// </summary>public const KeyCode Menu = KeyCode.JoystickButton7;}
}

4.人物的控制脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Player : MonoBehaviour
{public float speed;new private Rigidbody2D rigidbody;private Animator animator;private float inputX, inputY;//private Vector3 offset;void Start(){// offset = Camera.main.transform.position - transform.position; rigidbody = GetComponent<Rigidbody2D>();animator = GetComponent<Animator>();}// Update is called once per framevoid Update(){inputX = Input.GetAxisRaw("Horizontal");inputY = Input.GetAxisRaw("Vertical");Vector2 input = (inputX*transform.right + inputY*transform.up).normalized; //标准化到0 1 rigidbody.velocity = input * speed;if (input != Vector2.zero){animator.SetBool("IsMoving", true);}else{animator.SetBool("IsMoving", false);}animator.SetFloat("InputX", inputX);animator.SetFloat("InputY", inputY);//  Camera.main.transform.position = transform.position + offset;}
}

修改 Vector2 input = new Vector2(inputX, inputY).normalized;
到 的解释:
inputX和inputY是基于世界坐标系的参数,如果当自身坐标系和世界坐标系发生偏转时(按下LB或者RB)如下图所示,使用INPUTX 的参数也仅仅会让物体基于世界坐标移动,人物斜着走。
在这里插入图片描述
因此需要对人物基于自身坐标进行矫正:
假设人物要向其自身坐标系的Y轴移动
在这里插入图片描述
归一化是保证速度不会跟随方向的变化而动态变化,详细见相关文章:为什么要使用Vector2().normalized()

这篇关于unity2D笔记-实现饥荒效果的2.5D游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/846920

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的