diff --git a/posts/studycase1.md b/posts/studycase1.md index c3a6d2b..d5cc249 100644 --- a/posts/studycase1.md +++ b/posts/studycase1.md @@ -91,7 +91,7 @@ head: ### 创建 InputActions -- 在项目窗口右键 `Create > Input Actions`,命名为 `PlayerInputActions` +- 创建`Resources`文件夹,右键 `Create > Input Actions`,,命名为 `PlayerInputActions` diff --git a/posts/studycase2.md b/posts/studycase2.md index f37c02e..a71497e 100644 --- a/posts/studycase2.md +++ b/posts/studycase2.md @@ -108,9 +108,8 @@ head: ### 编写脚本`ThirdCharacterController.cs` -- 在 `Assets\Scripts\StudyCase2` 下创建脚本 `ThirdCharacterController` +- 修改`Assets\Scripts\StudyCase2` 下脚本 `ThirdCharacterController` ```csharp -using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; @@ -118,17 +117,32 @@ namespace StudyCase2 { public class ThirdCharacterController : MonoBehaviour { - public CharacterController characterController; - public Animator animator; - public Transform forward; - public Transform model; - public Cinemachine.CinemachineVirtualCamera vCam; + CharacterController characterController; + InputActionAsset inputAction; + Animator animator; + Transform forward; + Transform model; + Cinemachine.CinemachineVirtualCamera vCam; public float moveSpeed = 5f; public float jumpSpeed = 2f; public float turnSpeed = 10f; public float gravity = 10f; Vector3 moveDir; Vector2 moveInput; + private void Awake() + { + characterController = GetComponent(); + forward = transform.Find("Forward"); + model = transform.Find("Model"); + animator = model.GetComponentInChildren(); + vCam = transform.Find("Virtual Camera").GetComponent(); + inputAction = Resources.Load("Player"); + inputAction.FindAction("Move").started += OnMove; + inputAction.FindAction("Move").performed += OnMove; + inputAction.FindAction("Move").canceled += OnMove; + inputAction.FindAction("Jump").performed += OnJump; + inputAction.Enable(); + } private void Update() { moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y); @@ -162,9 +176,22 @@ namespace StudyCase2 } ``` ### 代码讲解 +在Awake中自动获取组件,移除PlayerInput ```csharp - //获取动画控制器 - public Animator animator; +private void Awake() +{ + characterController = GetComponent(); + forward = transform.Find("Forward"); + model = transform.Find("Model"); + animator = model.GetComponentInChildren(); + vCam = transform.Find("Virtual Camera").GetComponent(); + inputAction = Resources.Load("Player"); + inputAction.FindAction("Move").started += OnMove; + inputAction.FindAction("Move").performed += OnMove; + inputAction.FindAction("Move").canceled += OnMove; + inputAction.FindAction("Jump").performed += OnJump; + inputAction.Enable(); +} ``` ```csharp //设置动画控制器参数 @@ -173,6 +200,7 @@ namespace StudyCase2 animator.SetBool("Move", true); ``` ### Player节点设置 -- 新增 `Animator` ,其他同StudyCase1 +挂载studycase2的控制器,移除PlayerInput组件 + 完成以上步骤,即可得到一个带动画的第三人称控制器。[项目地址](http://home.gtuantuan.online:8300/TuanTuan/StudyCase) \ No newline at end of file diff --git a/public/image/studycase2/重载控制器.png b/public/image/studycase2/重载控制器.png new file mode 100644 index 0000000..f2209cd Binary files /dev/null and b/public/image/studycase2/重载控制器.png differ