From bdd0c3eee905c5dc6fcf22344f7fdbd4cc3e3a02 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 30 Jan 2026 23:07:07 +0800 Subject: 完成双角色操纵 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/PlayerController.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'Assets/Scripts/PlayerController.cs') 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); -- cgit