_xiaofang/xiaofang/Assets/Script/Character/PlayerMovement_Jpystick.cs
2024-11-19 11:18:41 +08:00

162 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement_Joystick : MonoBehaviour
{
public FixedJoystick joystick; // 引用 Fixed Joystick
public Transform cameraTransform; // 引用主摄像机的 Transform
public float moveSpeed = 5f;
public CharacterControl characterControl;
private CharacterAin Characterain;
public Camera mainCamera;
private Rigidbody rb;
// 定义相机 FOV 相关变量
public float normalFOV = 60f; // 正常行走时的FOV
public float sprintFOV = 120f; // 奔跑时的FOV
public float fovChangeSpeed = 2f; // FOV改变的速度
// 触摸ID变量用于区分左右区域触摸
private int leftFingerId = -1; // 左侧区域触摸ID
private int rightFingerId = -1; // 右侧区域触摸ID
private Vector2 rightTouchStartPos; // 记录右手触摸开始位置
private bool IsMoving = false;
private float MoveTime = 0f; // 跑步切换的时间
public float walkTime; // 走路的时间
public float runTime; // 跑的时间
private float targetFOV; // 跑步时平滑切换FOV
private void Start()
{
characterControl = GetComponent<CharacterControl>();
Characterain = GetComponent<CharacterAin>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
foreach (Touch touch in Input.touches)
{
// 触摸开始时分配触摸区域
if (touch.phase == TouchPhase.Began)
{
if (touch.position.x < Screen.width / 2 && leftFingerId == -1)
{
leftFingerId = touch.fingerId; // 左侧区域用于控制虚拟摇杆
}
else if (touch.position.x >= Screen.width / 2 && rightFingerId == -1)
{
rightFingerId = touch.fingerId; // 右侧区域用于滑动视角
rightTouchStartPos = touch.position; // 记录右手触摸起点
}
}
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)
{
if (touch.fingerId == leftFingerId)
{
leftFingerId = -1; // 触摸结束时重置左手ID
}
else if (touch.fingerId == rightFingerId)
{
rightFingerId = -1; // 触摸结束时重置右手ID
}
}
}
}
void HandleJoystickControl()
{
// 获取摇杆输入
float horizontal = joystick.Horizontal;
float vertical = joystick.Vertical;
// 转换为三维方向向量 ( 保持在水平面 )
Vector3 inputDirection = new Vector3(horizontal, 0f, vertical);
// 获取移动方向相对于摄像机的世界方向
Vector3 cameraForward = cameraTransform.forward; // 摄像机的前向方向
Vector3 cameraRight = cameraTransform.right; // 摄像机的右方向
// 只需要水平的移动方向去掉摄像头的y轴分量
cameraForward.y = 0;
cameraRight.y = 0;
cameraForward.Normalize();
cameraRight.Normalize();
// 计算最终的移动方向 ( 相对于摄像机的前后左右 )
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;
}
}
// 应用移动
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)
{
// 滑动视角逻辑
Vector2 swipeDelta = touch.position - rightTouchStartPos;
float horizontalSwipe = swipeDelta.x * 0.1f; // 可调整灵敏度
float verticalSwipe = -swipeDelta.y * 0.1f;
cameraTransform.Rotate(0, horizontalSwipe, 0, Space.World);
cameraTransform.Rotate(verticalSwipe, 0, 0, Space.Self);
// 更新右手触摸起点
rightTouchStartPos = touch.position;
}
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);
}
}