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"/>