From 6eb2c4375c3cdc7d22cb6c68412403c87e00b15d Mon Sep 17 00:00:00 2001 From: hyskai <2545507770@qq.com> Date: Tue, 12 Nov 2024 20:07:19 +0800 Subject: [PATCH] add --- .../Character/PlayerMovement_Jpystick.cs | 87 ++++++++++++++++--- 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/xiaofang/Assets/Script/Character/PlayerMovement_Jpystick.cs b/xiaofang/Assets/Script/Character/PlayerMovement_Jpystick.cs index cb9d39fe..1f2de9ef 100644 --- a/xiaofang/Assets/Script/Character/PlayerMovement_Jpystick.cs +++ b/xiaofang/Assets/Script/Character/PlayerMovement_Jpystick.cs @@ -19,6 +19,10 @@ public class PlayerMovement_Jpystick : MonoBehaviour public float sprintFOV = 120f; // 奔跑时的FOV public float fovChangeSpeed = 2f; // FOV改变的速 + // 触摸ID变量,用于区分左右区域触摸 + private int leftFingerId = -1; + private int rightFingerId = -1; + private bool IsMoving = false; //跑步切换的时间 private float MoveTime = 0f; @@ -39,6 +43,56 @@ public class PlayerMovement_Jpystick : MonoBehaviour void Update() { + + + // 检测屏幕上所有触摸点 + foreach (Touch touch in Input.touches) + { + // 触摸开始时,分配触摸区域 + if (touch.phase == TouchPhase.Began) + { + if (touch.position.x < Screen.width / 2 && leftFingerId == -1) + { + // 左侧区域,绑定左手指ID,用于控制虚拟摇杆 + leftFingerId = touch.fingerId; + } + else if (touch.position.x >= Screen.width / 2 && rightFingerId == -1) + { + // 右侧区域,绑定右手指ID,用于滑动视角 + rightFingerId = touch.fingerId; + } + } + else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) + { + if (touch.fingerId == leftFingerId) + { + // 左侧触摸:处理角色移动 + HandleJoystickControl(); + } + else if (touch.fingerId == rightFingerId) + { + // 右侧触摸:滑动视角 + HandleViewSwipe(touch); + } + } + else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) + { + // 触摸结束时,重置ID + if (touch.fingerId == leftFingerId) + { + leftFingerId = -1; + } + else if (touch.fingerId == rightFingerId) + { + rightFingerId = -1; + } + } + } + } + + void HandleJoystickControl() + { + // 1. 获取摇杆输入 float horizontal = joystick.Horizontal; float vertical = joystick.Vertical; @@ -68,21 +122,30 @@ public class PlayerMovement_Jpystick : MonoBehaviour MoveTime += Time.deltaTime; } } - - - - // 5. 应用移动 if (moveDirection.magnitude > 0.1f) - { - // 使用速度移动角色 - Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime; - rb.MovePosition(newPosition); - MoveState(); - // 使角色面朝移动方向 - Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up); - transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 720 * Time.deltaTime); + { + // 使用速度移动角色 + Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime; + rb.MovePosition(newPosition); + MoveState(); + // 使角色面朝移动方向 + Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up); + transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 720 * Time.deltaTime); } + + + + } + + void HandleViewSwipe(Touch touch) + { + // 滑动视角逻辑 + float horizontalSwipe = touch.deltaPosition.x * 0.1f; // 可调整灵敏度 + float verticalSwipe = -touch.deltaPosition.y * 0.1f; + + cameraTransform.Rotate(0, horizontalSwipe, 0, Space.World); + cameraTransform.Rotate(verticalSwipe, 0, 0, Space.Self); } public void MoveState()