diff --git a/posts/studycase1.md b/posts/studycase1.md index 49d0de5..83b896f 100644 --- a/posts/studycase1.md +++ b/posts/studycase1.md @@ -85,12 +85,12 @@ head: ## 5.勇者行迹录-章节任务 -### 添加虚拟相机根节点 +### 5.1 添加虚拟相机根节点 - 在主相机`Main Camera`上添加 `CinemachineBrain`作为根节点以便统一控制 -### 创建玩家节点 +### 5.2 创建玩家节点 - 创建 `Player` 空节点,创建`Forward`和`Model`空节点并作为`Player`子节点以及虚拟相机 - 注意`Forward`和`Model`的尽量不要有偏移 @@ -100,12 +100,12 @@ head: -### 配置虚拟相机 +### 5.3 配置虚拟相机 - 将虚拟相机 `Follow`、`LookAt` 指向玩家模型,调整 Body/ Aim,使镜头保持第三人称视角 -### 创建 InputActions +### 5.4 创建 InputActions - 创建`Resources`文件夹,右键 `Create > Input Actions`,命名为 `PlayerInputActions` @@ -124,7 +124,7 @@ head: -### 编写脚本`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` diff --git a/posts/studycase2.md b/posts/studycase2.md index fb078bc..395faa4 100644 --- a/posts/studycase2.md +++ b/posts/studycase2.md @@ -44,7 +44,7 @@ head: ## 5.勇者行迹录-章节任务 -### 创建StudyCase2 +### 5.1 创建StudyCase2 - 复制场景和脚本改名为StudyCase2 @@ -53,7 +53,7 @@ head: -### 模型处理 +### 5.2 模型处理 - 导出FBX材质球到Mats文件夹方便修改 @@ -70,7 +70,7 @@ head: -### 用模型替换之前的胶囊体 +### 5.3 用模型替换之前的胶囊体 - 替换模型 @@ -79,7 +79,7 @@ head: -### 创建动画控制器,添加Idle和Move动画片段 +### 5.4 创建动画控制器,添加Idle和Move动画片段 - 创建动画控制器 @@ -100,7 +100,7 @@ head: -### 创建状态条件 +### 5.5 创建状态条件 - 设置idle到moving的条件 @@ -113,7 +113,7 @@ head: -### 编写脚本`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组件 diff --git a/posts/studycase3.md b/posts/studycase3.md index 737cd55..90edaf0 100644 --- a/posts/studycase3.md +++ b/posts/studycase3.md @@ -37,11 +37,11 @@ head: ### 导入[资源](/resources/studycase3/case3.zip)(天空球,描边,道具模型) ## 5.勇者行迹录-章节任务 -### 创建StudyCase3 +### 5.1 创建StudyCase3 - 复制场景和脚本改名为StudyCase3 - 修改控制器的命名空间 -### 道具 +### 5.2 道具 - 把导入的道具模型放到场景中 @@ -119,6 +119,7 @@ namespace StudyCase3 +### 5.3 Player脚本和设置 - 修改`Assets\Scripts\StudyCase3` 下脚本 `ThirdCharacterController.cs` ```csharp using UnityEngine; diff --git a/posts/studycase4.md b/posts/studycase4.md index a09ec63..e5d3f4d 100644 --- a/posts/studycase4.md +++ b/posts/studycase4.md @@ -44,15 +44,20 @@ head: ## 5.勇者行迹录-章节任务 -### 创建studycase4 +### 5.1 创建StudyCase4 - 复制场景和脚本改名为studycase4 - 修改控制器和道具的命名空间 -### IngameDebugConsole +### 5.2 IngameDebugConsole - 把IngameDebugConsole预制体拖入场景 +- 在Resources文件夹下创建材质Line,Shader选择Sprite-Unlit-Default + + + +### 5.3 虚拟摇杆创建 - 全选mobile-controls-1下的Texture @@ -93,6 +98,7 @@ head: +### 5.4 Player脚本和设置 - 修改`Assets\Scripts\studycase4` 下脚本 `ThirdCharacterController.cs` ```csharp using UnityEngine; @@ -275,6 +281,7 @@ namespace StudyCase4 } } ``` +### 5.5打包并测试 - 玩家移除case3的脚本,挂载case4脚本 - BuildSettings切换到Android平台 diff --git a/posts/studycase5.md b/posts/studycase5.md new file mode 100644 index 0000000..99638db --- /dev/null +++ b/posts/studycase5.md @@ -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.光流影卷-演示视频 + + +## 4.初始祭坛-前置准备 +### 导入[资源](/resources/studycase5/case5.zip)(Mirror) + + + + + +## 5.勇者行迹录-章节任务 +### 5.1 创建StudyCase5 + +- 复制场景改名为StudyCase5 +- 删除道具,Player移除ThirdCharacterController + +### 5.2 了解 `Mirror` + +- 打开示例Tanks场景 + + + +- 为了查看联机效果,修改项目设置 + + + +- 把示例打包成exe + + + +- 打开俩个exe,一个选择host,另一个选择client + + + +- 体验联机效果 + + + +### 5.2 使用 `Mirror` + +- 首先将示例场景(MirrorTanks)中的网络控制器(NetworkManager)复制到我们的场景中(StudyCase5) + + + +- Player添加NetworkIdentity组件(联网对象需要这个组件) + + + +- 添加俩个NetworkTransform(Transform同步需要这个组件) + + + +- 设置NetworkTransform(Player和Model) + + + +- Player添加NetworkAnimator组件并设置(动画状态同步需要这个组件) + + + +- PlayerInputActions中创建Esc的监听事件 + + + +- 创建NetworkBehaviour,命名为 `NetWorkPlayer` + + + +- 编写脚本 `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(); + 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的设置 + + + +- 将Player保存为预制体(拖拽到文件夹中会自动保存为预制体) + + + +- 删除场景中的Player,后续由NetworkManager创建,创建俩个出生点,挂载 `NetworkStartPosition` + + + +- 将预制体Player拖拽到NetworkManager上的 `PlayerPrefab` + + + +- 打包exe,打开俩个体验联机 + + + +## 6.异闻录-FAQ \ No newline at end of file diff --git a/public/image/studycase4/创建材质Line,设置shader为Sprite-Unlit-Default.png b/public/image/studycase4/创建材质Line,设置shader为Sprite-Unlit-Default.png new file mode 100644 index 0000000..ee3a24c Binary files /dev/null and b/public/image/studycase4/创建材质Line,设置shader为Sprite-Unlit-Default.png differ diff --git a/public/image/studycase5/Player添加NetworkAnimator组件并设置.png b/public/image/studycase5/Player添加NetworkAnimator组件并设置.png new file mode 100644 index 0000000..a7d7bd3 Binary files /dev/null and b/public/image/studycase5/Player添加NetworkAnimator组件并设置.png differ diff --git a/public/image/studycase5/Player添加NetworkIdentity组件.png b/public/image/studycase5/Player添加NetworkIdentity组件.png new file mode 100644 index 0000000..930a5ad Binary files /dev/null and b/public/image/studycase5/Player添加NetworkIdentity组件.png differ diff --git a/public/image/studycase5/Player的设置.png b/public/image/studycase5/Player的设置.png new file mode 100644 index 0000000..24d8a63 Binary files /dev/null and b/public/image/studycase5/Player的设置.png differ diff --git a/public/image/studycase5/Windows项目设置.png b/public/image/studycase5/Windows项目设置.png new file mode 100644 index 0000000..c759a58 Binary files /dev/null and b/public/image/studycase5/Windows项目设置.png differ diff --git a/public/image/studycase5/player替换tank.png b/public/image/studycase5/player替换tank.png new file mode 100644 index 0000000..83e9def Binary files /dev/null and b/public/image/studycase5/player替换tank.png differ diff --git a/public/image/studycase5/体验联机.png b/public/image/studycase5/体验联机.png new file mode 100644 index 0000000..db1f276 Binary files /dev/null and b/public/image/studycase5/体验联机.png differ diff --git a/public/image/studycase5/体验联机2.png b/public/image/studycase5/体验联机2.png new file mode 100644 index 0000000..659ac93 Binary files /dev/null and b/public/image/studycase5/体验联机2.png differ diff --git a/public/image/studycase5/保存玩家Prefab.png b/public/image/studycase5/保存玩家Prefab.png new file mode 100644 index 0000000..39077a7 Binary files /dev/null and b/public/image/studycase5/保存玩家Prefab.png differ diff --git a/public/image/studycase5/创建NetworkBehaviour.png b/public/image/studycase5/创建NetworkBehaviour.png new file mode 100644 index 0000000..75b0acd Binary files /dev/null and b/public/image/studycase5/创建NetworkBehaviour.png differ diff --git a/public/image/studycase5/复制NetworkManager.png b/public/image/studycase5/复制NetworkManager.png new file mode 100644 index 0000000..86b8704 Binary files /dev/null and b/public/image/studycase5/复制NetworkManager.png differ diff --git a/public/image/studycase5/导入Mirror.png b/public/image/studycase5/导入Mirror.png new file mode 100644 index 0000000..f29bb9b Binary files /dev/null and b/public/image/studycase5/导入Mirror.png differ diff --git a/public/image/studycase5/导入Mirror步骤2.png b/public/image/studycase5/导入Mirror步骤2.png new file mode 100644 index 0000000..93d7789 Binary files /dev/null and b/public/image/studycase5/导入Mirror步骤2.png differ diff --git a/public/image/studycase5/导入Mirror步骤3.png b/public/image/studycase5/导入Mirror步骤3.png new file mode 100644 index 0000000..045e710 Binary files /dev/null and b/public/image/studycase5/导入Mirror步骤3.png differ diff --git a/public/image/studycase5/打包Windows.png b/public/image/studycase5/打包Windows.png new file mode 100644 index 0000000..b0a94ff Binary files /dev/null and b/public/image/studycase5/打包Windows.png differ diff --git a/public/image/studycase5/打开host和client.png b/public/image/studycase5/打开host和client.png new file mode 100644 index 0000000..89f5c8e Binary files /dev/null and b/public/image/studycase5/打开host和client.png differ diff --git a/public/image/studycase5/打开示例Tanks场景.png b/public/image/studycase5/打开示例Tanks场景.png new file mode 100644 index 0000000..32ffcfd Binary files /dev/null and b/public/image/studycase5/打开示例Tanks场景.png differ diff --git a/public/image/studycase5/添加ESC.png b/public/image/studycase5/添加ESC.png new file mode 100644 index 0000000..28bdc81 Binary files /dev/null and b/public/image/studycase5/添加ESC.png differ diff --git a/public/image/studycase5/添加俩个NetworkTransform.png b/public/image/studycase5/添加俩个NetworkTransform.png new file mode 100644 index 0000000..027aaca Binary files /dev/null and b/public/image/studycase5/添加俩个NetworkTransform.png differ diff --git a/public/image/studycase5/设置NetworkTransform.png b/public/image/studycase5/设置NetworkTransform.png new file mode 100644 index 0000000..ead37c6 Binary files /dev/null and b/public/image/studycase5/设置NetworkTransform.png differ diff --git a/public/image/studycase5/设置场景.png b/public/image/studycase5/设置场景.png new file mode 100644 index 0000000..e777fa6 Binary files /dev/null and b/public/image/studycase5/设置场景.png differ diff --git a/public/resources/studycase5/case5.zip b/public/resources/studycase5/case5.zip new file mode 100644 index 0000000..389ac72 Binary files /dev/null and b/public/resources/studycase5/case5.zip differ diff --git a/public/video/studycase5/演示视频.mp4 b/public/video/studycase5/演示视频.mp4 new file mode 100644 index 0000000..b58db7e Binary files /dev/null and b/public/video/studycase5/演示视频.mp4 differ diff --git a/public/video/studycase5/演示视频.png b/public/video/studycase5/演示视频.png new file mode 100644 index 0000000..20b50ea Binary files /dev/null and b/public/video/studycase5/演示视频.png differ