summaryrefslogtreecommitdiff
path: root/Assets/Scripts
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-01-30 23:07:07 +0800
committer魏曹先生 <1992414357@qq.com>2026-01-30 23:07:07 +0800
commitbdd0c3eee905c5dc6fcf22344f7fdbd4cc3e3a02 (patch)
tree2a8fb0c76b134890aa61dcc9e9949cedab055f57 /Assets/Scripts
parentb0642164b112508bbfc0b60553586ea260a79d2e (diff)
完成双角色操纵
Diffstat (limited to 'Assets/Scripts')
-rw-r--r--Assets/Scripts/DEBUG_GrabbingDisplay.cs21
-rw-r--r--Assets/Scripts/DEBUG_GrabbingDisplay.cs.meta2
-rw-r--r--Assets/Scripts/GameControllerInput.cs27
-rw-r--r--Assets/Scripts/GameControllerInput.cs.meta2
-rw-r--r--Assets/Scripts/PlayerControl.cs8
-rw-r--r--Assets/Scripts/PlayerControl.cs.meta2
-rw-r--r--Assets/Scripts/PlayerController.cs10
-rw-r--r--Assets/Scripts/PlayerMovement.cs37
-rw-r--r--Assets/Scripts/PlayerMovement.cs.meta2
-rw-r--r--Assets/Scripts/RandomSpawnPosition.cs16
-rw-r--r--Assets/Scripts/RandomSpawnPosition.cs.meta2
-rw-r--r--Assets/Scripts/SimpleControl.cs35
-rw-r--r--Assets/Scripts/SimpleControl.cs.meta2
13 files changed, 164 insertions, 2 deletions
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<TMP_Text>();
+ }
+
+ 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<PlayerControl>();
+ }
+
+ public void OnMove(InputAction.CallbackContext context)
+ {
+ Vector2 input = context.ReadValue<Vector2>();
+ _playerControl.movementHorizontal = input.x;
+ _playerControl.movementVertical = input.y;
+ }
+
+ public void OnGrab(InputAction.CallbackContext context)
+ {
+ float value = context.ReadValue<float>();
+ 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<PlayerControl>();
+ _rigidbody = GetComponent<Rigidbody>();
+ }
+
+ 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<PlayerControl>();
+ }
+
+ 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