_xiaofang/xiaofang/Assets/Script/Character/PlayerMovement_Jpystick.cs
2024-11-12 20:07:19 +08:00

175 lines
5.3 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_Jpystick : 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;
private int rightFingerId = -1;
private bool IsMoving = false;
//跑步切换的时间
private float MoveTime = 0f;
//走路的时间
public float walkTime;
//跑的时间
public float runTime;
//跑步时平滑切换fov
private float targetFOV;
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)
{
// 左侧区域绑定左手指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;
// 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.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()
{
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);
}
}