Compare commits

...

9 Commits

Author SHA1 Message Date
c34aac38ab 111 2025-12-04 17:41:03 +08:00
92c8721d7a case5 2025-12-02 18:28:53 +08:00
917ba0626a case4 2025-12-01 19:00:28 +08:00
135ac2c03c 111 2025-11-29 18:21:03 +08:00
a47d27059d 111 2025-11-27 16:48:01 +08:00
c87e4d9ddf 更新时间 2025-11-27 16:36:45 +08:00
83cb8eaaea 111 2025-11-27 16:32:32 +08:00
59d430a388 Merge branch 'main' of http://home.gtuantuan.online:8300/TuanTuan/BA-VitePress-Pages 2025-11-27 16:23:19 +08:00
44b3a16800 case3修改 2025-11-27 16:23:06 +08:00
56 changed files with 569 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
---
title: StudyCase1
date: 2025-11-26 15:35
date: 2025-11-27 16:35:00
tags: [studycase, unity]
pinned: true
head:
@@ -60,7 +60,9 @@ head:
- 安装 `Input System``Cinemachine`
<img src="/image/studycase1/安装俩个包.png" data-fancybox="gallery"/>
<img src="/image/studycase1/安装俩个包.png" data-fancybox="gallery"/>
> 提示:安装 Input System 后Unity 会提示重启并切换至新输入系统,务必确认。
- 导入示例
@@ -70,15 +72,25 @@ head:
<img src="/image/studycase1/从示例项目导入模型到场景.png" data-fancybox="gallery"/>
> 提示:安装 Input System 后Unity 会提示重启并切换至新输入系统,务必确认。
- 模型目前是紫色的,材质需要进行修改,搜索所有材质
<img src="/image/studycase1/搜索所有材质.png" data-fancybox="gallery"/>
- 全选材质,点击Edit>Rendering>Materials>Convert Selected Built-in Materials to URP
<img src="/image/studycase1/切换到URP.png" data-fancybox="gallery"/>
- 点击确定,等待完成
<img src="/image/studycase1/点击确定.png" data-fancybox="gallery"/>
## 5.勇者行迹录-章节任务
### 添加虚拟相机根节点
- 为相机添加 `CinemachineBrain`作为根节点以便统一控制
### 5.1 添加虚拟相机根节点
- 在主相机`Main Camera`添加 `CinemachineBrain`作为根节点以便统一控制
<img src="/image/studycase1/添加虚拟相机根节点.png" data-fancybox="gallery"/>
### 创建玩家节点
### 5.2 创建玩家节点
- 创建 `Player` 空节点,创建`Forward``Model`空节点并作为`Player`子节点以及虚拟相机
- 注意`Forward``Model`的尽量不要有偏移
@@ -88,26 +100,31 @@ head:
<img src="/image/studycase1/玩家节点.png" data-fancybox="gallery"/>
### 配置虚拟相机
### 5.3 配置虚拟相机
- 将虚拟相机 `Follow``LookAt` 指向玩家模型,调整 Body/ Aim使镜头保持第三人称视角
<img src="/image/studycase1/配置虚拟相机.png" data-fancybox="gallery"/>
### 创建 InputActions
- 创建`Resources`文件夹,右键 `Create > Input Actions`,,命名为 `PlayerInputActions`
### 5.4 创建 InputActions
- 创建`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"/>
### 编写脚本`ThirdCharacterController.cs`
- Jump 绑定 `space`
<img src="/image/studycase1/jump绑定space.png" data-fancybox="gallery"/>
### 5.5 编写脚本`ThirdCharacterController.cs`
-`Assets\Scripts\StudyCase1` 下创建脚本 `ThirdCharacterController`
```csharp
using UnityEngine;
@@ -158,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

