diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-01-30 23:07:07 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-01-30 23:07:07 +0800 |
| commit | bdd0c3eee905c5dc6fcf22344f7fdbd4cc3e3a02 (patch) | |
| tree | 2a8fb0c76b134890aa61dcc9e9949cedab055f57 /Assets/Scripts/PlayerController.cs | |
| parent | b0642164b112508bbfc0b60553586ea260a79d2e (diff) | |
完成双角色操纵
Diffstat (limited to 'Assets/Scripts/PlayerController.cs')
| -rw-r--r-- | Assets/Scripts/PlayerController.cs | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 10bde44..c107e19 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -12,6 +12,8 @@ public class PlayerMove3D : MonoBehaviour [Tooltip("是否使用物理系统(Rigidbody)移动")] public bool useRigidbody = true; + public bool player2; + // 3D物理组件引用 private Rigidbody rb; @@ -56,8 +58,12 @@ public class PlayerMove3D : MonoBehaviour private void MoveByTransform() { // 1. 获取Input Manager的水平/垂直输入(返回值范围:-1 ~ 1) - float horizontalInput = Input.GetAxis("Horizontal"); // 左右:A/D 或 左/右方向键 - float verticalInput = Input.GetAxis("Vertical"); // 前后:W/S 或 上/下方向键 + float horizontalInput = player2 ? + (Input.GetKey(KeyCode.LeftArrow) ? -1 : 0) + (Input.GetKey(KeyCode.RightArrow) ? 1 : 0) : + (Input.GetKey(KeyCode.A) ? -1 : 0) + (Input.GetKey(KeyCode.D) ? 1 : 0); + float verticalInput = player2 ? + (Input.GetKey(KeyCode.DownArrow) ? -1 : 0) + (Input.GetKey(KeyCode.UpArrow) ? 1 : 0) : + (Input.GetKey(KeyCode.S) ? -1 : 0) + (Input.GetKey(KeyCode.W) ? 1 : 0); // 2. 构建移动方向向量(基于世界坐标系的X/Z平面,Y轴不变避免上下移动) Vector3 moveDirection = new Vector3(horizontalInput, 0f, verticalInput); |
