case3修改

This commit is contained in:
2025-11-27 16:23:06 +08:00
parent fa4d844d13
commit 44b3a16800
11 changed files with 47 additions and 24 deletions

View File

@@ -59,7 +59,9 @@ head:
- 安装 `Input System``Cinemachine`
<img src="/image/studycase1/安装俩个包.png" data-fancybox="gallery"/>
<img src="/image/studycase1/安装俩个包.png" data-fancybox="gallery"/>
> 提示:安装 Input System 后Unity 会提示重启并切换至新输入系统,务必确认。
- 导入示例
@@ -73,11 +75,21 @@ head:
<img src="/image/studycase1/从示例项目导入模型到场景.png" data-fancybox="gallery"/>
> 提示:安装 Input System 后Unity 会提示重启并切换至新输入系统,务必确认。
- 模型目前是紫色的,材质需要进行修改,搜索所有材质
<img src="/image/studycase1/搜索所有材质.png" data-fancybox="gallery"/>
- 依次点击切换到URP
<img src="/image/studycase1/切换到URP.png" data-fancybox="gallery"/>
- 点击确定,等待完成
<img src="/image/studycase1/点击确定.png" data-fancybox="gallery"/>
## 5.勇者行迹录-章节任务
### 添加虚拟相机根节点
- 为相机添加 `CinemachineBrain`作为根节点以便统一控制
- 在主相机`Main Camera`添加 `CinemachineBrain`作为根节点以便统一控制
<img src="/image/studycase1/添加虚拟相机根节点.png" data-fancybox="gallery"/>
@@ -97,18 +109,23 @@ head:
<img src="/image/studycase1/配置虚拟相机.png" data-fancybox="gallery"/>
### 创建 InputActions
- 创建`Resources`文件夹,右键 `Create > Input Actions`,,命名为 `PlayerInputActions`
- 创建`Resources`文件夹,右键 `Create > Input Actions`,命名为 `PlayerInputActions`
<img src="/image/studycase1/创建InputActions.png" data-fancybox="gallery"/>
<img src="/image/studycase1/创建InputActions.png" data-fancybox="gallery"/>
<img src="/image/studycase1/命名为PlayerInputActions.png" data-fancybox="gallery"/>
- 创建 `Player` Action Map添加 `Move`Vector2`Jump`Button
<img src="/image/studycase1/创建playerinputactions.png" data-fancybox="gallery"/>
- Move 绑定 `WASD` Jump 绑定 `space`
- Move 绑定 `WASD`
<img src="/image/studycase1/创建4向绑定.png" data-fancybox="gallery"/>
<img src="/image/studycase1/绑定wasd.png" data-fancybox="gallery"/>
<img src="/image/studycase1/绑定wasd.png" data-fancybox="gallery"/>
- Jump 绑定 `space`
<img src="/image/studycase1/jump绑定space.png" data-fancybox="gallery"/>
### 编写脚本`ThirdCharacterController.cs`
-`Assets\Scripts\StudyCase1` 下创建脚本 `ThirdCharacterController`

View File

@@ -142,7 +142,7 @@ namespace StudyCase2
model = transform.Find("Model");
animator = model.GetComponentInChildren<Animator>();
vCam = transform.Find("Virtual Camera").GetComponent<Cinemachine.CinemachineVirtualCamera>();
inputAction = Resources.Load<InputActionAsset>("Player");
inputAction = Resources.Load<InputActionAsset>("PlayerInputActions");
inputAction.FindAction("Move").started += OnMove;
inputAction.FindAction("Move").performed += OnMove;
inputAction.FindAction("Move").canceled += OnMove;
@@ -173,7 +173,7 @@ namespace StudyCase2
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed && characterController.isGrounded)
if (characterController.isGrounded)
{
moveDir.y = jumpSpeed;
}
@@ -191,7 +191,7 @@ private void Awake()
model = transform.Find("Model");
animator = model.GetComponentInChildren<Animator>();
vCam = transform.Find("Virtual Camera").GetComponent<Cinemachine.CinemachineVirtualCamera>();
inputAction = Resources.Load<InputActionAsset>("Player");
inputAction = Resources.Load<InputActionAsset>("PlayerInputActions");
inputAction.FindAction("Move").started += OnMove;
inputAction.FindAction("Move").performed += OnMove;
inputAction.FindAction("Move").canceled += OnMove;
@@ -210,8 +210,10 @@ private void Awake()
<img src="/image/studycase2/重载控制器.png" data-fancybox="gallery"/>
## 6.异闻录-FAQ
- **代码查找组件报错**:确认获取物体的名字和物体实际名字一致。
- **代码加载InputActionAsset报错**:确认处于`Resources`文件夹下,名字和查找名字一致。
- **移动不播放动画**:确认动画控制器的参数名字和脚本中设置的一样。
- **动画只播放一遍**:检查是否勾选循环播放,在模型上看。
- **动画状态切换有延迟**确认动画控制器中连接线的HasExitTime是否勾选。
- **动画状态切换有延迟**:确认动画控制器中连接线的`HasExitTime`是否勾选。
完成以上步骤,即可得到一个带动画的第三人称控制器。[项目地址](http://home.gtuantuan.online:8300/TuanTuan/StudyCase)

View File

@@ -115,6 +115,10 @@ namespace StudyCase3
<img src="/image/studycase3/道具挂载脚本.png" data-fancybox="gallery"/>
- 创建Pick输入事件绑定按键F
<img src="/image/studycase3/创建Pick输入事件绑定F.png" data-fancybox="gallery"/>
- 修改`Assets\Scripts\StudyCase3` 下脚本 `ThirdCharacterController.cs`
```csharp
using UnityEngine;
@@ -160,7 +164,7 @@ namespace StudyCase3
model = transform.Find("Model");
animator = model.GetComponentInChildren<Animator>();
vCam = transform.Find("Virtual Camera").GetComponent<Cinemachine.CinemachineVirtualCamera>();
inputAction = Resources.Load<InputActionAsset>("Player");
inputAction = Resources.Load<InputActionAsset>("PlayerInputActions");
inputAction.FindAction("Move").started += OnMove;
inputAction.FindAction("Move").performed += OnMove;
inputAction.FindAction("Move").canceled += OnMove;
@@ -168,6 +172,12 @@ namespace StudyCase3
inputAction.FindAction("Pick").performed += OnPickUp;
inputAction.Enable();
itemRoot = transform.Find("ItemRoot");
if (lineRenderer == null)
{
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Universal Render Pipeline/2D/Sprite-Unlit-Default"));
lineRenderer.useWorldSpace = true;
}
}
private void Update()
{
@@ -214,7 +224,12 @@ namespace StudyCase3
}
void CheckRayHit()
{
if (pickMode != PickMode.RayCast) return;
lineRenderer.enabled = useDrawRay;
if (pickMode != PickMode.RayCast)
{
useDrawRay = false;
return;
}
Ray ray = new Ray(model.position, model.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
@@ -239,17 +254,6 @@ namespace StudyCase3
}
void DrawRay(Vector3 startPos, Vector3 endPos, Color color)
{
if (lineRenderer == null)
{
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Universal Render Pipeline/2D/Sprite-Unlit-Default"));
lineRenderer.useWorldSpace = true;
}
if (!useDrawRay)
{
lineRenderer.enabled = false;
return;
}
lineRenderer.startColor = lineRenderer.endColor = color;
lineRenderer.startWidth = lineRenderer.endWidth = 0.1f;
lineRenderer.positionCount = 2;

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 610 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 608 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 KiB

After

Width:  |  Height:  |  Size: 290 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB