This commit is contained in:
2025-12-02 18:28:53 +08:00
parent 917ba0626a
commit 92c8721d7a
29 changed files with 225 additions and 19 deletions

View File

@@ -85,12 +85,12 @@ head:
<img src="/image/studycase1/点击确定.png" data-fancybox="gallery"/>
## 5.勇者行迹录-章节任务
### 添加虚拟相机根节点
### 5.1 添加虚拟相机根节点
- 在主相机`Main Camera`上添加 `CinemachineBrain`作为根节点以便统一控制
<img src="/image/studycase1/添加虚拟相机根节点.png" data-fancybox="gallery"/>
### 创建玩家节点
### 5.2 创建玩家节点
- 创建 `Player` 空节点,创建`Forward``Model`空节点并作为`Player`子节点以及虚拟相机
- 注意`Forward``Model`的尽量不要有偏移
@@ -100,12 +100,12 @@ head:
<img src="/image/studycase1/玩家节点.png" data-fancybox="gallery"/>
### 配置虚拟相机
### 5.3 配置虚拟相机
- 将虚拟相机 `Follow``LookAt` 指向玩家模型,调整 Body/ Aim使镜头保持第三人称视角
<img src="/image/studycase1/配置虚拟相机.png" data-fancybox="gallery"/>
### 创建 InputActions
### 5.4 创建 InputActions
- 创建`Resources`文件夹,右键 `Create > Input Actions`,命名为 `PlayerInputActions`
<img src="/image/studycase1/创建InputActions.png" data-fancybox="gallery"/>
@@ -124,7 +124,7 @@ head:
<img src="/image/studycase1/jump绑定space.png" data-fancybox="gallery"/>
### 编写脚本`ThirdCharacterController.cs`
### 5.5 编写脚本`ThirdCharacterController.cs`
-`Assets\Scripts\StudyCase1` 下创建脚本 `ThirdCharacterController`
```csharp
using UnityEngine;
@@ -175,14 +175,14 @@ namespace StudyCase1
}
```
### 代码讲解
### 5.6 代码讲解
- **namespace StudyCase1**:因为可能出现同名文件
- **forward**:作为“参照物”同步虚拟相机的 Y 轴旋转,用来转换输入的坐标轴。
- **moveDir**:在本地坐标系下计算移动向量,并通过 `CharacterController.Move` 驱动。
- **OnMove/OnJump**:直接使用 Input System 回调,确保只有在贴地时才写入输入,避免空中漂移。
- **旋转插值**`Quaternion.Slerp` 让角色转身更加顺滑,可根据手感微调 `turnSpeed`
### Player节点设置
### 5.7 Player节点设置
- 挂载`ThirdCharacterController`
<img src="/image/studycase1/挂载ThirdCharacterController.png" data-fancybox="gallery"/>

View File

@@ -44,7 +44,7 @@ head:
<img src="/image/studycase2/导入shader.png" data-fancybox="gallery"/>
## 5.勇者行迹录-章节任务
### 创建StudyCase2
### 5.1 创建StudyCase2
- 复制场景和脚本改名为StudyCase2
<img src="/image/studycase2/复制场景和脚本改名为StudyCase2.png" data-fancybox="gallery"/>
@@ -53,7 +53,7 @@ head:
<img src="/image/studycase2/修改控制器的命名空间.png" data-fancybox="gallery"/>
### 模型处理
### 5.2 模型处理
- 导出FBX材质球到Mats文件夹方便修改
<img src="/image/studycase2/导出材质.png" data-fancybox="gallery"/>
@@ -70,7 +70,7 @@ head:
<img src="/image/studycase2/嘴部材质特殊处理.png" data-fancybox="gallery"/>
### 用模型替换之前的胶囊体
### 5.3 用模型替换之前的胶囊体
- 替换模型
<img src="/image/studycase2/替换模型.png" data-fancybox="gallery"/>
@@ -79,7 +79,7 @@ head:
<img src="/image/studycase2/调整模型大小和位置.png" data-fancybox="gallery"/>
### 创建动画控制器添加Idle和Move动画片段
### 5.4 创建动画控制器添加Idle和Move动画片段
- 创建动画控制器
<img src="/image/studycase2/创建动画控制器.png" data-fancybox="gallery"/>
@@ -100,7 +100,7 @@ head:
<img src="/image/studycase2/moving和idle设置为循环播放.png" data-fancybox="gallery"/>
### 创建状态条件
### 5.5 创建状态条件
- 设置idle到moving的条件
<img src="/image/studycase2/设置idle到moving的条件.png" data-fancybox="gallery"/>
@@ -113,7 +113,7 @@ head:
<img src="/image/studycase2/设置move end到moving的条件.png" data-fancybox="gallery"/>
### 编写脚本`ThirdCharacterController.cs`
### 5.6 编写脚本`ThirdCharacterController.cs`
- 修改`Assets\Scripts\StudyCase2` 下脚本 `ThirdCharacterController.cs`
```csharp
using UnityEngine;
@@ -181,7 +181,7 @@ namespace StudyCase2
}
}
```
### 代码讲解
### 5.7代码讲解
在Awake中自动获取组件移除PlayerInput
```csharp
private void Awake()
@@ -205,7 +205,7 @@ private void Awake()
//false播放Move end动画然后回到Idle
animator.SetBool("Move", true);
```
### Player节点设置
### 5.8 Player节点设置
挂载studycase2的控制器移除PlayerInput组件
<img src="/image/studycase2/重载控制器.png" data-fancybox="gallery"/>

