case2修改
This commit is contained in:
@@ -91,7 +91,7 @@ head:
|
|||||||
<img src="/image/studycase1/配置虚拟相机.png" data-fancybox="gallery"/>
|
<img src="/image/studycase1/配置虚拟相机.png" data-fancybox="gallery"/>
|
||||||
|
|
||||||
### 创建 InputActions
|
### 创建 InputActions
|
||||||
- 在项目窗口右键 `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"/>
|
||||||
|
|
||||||
|
|||||||
@@ -108,9 +108,8 @@ head:
|
|||||||
<img src="/image/studycase2/设置move end到moving的条件.png" data-fancybox="gallery"/>
|
<img src="/image/studycase2/设置move end到moving的条件.png" data-fancybox="gallery"/>
|
||||||
|
|
||||||
### 编写脚本`ThirdCharacterController.cs`
|
### 编写脚本`ThirdCharacterController.cs`
|
||||||
- 在 `Assets\Scripts\StudyCase2` 下创建脚本 `ThirdCharacterController`
|
- 修改`Assets\Scripts\StudyCase2` 下脚本 `ThirdCharacterController`
|
||||||
```csharp
|
```csharp
|
||||||
using System.Collections.Generic;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.InputSystem;
|
using UnityEngine.InputSystem;
|
||||||
|
|
||||||
@@ -118,17 +117,32 @@ namespace StudyCase2
|
|||||||
{
|
{
|
||||||
public class ThirdCharacterController : MonoBehaviour
|
public class ThirdCharacterController : MonoBehaviour
|
||||||
{
|
{
|
||||||
public CharacterController characterController;
|
CharacterController characterController;
|
||||||
public Animator animator;
|
InputActionAsset inputAction;
|
||||||
public Transform forward;
|
Animator animator;
|
||||||
public Transform model;
|
Transform forward;
|
||||||
public Cinemachine.CinemachineVirtualCamera vCam;
|
Transform model;
|
||||||
|
Cinemachine.CinemachineVirtualCamera vCam;
|
||||||
public float moveSpeed = 5f;
|
public float moveSpeed = 5f;
|
||||||
public float jumpSpeed = 2f;
|
public float jumpSpeed = 2f;
|
||||||
public float turnSpeed = 10f;
|
public float turnSpeed = 10f;
|
||||||
public float gravity = 10f;
|
public float gravity = 10f;
|
||||||
Vector3 moveDir;
|
Vector3 moveDir;
|
||||||
Vector2 moveInput;
|
Vector2 moveInput;
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
characterController = GetComponent<CharacterController>();
|
||||||
|
forward = transform.Find("Forward");
|
||||||
|
model = transform.Find("Model");
|
||||||
|
animator = model.GetComponentInChildren<Animator>();
|
||||||
|
vCam = transform.Find("Virtual Camera").GetComponent<Cinemachine.CinemachineVirtualCamera>();
|
||||||
|
inputAction = Resources.Load<InputActionAsset>("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()
|
private void Update()
|
||||||
{
|
{
|
||||||
moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
|
moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
|
||||||
@@ -162,9 +176,22 @@ namespace StudyCase2
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
### 代码讲解
|
### 代码讲解
|
||||||
|
在Awake中自动获取组件,移除PlayerInput
|
||||||
```csharp
|
```csharp
|
||||||
//获取动画控制器
|
private void Awake()
|
||||||
public Animator animator;
|
{
|
||||||
|
characterController = GetComponent<CharacterController>();
|
||||||
|
forward = transform.Find("Forward");
|
||||||
|
model = transform.Find("Model");
|
||||||
|
animator = model.GetComponentInChildren<Animator>();
|
||||||
|
vCam = transform.Find("Virtual Camera").GetComponent<Cinemachine.CinemachineVirtualCamera>();
|
||||||
|
inputAction = Resources.Load<InputActionAsset>("Player");
|
||||||
|
inputAction.FindAction("Move").started += OnMove;
|
||||||
|
inputAction.FindAction("Move").performed += OnMove;
|
||||||
|
inputAction.FindAction("Move").canceled += OnMove;
|
||||||
|
inputAction.FindAction("Jump").performed += OnJump;
|
||||||
|
inputAction.Enable();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
```csharp
|
```csharp
|
||||||
//设置动画控制器参数
|
//设置动画控制器参数
|
||||||
@@ -173,6 +200,7 @@ namespace StudyCase2
|
|||||||
animator.SetBool("Move", true);
|
animator.SetBool("Move", true);
|
||||||
```
|
```
|
||||||
### Player节点设置
|
### Player节点设置
|
||||||
- 新增 `Animator` ,其他同StudyCase1
|
挂载studycase2的控制器,移除PlayerInput组件
|
||||||
|
<img src="/image/studycase2/重载控制器.png" data-fancybox="gallery"/>
|
||||||
|
|
||||||
完成以上步骤,即可得到一个带动画的第三人称控制器。[项目地址](http://home.gtuantuan.online:8300/TuanTuan/StudyCase)
|
完成以上步骤,即可得到一个带动画的第三人称控制器。[项目地址](http://home.gtuantuan.online:8300/TuanTuan/StudyCase)
|
||||||
BIN
public/image/studycase2/重载控制器.png
Normal file
BIN
public/image/studycase2/重载控制器.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 888 KiB |
Reference in New Issue
Block a user