This commit is contained in:
hyskai 2024-11-12 20:07:19 +08:00
parent 2b5f9b33c5
commit 6eb2c4375c

View File

@ -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()