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/DEBUG_GrabbingDisplay.cs | 21 ++++++++++++++++ Assets/Scripts/DEBUG_GrabbingDisplay.cs.meta | 2 ++ Assets/Scripts/GameControllerInput.cs | 27 ++++++++++++++++++++ Assets/Scripts/GameControllerInput.cs.meta | 2 ++ Assets/Scripts/PlayerControl.cs | 8 ++++++ Assets/Scripts/PlayerControl.cs.meta | 2 ++ Assets/Scripts/PlayerController.cs | 10 ++++++-- Assets/Scripts/PlayerMovement.cs | 37 ++++++++++++++++++++++++++++ Assets/Scripts/PlayerMovement.cs.meta | 2 ++ Assets/Scripts/RandomSpawnPosition.cs | 16 ++++++++++++ Assets/Scripts/RandomSpawnPosition.cs.meta | 2 ++ Assets/Scripts/SimpleControl.cs | 35 ++++++++++++++++++++++++++ Assets/Scripts/SimpleControl.cs.meta | 2 ++ 13 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 Assets/Scripts/DEBUG_GrabbingDisplay.cs create mode 100644 Assets/Scripts/DEBUG_GrabbingDisplay.cs.meta create mode 100644 Assets/Scripts/GameControllerInput.cs create mode 100644 Assets/Scripts/GameControllerInput.cs.meta create mode 100644 Assets/Scripts/PlayerControl.cs create mode 100644 Assets/Scripts/PlayerControl.cs.meta create mode 100644 Assets/Scripts/PlayerMovement.cs create mode 100644 Assets/Scripts/PlayerMovement.cs.meta create mode 100644 Assets/Scripts/RandomSpawnPosition.cs create mode 100644 Assets/Scripts/RandomSpawnPosition.cs.meta create mode 100644 Assets/Scripts/SimpleControl.cs create mode 100644 Assets/Scripts/SimpleControl.cs.meta (limited to 'Assets/Scripts') diff --git a/Assets/Scripts/DEBUG_GrabbingDisplay.cs b/Assets/Scripts/DEBUG_GrabbingDisplay.cs new file mode 100644 index 0000000..0bb1cf5 --- /dev/null +++ b/Assets/Scripts/DEBUG_GrabbingDisplay.cs @@ -0,0 +1,21 @@ +using System; +using TMPro; +using UnityEngine; + +[RequireComponent(typeof(TMP_Text))] +public class DebugGrabbingDisplay : MonoBehaviour +{ + public PlayerControl playerControl; + + private TMP_Text _text; + + private void Awake() + { + _text = GetComponent(); + } + + private void Update() + { + _text.text = playerControl.grabbing ? "Grabbing" : "Not Grabbing"; + } +} diff --git a/Assets/Scripts/DEBUG_GrabbingDisplay.cs.meta b/Assets/Scripts/DEBUG_GrabbingDisplay.cs.meta new file mode 100644 index 0000000..1a704d9 --- /dev/null +++ b/Assets/Scripts/DEBUG_GrabbingDisplay.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 8759766e03fde51bebac18e7eac0d1dd \ No newline at end of file diff --git a/Assets/Scripts/GameControllerInput.cs b/Assets/Scripts/GameControllerInput.cs new file mode 100644 index 0000000..ff29c12 --- /dev/null +++ b/Assets/Scripts/GameControllerInput.cs @@ -0,0 +1,27 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +[RequireComponent(typeof(PlayerControl))] +public class GameControllerInput : MonoBehaviour +{ + private PlayerControl _playerControl; + + private void Awake() + { + _playerControl = GetComponent(); + } + + public void OnMove(InputAction.CallbackContext context) + { + Vector2 input = context.ReadValue(); + _playerControl.movementHorizontal = input.x; + _playerControl.movementVertical = input.y; + } + + public void OnGrab(InputAction.CallbackContext context) + { + float value = context.ReadValue(); + bool isGrabbing = value > 0.5f; + _playerControl.grabbing = isGrabbing; + } +} diff --git a/Assets/Scripts/GameControllerInput.cs.meta b/Assets/Scripts/GameControllerInput.cs.meta new file mode 100644 index 0000000..6b0e7bf --- /dev/null +++ b/Assets/Scripts/GameControllerInput.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 58eaaa4007cbe2bc992ac217cbb4b234 \ No newline at end of file diff --git a/Assets/Scripts/PlayerControl.cs b/Assets/Scripts/PlayerControl.cs new file mode 100644 index 0000000..3ef3dc8 --- /dev/null +++ b/Assets/Scripts/PlayerControl.cs @@ -0,0 +1,8 @@ +using UnityEngine; + +public class PlayerControl : MonoBehaviour +{ + public float movementHorizontal; + public float movementVertical; + public bool grabbing; +} diff --git a/Assets/Scripts/PlayerControl.cs.meta b/Assets/Scripts/PlayerControl.cs.meta new file mode 100644 index 0000000..d53f4be --- /dev/null +++ b/Assets/Scripts/PlayerControl.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: d5e8c581f8140e7b0bec2083d8bbbbfb \ No newline at end of file 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); diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs new file mode 100644 index 0000000..90252b3 --- /dev/null +++ b/Assets/Scripts/PlayerMovement.cs @@ -0,0 +1,37 @@ +using System; +using UnityEngine; + +[RequireComponent(typeof(PlayerControl))] +[RequireComponent(typeof(Rigidbody))] +public class PlayerMovement : MonoBehaviour +{ + public float movementSpeed; + public float movementSpeedOnGrabbing; + + private PlayerControl _playerControl; + private Rigidbody _rigidbody; + + private void Awake() + { + _playerControl = GetComponent(); + _rigidbody = GetComponent(); + } + + private void FixedUpdate() + { + FixedUpdateMovement(); + } + + private void FixedUpdateMovement() + { + var velocity = _rigidbody.linearVelocity; + var speed = _playerControl.grabbing ? movementSpeedOnGrabbing : movementSpeed; + var direction = new Vector3( + _playerControl.movementHorizontal, + 0, + _playerControl.movementVertical) + .normalized; + var playerVelocity = speed * direction + velocity.y * Vector3.up; + _rigidbody.linearVelocity = playerVelocity; + } +} diff --git a/Assets/Scripts/PlayerMovement.cs.meta b/Assets/Scripts/PlayerMovement.cs.meta new file mode 100644 index 0000000..52a44ab --- /dev/null +++ b/Assets/Scripts/PlayerMovement.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 77eb8351ca7bb62868b1af2703515143 \ No newline at end of file diff --git a/Assets/Scripts/RandomSpawnPosition.cs b/Assets/Scripts/RandomSpawnPosition.cs new file mode 100644 index 0000000..cc8b92e --- /dev/null +++ b/Assets/Scripts/RandomSpawnPosition.cs @@ -0,0 +1,16 @@ +using System; +using UnityEngine; +using Random = UnityEngine.Random; + +public class RandomSpawnPosition : MonoBehaviour +{ + public float range = 3; + + private void Start() + { + var x = Random.Range(-range, range); + var z = Random.Range(-range, range); + + transform.position = new Vector3(x,transform.position.y,z); + } +} diff --git a/Assets/Scripts/RandomSpawnPosition.cs.meta b/Assets/Scripts/RandomSpawnPosition.cs.meta new file mode 100644 index 0000000..0c27b11 --- /dev/null +++ b/Assets/Scripts/RandomSpawnPosition.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 6ff1bce659fadf96baafe0e1cf9e88fb \ No newline at end of file diff --git a/Assets/Scripts/SimpleControl.cs b/Assets/Scripts/SimpleControl.cs new file mode 100644 index 0000000..fd96db1 --- /dev/null +++ b/Assets/Scripts/SimpleControl.cs @@ -0,0 +1,35 @@ +using System; +using UnityEngine; + +[RequireComponent(typeof(PlayerControl))] +public class SimpleControl : MonoBehaviour +{ + private PlayerControl _playerControl; + + [Header("Parameter")] + public KeyCode moveLeft = KeyCode.A; + public KeyCode moveRight = KeyCode.D; + public KeyCode moveUp = KeyCode.W; + public KeyCode moveDown = KeyCode.S; + public KeyCode grab = KeyCode.G; + + private float HorizontalAxis => + (Input.GetKey(moveLeft) ? -1 : 0) + + (Input.GetKey(moveRight) ? 1 : 0); + + private float VerticalAxis => + (Input.GetKey(moveDown) ? -1 : 0) + + (Input.GetKey(moveUp) ? 1 : 0); + + private void Awake() + { + _playerControl = GetComponent(); + } + + private void Update() + { + _playerControl.movementHorizontal = HorizontalAxis; + _playerControl.movementVertical = VerticalAxis; + _playerControl.grabbing = Input.GetKey(grab); + } +} diff --git a/Assets/Scripts/SimpleControl.cs.meta b/Assets/Scripts/SimpleControl.cs.meta new file mode 100644 index 0000000..aef8c2a --- /dev/null +++ b/Assets/Scripts/SimpleControl.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 257d4898da204f802b1f18ec90f3966a \ No newline at end of file -- cgit