@@ -1,6 +1,6 @@
---
title: StudyCase2
date: 2025-11-26 15:38
date: 2025-11-27 16:35:05
tags: [studycase, unity]
pinned: true
head:
@@ -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;
@@ -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;
@@ -156,11 +156,11 @@ namespace StudyCase2
moveDir = forward.TransformDirection(moveDir);
if (moveInput != Vector2.zero)
{
animator?.SetBool("Move", true);
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);
else animator.SetBool("Move", false);
if (!characterController.isGrounded)
moveDir.y -= gravity * Time.deltaTime;
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
@@ -173,7 +173,7 @@ namespace StudyCase2
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed && characterController.isGrounded)
if (characterController.isGrounded)
{
moveDir.y = jumpSpeed;
}
@@ -181,7 +181,7 @@ namespace StudyCase2
}
}
```
### 代码讲解
### 5.7代码讲解
在Awake中自动获取组件移除PlayerInput
```csharp
private void Awake()
@@ -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;
@@ -205,13 +205,15 @@ 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"/>
## 6.异闻录-FAQ
- **代码查找组件报错**:确认获取物体的名字和物体实际名字一致。
- **代码加载InputActionAsset报错**:确认处于`Resources`文件夹下,名字和查找名字一致。
- **移动不播放动画**:确认动画控制器的参数名字和脚本中设置的一样。
- **动画只播放一遍**:检查是否勾选循环播放,在模型上看。
- **动画状态切换有延迟**确认动画控制器中连接线的HasExitTime是否勾选。
- **动画状态切换有延迟**:确认动画控制器中连接线的`HasExitTime`是否勾选。
完成以上步骤,即可得到一个带动画的第三人称控制器。[项目地址](http://home.gtuantuan.online:8300/TuanTuan/StudyCase)

View File

@@ -1,6 +1,6 @@
---
title: StudyCase3
date: 2025-11-26 21:33
date: 2025-11-27 16:36:05
tags: [studycase, unity]
pinned: true
head:
@@ -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"/>
@@ -115,6 +115,11 @@ namespace StudyCase3
<img src="/image/studycase3/道具挂载脚本.png" data-fancybox="gallery"/>
- 创建Pick输入事件绑定按键F
<img src="/image/studycase3/创建Pick输入事件绑定F.png" data-fancybox="gallery"/>
### 5.3 Player脚本和设置
- 修改`Assets\Scripts\StudyCase3` 下脚本 `ThirdCharacterController.cs`
```csharp
using UnityEngine;
@@ -149,7 +154,7 @@ namespace StudyCase3
public float maxDistance = 5f;
public LayerMask layerMask;
public float itemsRotateSpeed = 120f;
public Transform itemRoot;
[HideInInspector] public Transform itemRoot;
Item selectItem;
LineRenderer lineRenderer;
private void Awake()
@@ -160,7 +165,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 +173,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()
{
@@ -195,11 +206,11 @@ namespace StudyCase3
moveDir = forward.TransformDirection(moveDir);
if (moveInput != Vector2.zero)
{
animator?.SetBool("Move", true);
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);
else animator.SetBool("Move", false);
if (!characterController.isGrounded)
moveDir.y -= gravity * Time.deltaTime;
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
@@ -214,7 +225,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 +255,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;

296
posts/studycase4.md Normal file
View File

@@ -0,0 +1,296 @@
---
title: studycase4
date: 2025-11-30 12:40:05
tags: [studycase, unity]
pinned: true
head:
- - meta
- name: description
content: vitepress-theme-bluearchive studycase4
- - meta
- name: keywords
content: vitepress theme bluearchive studycase4
---
# 虚拟摇杆
- [世界构筑的起始篇章-编辑器](#_1-世界构筑的起始篇章-编辑器)
- [光芒汇聚之所-章节目标](#_2-光芒汇聚之所-章节目标)
- [光流影卷-演示视频](#_3-光流影卷-演示视频)
- [初始祭坛-前置准备](#_4-初始祭坛-前置准备)
- [勇者行迹录-章节任务](#_5-勇者行迹录-章节任务)
- [异闻录-FAQ](#_6-异闻录-faq)
## 1.世界构筑的起始篇章-编辑器
- **Unity 2022.3.62f2**
- **Visual Studio 2022**
## 2.光芒汇聚之所-章节目标
- **虚拟摇杆和按钮**
- **In game Debug**
- **打包APK**
## 3.光流影卷-演示视频
<video id="vdMain" controls muted poster="/video/studycase4/演示视频.png" playsinline>
<source id="vSource" src="/video/studycase4/演示视频.mp4" type="video/mp4" />
</video>
## 4.初始祭坛-前置准备
### 导入[资源](/resources/studycase4/case4.zip)(摇杆图标、Debug工具)
<img src="/image/studycase4/导入.png" data-fancybox="gallery"/>
### 导入InputSystem的OnScreen示例
<img src="/image/studycase4/onscreen.png" data-fancybox="gallery"/>
## 5.勇者行迹录-章节任务
### 5.1 创建StudyCase4
- 复制场景和脚本改名为studycase4
- 修改控制器和道具的命名空间
### 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"/>
- 切换到sprite(2d and ui)
<img src="/image/studycase4/切换到sprite.png" data-fancybox="gallery"/>
- 创建一个canvas再创建一个image命名为leftBgsprite使用joystick_circle_pad_a设置大小位置和锚点
<img src="/image/studycase4/创建leftBg.png" data-fancybox="gallery"/>
- 创建一个image命名为leftStick透明度0设置大小位置和锚点挂载On-Screen-Stick
<img src="/image/studycase4/创建leftStick.png" data-fancybox="gallery"/>
- 设置 `MovementRange` 为200设置 `ControlPath` 为LeftStick
<img src="/image/studycase4/设置leftStick.png" data-fancybox="gallery"/>
- 复制一个leftBg改名为rightBg设置大小位置和锚点
<img src="/image/studycase4/创建rightBg.png" data-fancybox="gallery"/>
- 复制一个leftStick改名为rightStick设置`ControlPath` 为RightStick
<img src="/image/studycase4/创建并设置rightStick.png" data-fancybox="gallery"/>
- 添加输入事件Move添加LeftStick创建Look添加Detla和RightStick
<img src="/image/studycase4/添加输入事件.gif" data-fancybox="gallery"/>
- 设置虚拟相机参数移动速度从300调到100限制角度0到70
<img src="/image/studycase4/设置虚拟相机参数.png" data-fancybox="gallery"/>
- 修改虚拟相机输入
<img src="/image/studycase4/配置虚拟相机输入.gif" data-fancybox="gallery"/>
### 5.4 Player脚本和设置
- 修改`Assets\Scripts\studycase4` 下脚本 `ThirdCharacterController.cs`
```csharp
using UnityEngine;
using UnityEngine.InputSystem;
using Cinemachine;
namespace StudyCase4
{
public enum PickMode
{
RayCast,
Mouse
}
public class ThirdCharacterController : MonoBehaviour
{
[Header("MoveSettings")]
CharacterController characterController;
InputActionAsset inputAction;
Animator animator;
Transform forward;
Transform model;
CinemachineVirtualCamera vCam;
public float moveSpeed = 5f;
public float jumpSpeed = 2f;
public float turnSpeed = 10f;
public float gravity = 10f;
Vector3 moveDir;
Vector2 moveInput;
[Header("PickSettings")]
public PickMode pickMode = PickMode.RayCast;
public CursorLockMode cursorLock;
public bool useDrawRay = true;
public float maxDistance = 5f;
public LayerMask layerMask;
public float itemsRotateSpeed = 120f;
public float minCameraDistance = 2f;
public float maxCameraDistance = 10f;
public float cameraZoomSpeed = 0.1f;
[HideInInspector] public Transform itemRoot;
Item selectItem;
LineRenderer lineRenderer;
CinemachineFramingTransposer framingTransposer;
private void Awake()
{
Cursor.lockState = cursorLock;
characterController = GetComponent<CharacterController>();
forward = transform.Find("Forward");
model = transform.Find("Model");
animator = model.GetComponentInChildren<Animator>();
vCam = transform.Find("Virtual Camera").GetComponent<CinemachineVirtualCamera>();
framingTransposer = vCam.GetCinemachineComponent<CinemachineFramingTransposer>();
inputAction = Resources.Load<InputActionAsset>("PlayerInputActions");
inputAction.FindAction("Move").started += OnMove;
inputAction.FindAction("Move").performed += OnMove;
inputAction.FindAction("Move").canceled += OnMove;
inputAction.FindAction("Jump").performed += OnJump;
inputAction.FindAction("Pick").performed += OnPickUp;
inputAction.FindAction("Zoom").started += OnZoom;
inputAction.FindAction("Zoom").performed += OnZoom;
inputAction.FindAction("Zoom").canceled += OnZoom;
inputAction.Enable();
itemRoot = transform.Find("ItemRoot");
if (lineRenderer == null)
{
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = Resources.Load<Material>("Line");
lineRenderer.useWorldSpace = true;
}
layerMask = LayerMask.GetMask("Item");
//开启多点触控
Input.multiTouchEnabled = true;
}
private void Update()
{
Move();
CheckRayHit();
ItemsRotate();
}
public void OnMove(InputAction.CallbackContext context)
{
if (characterController.isGrounded)
moveInput = context.ReadValue<Vector2>();
else moveInput = Vector2.zero;
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed && characterController.isGrounded)
{
moveDir.y = jumpSpeed;
}
}
//增加控制相机距离的方法
public void OnZoom(InputAction.CallbackContext context)
{
float scrollValue = context.ReadValue<float>();
float newDistance = framingTransposer.m_CameraDistance + (scrollValue * cameraZoomSpeed * 0.01f);
framingTransposer.m_CameraDistance = Mathf.Clamp(newDistance, minCameraDistance, maxCameraDistance);
}
void Move()
{
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);
}
#region PickUp
public void OnPickUp(InputAction.CallbackContext context)
{
if (pickMode != PickMode.RayCast) return;
if (selectItem != null)
selectItem.PickUp(itemRoot);
selectItem = null;
}
void CheckRayHit()
{
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))
{
DrawRay(model.position, hit.point, new Color(0, 1, 1));
Item curItem = hit.transform.GetComponent<Item>();
if (curItem != null && curItem != selectItem)
{
if (selectItem != null)
selectItem.UnSelect();
selectItem = curItem;
selectItem.Select();
}
}
else
{
DrawRay(model.position, model.position+ model.forward* maxDistance, new Color(1, 1, 1));
if (selectItem != null)
selectItem.UnSelect();
selectItem = null;
}
}
void DrawRay(Vector3 startPos, Vector3 endPos, Color color)
{
lineRenderer.startColor = lineRenderer.endColor = color;
lineRenderer.startWidth = lineRenderer.endWidth = 0.1f;
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, startPos);
lineRenderer.SetPosition(1, endPos);
lineRenderer.enabled = true;
}
void ItemsRotate()
{
itemRoot.Rotate(0,itemsRotateSpeed*Time.deltaTime, 0);
}
public void SetItems()
{
float radius = 1.5f;
float angleStep = 360 / itemRoot.childCount;
for (int i = 0; i < itemRoot.childCount; i++)
{
Transform item = itemRoot.GetChild(i);
float angle = angleStep * i;
Quaternion rot = Quaternion.Euler(0, angle, 0);
item.localPosition = rot * itemRoot.forward * radius;
item.localScale = Vector3.one * 0.1f;
}
}
#endregion
}
}
```
### 5.5打包并测试
- 玩家移除case3的脚本挂载case4脚本
- BuildSettings切换到Android平台
<img src="/image/studycase4/切换到Android平台.png" data-fancybox="gallery"/>
- 打包apk
<img src="/image/studycase4/打包apk.png" data-fancybox="gallery"/>
- 在手机上安装和运行
## 6.异闻录-FAQ

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: 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 912 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 394 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 696 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 682 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 515 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 432 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 538 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 591 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 728 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 307 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 303 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.

Binary file not shown.

After

Width:  |  Height:  |  Size: 574 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 471 KiB

View File

@@ -4,7 +4,8 @@
BA 主题博客,基于 VitePress绝赞画饼中
[在线预览](https://vitepress-theme-bluearchive.vercel.app/)
[模版在线预览](https://vitepress-theme-bluearchive.vercel.app/)
[个人在线预览](https://home.gtuantuan.online:9443/)
![preview](./preview.webp)
![preview_dark](./preview_dark.webp)