using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class PlayerMovement_Jpystick : MonoBehaviour, IDragHandler { public FixedJoystick joystick; // 引用 Fixed Joystick public Transform cameraTransform; // 引用主摄像机的 Transform public float moveSpeed = 5f; public CharacterControl characterControl; private CharacterAin Characterain; public Camera mainCamera; public Rigidbody rb; // 定义相机 FOV 相关变量 public float normalFOV = 60f; // 正常行走时的FOV 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; //走路的时间 public float walkTime; //跑的时间 public float runTime; //跑步时平滑切换fov private float targetFOV; public CameraControl cameraControl; private void Start() { characterControl = GetComponent(); Characterain = GetComponent(); //rb = GetComponent(); Debug.Log("------------------"+rb); } public void OnDrag(PointerEventData eventData) { Debug.Log("=-=-----"); } public void HandleJoystickControl(float horizontal, float vertical) { // 2. 转换为三维方向向量 (保持在水平面) //Vector3 inputDirection = new Vector3(horizontal, 0f, vertical); // 3. 获取移动方向相对于摄像机的世界方向 Vector3 cameraForward = cameraTransform.forward; // 摄像机的前向方向 Vector3 cameraRight = cameraTransform.right; // 摄像机的右方向 // 由于我们只需要水平的移动方向,去掉摄像头的y轴分量 cameraForward.y = 0; cameraRight.y = 0; cameraForward.Normalize(); cameraRight.Normalize(); // 4. 计算最终的移动方向 (相对于摄像机的前后左右) Vector3 moveDirection = (cameraRight * horizontal + cameraForward * vertical).normalized; if (Mathf.Abs(vertical) > 0.01f || Mathf.Abs(horizontal) > 0.01f) { if (MoveTime < (walkTime + runTime + 0.1f)) { MoveTime += Time.deltaTime; } } // 5. 应用移动 if (moveDirection.magnitude > 0.1f) { // 使用速度移动角色 Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.deltaTime; rb.MovePosition(newPosition); MoveState(); // 使角色面朝移动方向 Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up); transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 720 * Time.deltaTime); } } public void HandleViewSwipe() { cameraControl.CamerMove(); // // 滑动视角逻辑 // 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() { if (MoveTime < walkTime) // 慢走 { Characterain.SetPlayerState(CharacterState.SlowWalk); targetFOV = normalFOV; // 设置目标FOV为正常值 } else if (MoveTime < (walkTime + runTime)) // 普通行走 { Characterain.SetPlayerState(CharacterState.walk); targetFOV = normalFOV + 30f; // 设置目标FOV为增加值 } else // 奔跑 { Characterain.SetPlayerState(CharacterState.run); targetFOV = sprintFOV; // 设置目标FOV为奔跑值 } // 使用插值平滑调整相机的FOV mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, targetFOV, fovChangeSpeed * Time.deltaTime); } }