View File

@@ -37,11 +37,11 @@ head:
### 导入[资源](/resources/studycase3/case3.zip)(天空球,描边,道具模型)
## 5.勇者行迹录-章节任务
### 创建StudyCase3
### 5.1 创建StudyCase3
- 复制场景和脚本改名为StudyCase3
- 修改控制器的命名空间
### 道具
### 5.2 道具
- 把导入的道具模型放到场景中
<img src="/image/studycase3/把导入的道具模型放到场景中.png" data-fancybox="gallery"/>
@@ -119,6 +119,7 @@ namespace StudyCase3
<img src="/image/studycase3/创建Pick输入事件绑定F.png" data-fancybox="gallery"/>
### 5.3 Player脚本和设置
- 修改`Assets\Scripts\StudyCase3` 下脚本 `ThirdCharacterController.cs`
```csharp
using UnityEngine;

View File

@@ -44,15 +44,20 @@ head:
<img src="/image/studycase4/onscreen.png" data-fancybox="gallery"/>
## 5.勇者行迹录-章节任务
### 创建studycase4
### 5.1 创建StudyCase4
- 复制场景和脚本改名为studycase4
- 修改控制器和道具的命名空间
### IngameDebugConsole
### 5.2 IngameDebugConsole
- 把IngameDebugConsole预制体拖入场景
<img src="/image/studycase4/debug.png" data-fancybox="gallery"/>
- 在Resources文件夹下创建材质LineShader选择Sprite-Unlit-Default
<img src="/image/studycase4/创建材质Line设置shader为Sprite-Unlit-Default.png" data-fancybox="gallery"/>
### 5.3 虚拟摇杆创建
- 全选mobile-controls-1下的Texture
<img src="/image/studycase4/全选mobile-controls-1下的Texture.png" data-fancybox="gallery"/>
@@ -93,6 +98,7 @@ head:
<img src="/image/studycase4/配置虚拟相机输入.gif" data-fancybox="gallery"/>
### 5.4 Player脚本和设置
- 修改`Assets\Scripts\studycase4` 下脚本 `ThirdCharacterController.cs`
```csharp
using UnityEngine;
@@ -275,6 +281,7 @@ namespace StudyCase4
}
}
```
### 5.5打包并测试
- 玩家移除case3的脚本挂载case4脚本
- BuildSettings切换到Android平台

198
posts/studycase5.md Normal file
View File

@@ -0,0 +1,198 @@
---
title: studycase5
date: 2025-12-2 17:23:05
tags: [studycase, unity]
pinned: true
head:
- - meta
- name: description
content: vitepress-theme-bluearchive studycase5
- - meta
- name: keywords
content: vitepress theme bluearchive studycase5
---
# 虚拟摇杆
- [世界构筑的起始篇章-编辑器](#_1-世界构筑的起始篇章-编辑器)
- [光芒汇聚之所-章节目标](#_2-光芒汇聚之所-章节目标)
- [光流影卷-演示视频](#_3-光流影卷-演示视频)
- [初始祭坛-前置准备](#_4-初始祭坛-前置准备)
- [勇者行迹录-章节任务](#_5-勇者行迹录-章节任务)
- [异闻录-FAQ](#_6-异闻录-faq)
## 1.世界构筑的起始篇章-编辑器
- **Unity 2022.3.62f2**
- **Visual Studio 2022**
## 2.光芒汇聚之所-章节目标
- **使用Mirror实现简单联机**
- **打包exe**
## 3.光流影卷-演示视频
<video id="vdMain" controls muted poster="/video/studycase5/演示视频.png" playsinline>
<source id="vSource" src="/video/studycase5/演示视频.mp4" type="video/mp4" />
</video>
## 4.初始祭坛-前置准备
### 导入[资源](/resources/studycase5/case5.zip)(Mirror)
<img src="/image/studycase5/导入Mirror.png" data-fancybox="gallery"/>
<img src="/image/studycase5/导入Mirror步骤2.png" data-fancybox="gallery"/>
<img src="/image/studycase5/导入Mirror步骤3.png" data-fancybox="gallery"/>
## 5.勇者行迹录-章节任务
### 5.1 创建StudyCase5
- 复制场景改名为StudyCase5
- 删除道具Player移除ThirdCharacterController
### 5.2 了解 `Mirror`
- 打开示例Tanks场景
<img src="/image/studycase5/打开示例Tanks场景.png" data-fancybox="gallery"/>
- 为了查看联机效果,修改项目设置
<img src="/image/studycase5/Windows项目设置.png" data-fancybox="gallery"/>
- 把示例打包成exe
<img src="/image/studycase5/打包Windows.png" data-fancybox="gallery"/>
- 打开俩个exe一个选择host另一个选择client
<img src="/image/studycase5/打开host和client.png" data-fancybox="gallery"/>
- 体验联机效果
<img src="/image/studycase5/体验联机.png" data-fancybox="gallery"/>
### 5.2 使用 `Mirror`
- 首先将示例场景(MirrorTanks)中的网络控制器(NetworkManager)复制到我们的场景中(StudyCase5)
<img src="/image/studycase5/复制NetworkManager.png" data-fancybox="gallery"/>
- Player添加NetworkIdentity组件(联网对象需要这个组件)
<img src="/image/studycase5/Player添加NetworkIdentity组件.png" data-fancybox="gallery"/>
- 添加俩个NetworkTransform(Transform同步需要这个组件)
<img src="/image/studycase5/添加俩个NetworkTransform.png" data-fancybox="gallery"/>
- 设置NetworkTransform(Player和Model)
<img src="/image/studycase5/设置NetworkTransform.png" data-fancybox="gallery"/>
- Player添加NetworkAnimator组件并设置(动画状态同步需要这个组件)
<img src="/image/studycase5/Player添加NetworkAnimator组件并设置.png" data-fancybox="gallery"/>
- PlayerInputActions中创建Esc的监听事件
<img src="/image/studycase5/添加ESC.png" data-fancybox="gallery"/>
- 创建NetworkBehaviour命名为 `NetWorkPlayer`
<img src="/image/studycase5/创建NetworkBehaviour.png" data-fancybox="gallery"/>
- 编写脚本 `NetWorkPlayer`
```csharp
using UnityEngine;
using Mirror;
using UnityEngine.InputSystem;
using Cinemachine;
public class NetWorkPlayer : NetworkBehaviour
{
public CharacterController characterController;
public InputActionAsset inputAction;
public Animator animator;
public Transform forward;
public Transform model;
public CinemachineVirtualCamera vCam;
public CursorLockMode cursorLock;
public float moveSpeed = 5f;
public float jumpSpeed = 2f;
public float turnSpeed = 10f;
public float gravity = 10f;
Vector3 moveDir;
Vector2 moveInput;
public override void OnStartLocalPlayer()
{
Cursor.lockState = cursorLock;
vCam.Priority = 15;
inputAction.FindAction("Move").started += OnMove;
inputAction.FindAction("Move").performed += OnMove;
inputAction.FindAction("Move").canceled += OnMove;
inputAction.FindAction("Jump").performed += OnJump;
inputAction.FindAction("Esc").performed += OnEsc;
inputAction.Enable();
}
private void Update()
{
if (!isLocalPlayer) return;
moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
forward.eulerAngles = new Vector3(0, vCam.transform.eulerAngles.y, 0);
moveDir = forward.TransformDirection(moveDir);
if (moveInput != Vector2.zero)
{
animator.SetBool("Move", true);
Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
}
else animator.SetBool("Move", false);
if (!characterController.isGrounded)
moveDir.y -= gravity * Time.deltaTime;
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
}
public void OnMove(InputAction.CallbackContext context)
{
if (!isLocalPlayer) return;
if (characterController.isGrounded)
moveInput = context.ReadValue<Vector2>();
else moveInput = Vector2.zero;
}
public void OnJump(InputAction.CallbackContext context)
{
if (!isLocalPlayer) return;
if (characterController.isGrounded)
{
moveDir.y = jumpSpeed;
}
}
public void OnEsc(InputAction.CallbackContext context)
{
if (!isLocalPlayer) return;
if(Cursor.lockState == CursorLockMode.Locked)
Cursor.lockState = CursorLockMode.None;
else
Cursor.lockState = CursorLockMode.Locked;
}
}
```
- Player挂载 `NetWorkPlayer` ,Player的设置
<img src="/image/studycase5/Player的设置.png" data-fancybox="gallery"/>
- 将Player保存为预制体(拖拽到文件夹中会自动保存为预制体)
<img src="/image/studycase5/保存玩家Prefab.png" data-fancybox="gallery"/>
- 删除场景中的Player后续由NetworkManager创建,创建俩个出生点,挂载 `NetworkStartPosition`
<img src="/image/studycase5/设置场景.png" data-fancybox="gallery"/>
- 将预制体Player拖拽到NetworkManager上的 `PlayerPrefab`
<img src="/image/studycase5/player替换tank.png" data-fancybox="gallery"/>
- 打包exe打开俩个体验联机
<img src="/image/studycase5/体验联机2.png" data-fancybox="gallery"/>
## 6.异闻录-FAQ

Binary file not shown.

After

Width:  |  Height:  |  Size: 591 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 693 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 655 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 903 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 667 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 619 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 919 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 508 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 384 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 546 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 593 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 332 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 656 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 761 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 880 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 471 KiB