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

相关文章

Java实现将byte[]转换为File对象

《Java实现将byte[]转换为File对象》这篇文章将通过一个简单的例子为大家演示Java如何实现byte[]转换为File对象,并将其上传到外部服务器,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言1. 问题背景2. 环境准备3. 实现步骤3.1 从 URL 获取图片字节数据3.2 将字节数组

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

Nginx实现前端灰度发布

《Nginx实现前端灰度发布》灰度发布是一种重要的策略,它允许我们在不影响所有用户的情况下,逐步推出新功能或更新,通过灰度发布,我们可以测试新版本的稳定性和性能,下面就来介绍一下前端灰度发布的使用,感... 目录前言一、基于权重的流量分配二、基于 Cookie 的分流三、基于请求头的分流四、基于请求参数的分

Python Excel实现自动添加编号

《PythonExcel实现自动添加编号》这篇文章主要为大家详细介绍了如何使用Python在Excel中实现自动添加编号效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、背景介绍2、库的安装3、核心代码4、完整代码1、背景介绍简单的说,就是在Excel中有一列h=会有重复

MySQL的隐式锁(Implicit Lock)原理实现

《MySQL的隐式锁(ImplicitLock)原理实现》MySQL的InnoDB存储引擎中隐式锁是一种自动管理的锁,用于保证事务在行级别操作时的数据一致性和安全性,本文主要介绍了MySQL的隐式锁... 目录1. 背景:什么是隐式锁?2. 隐式锁的工作原理3. 隐式锁的类型4. 隐式锁的实现与源代码分析4

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

MySQL中Next-Key Lock底层原理实现

《MySQL中Next-KeyLock底层原理实现》Next-KeyLock是MySQLInnoDB存储引擎中的一种锁机制,结合记录锁和间隙锁,用于高效并发控制并避免幻读,本文主要介绍了MySQL中... 目录一、Next-Key Lock 的定义与作用二、底层原理三、源代码解析四、总结Next-Key L

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

Redis实现RBAC权限管理

《Redis实现RBAC权限管理》本文主要介绍了Redis实现RBAC权限管理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1. 什么是 RBAC?2. 为什么使用 Redis 实现 RBAC?3. 设计 RBAC 数据结构

SpringBoot基于沙箱环境实现支付宝支付教程

《SpringBoot基于沙箱环境实现支付宝支付教程》本文介绍了如何使用支付宝沙箱环境进行开发测试,包括沙箱环境的介绍、准备步骤、在SpringBoot项目中结合支付宝沙箱进行支付接口的实现与测试... 目录一、支付宝沙箱环境介绍二、沙箱环境准备2.1 注册入驻支付宝开放平台2.2 配置沙箱环境2.3 沙箱