摇杆
This commit is contained in:
parent
0e36c4c6d3
commit
dc0b4d5f2d
@ -64,12 +64,13 @@ public class CameraControl : MonoBehaviour
|
|||||||
// 获取鼠标移动来控制相机的旋转。
|
// 获取鼠标移动来控制相机的旋转。
|
||||||
// 鼠标控制:
|
// 鼠标控制:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (Input.GetMouseButton(0))
|
//if (Input.GetMouseButton(0))
|
||||||
{
|
//{
|
||||||
angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * horizontalAimingSpeed;
|
// angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * horizontalAimingSpeed;
|
||||||
angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * verticalAimingSpeed;
|
// angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * verticalAimingSpeed;
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
// 设置垂直移动的限制。
|
// 设置垂直移动的限制。
|
||||||
@ -86,13 +87,13 @@ public class CameraControl : MonoBehaviour
|
|||||||
angleH += deltaH;
|
angleH += deltaH;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理相机的水平旋转限制(如果设置了)。
|
// 处理相机的水平旋转限制(如果设置了)
|
||||||
if (forwardHorizontalRef != default(Vector3))
|
if (forwardHorizontalRef != default(Vector3))
|
||||||
{
|
{
|
||||||
ClampHorizontal();
|
ClampHorizontal();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置相机的方向。
|
// 设置相机的方向
|
||||||
Quaternion camYRotation = Quaternion.Euler(0, angleH, 0);
|
Quaternion camYRotation = Quaternion.Euler(0, angleH, 0);
|
||||||
Quaternion aimRotation = Quaternion.Euler(-angleV, angleH, 0);
|
Quaternion aimRotation = Quaternion.Euler(-angleV, angleH, 0);
|
||||||
cam.rotation = aimRotation;
|
cam.rotation = aimRotation;
|
||||||
|
@ -46,15 +46,15 @@ public class GodCamera : MonoBehaviour
|
|||||||
{
|
{
|
||||||
|
|
||||||
//移动
|
//移动
|
||||||
Move();
|
// Move();
|
||||||
//视野缩放
|
//视野缩放
|
||||||
ScaleView();
|
///ScaleView();
|
||||||
|
|
||||||
}
|
}
|
||||||
private void Move()
|
private void Move()
|
||||||
{
|
{
|
||||||
//当鼠标左键按下的时候,0表示鼠标左键
|
//当鼠标左键按下的时候,0表示鼠标左键
|
||||||
if (Input.GetMouseButton(0))
|
if (Input.GetMouseButton(0))
|
||||||
{
|
{
|
||||||
var x = Input.GetAxis("Mouse X");//鼠标在两帧之间的水平偏移量
|
var x = Input.GetAxis("Mouse X");//鼠标在两帧之间的水平偏移量
|
||||||
var y = Input.GetAxis("Mouse Y");//竖直的偏移量
|
var y = Input.GetAxis("Mouse Y");//竖直的偏移量
|
||||||
|
@ -2,7 +2,7 @@ using System.Collections;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class PlayerMovement_Joystick : MonoBehaviour
|
public class PlayerMovement_Jpystick : MonoBehaviour
|
||||||
{
|
{
|
||||||
public FixedJoystick joystick; // 引用 Fixed Joystick
|
public FixedJoystick joystick; // 引用 Fixed Joystick
|
||||||
public Transform cameraTransform; // 引用主摄像机的 Transform
|
public Transform cameraTransform; // 引用主摄像机的 Transform
|
||||||
@ -14,24 +14,24 @@ public class PlayerMovement_Joystick : MonoBehaviour
|
|||||||
public Camera mainCamera;
|
public Camera mainCamera;
|
||||||
|
|
||||||
private Rigidbody rb;
|
private Rigidbody rb;
|
||||||
|
|
||||||
// 定义相机 FOV 相关变量
|
// 定义相机 FOV 相关变量
|
||||||
public float normalFOV = 60f; // 正常行走时的FOV
|
public float normalFOV = 60f; // 正常行走时的FOV
|
||||||
public float sprintFOV = 120f; // 奔跑时的FOV
|
public float sprintFOV = 120f; // 奔跑时的FOV
|
||||||
public float fovChangeSpeed = 2f; // FOV改变的速度
|
public float fovChangeSpeed = 2f; // FOV改变的速
|
||||||
|
|
||||||
// 触摸ID变量,用于区分左右区域触摸
|
// 触摸ID变量,用于区分左右区域触摸
|
||||||
private int leftFingerId = -1; // 左侧区域触摸ID
|
private int leftFingerId = -1;
|
||||||
private int rightFingerId = -1; // 右侧区域触摸ID
|
private int rightFingerId = -1;
|
||||||
|
|
||||||
private Vector2 rightTouchStartPos; // 记录右手触摸开始位置
|
|
||||||
|
|
||||||
private bool IsMoving = false;
|
private bool IsMoving = false;
|
||||||
private float MoveTime = 0f; // 跑步切换的时间
|
//跑步切换的时间
|
||||||
public float walkTime; // 走路的时间
|
private float MoveTime = 0f;
|
||||||
public float runTime; // 跑的时间
|
//走路的时间
|
||||||
private float targetFOV; // 跑步时平滑切换FOV
|
public float walkTime;
|
||||||
|
//跑的时间
|
||||||
|
public float runTime;
|
||||||
|
//跑步时平滑切换fov
|
||||||
|
private float targetFOV;
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
characterControl = GetComponent<CharacterControl>();
|
characterControl = GetComponent<CharacterControl>();
|
||||||
@ -39,43 +39,52 @@ public class PlayerMovement_Joystick : MonoBehaviour
|
|||||||
rb = GetComponent<Rigidbody>();
|
rb = GetComponent<Rigidbody>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
// 检测屏幕上所有触摸点
|
||||||
foreach (Touch touch in Input.touches)
|
foreach (Touch touch in Input.touches)
|
||||||
{
|
{
|
||||||
// 触摸开始时分配触摸区域
|
// 触摸开始时,分配触摸区域
|
||||||
if (touch.phase == TouchPhase.Began)
|
if (touch.phase == TouchPhase.Began)
|
||||||
{
|
{
|
||||||
if (touch.position.x < Screen.width / 2 && leftFingerId == -1)
|
if (touch.position.x < Screen.width / 2 && leftFingerId == -1)
|
||||||
{
|
{
|
||||||
leftFingerId = touch.fingerId; // 左侧区域用于控制虚拟摇杆
|
// 左侧区域,绑定左手指ID,用于控制虚拟摇杆
|
||||||
|
leftFingerId = touch.fingerId;
|
||||||
}
|
}
|
||||||
else if (touch.position.x >= Screen.width / 2 && rightFingerId == -1)
|
else if (touch.position.x >= Screen.width / 2 && rightFingerId == -1)
|
||||||
{
|
{
|
||||||
rightFingerId = touch.fingerId; // 右侧区域用于滑动视角
|
// 右侧区域,绑定右手指ID,用于滑动视角
|
||||||
rightTouchStartPos = touch.position; // 记录右手触摸起点
|
rightFingerId = touch.fingerId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
|
else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
|
||||||
{
|
{
|
||||||
if (touch.fingerId == leftFingerId)
|
if (touch.fingerId == leftFingerId)
|
||||||
{
|
{
|
||||||
HandleJoystickControl(); // 左手触摸:处理角色移动
|
// 左侧触摸:处理角色移动
|
||||||
|
HandleJoystickControl();
|
||||||
}
|
}
|
||||||
else if (touch.fingerId == rightFingerId)
|
else if (touch.fingerId == rightFingerId)
|
||||||
{
|
{
|
||||||
HandleViewSwipe(touch); // 右手触摸:滑动视角
|
// 右侧触摸:滑动视角
|
||||||
|
HandleViewSwipe(touch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
||||||
{
|
{
|
||||||
|
// 触摸结束时,重置ID
|
||||||
if (touch.fingerId == leftFingerId)
|
if (touch.fingerId == leftFingerId)
|
||||||
{
|
{
|
||||||
leftFingerId = -1; // 触摸结束时重置左手ID
|
leftFingerId = -1;
|
||||||
}
|
}
|
||||||
else if (touch.fingerId == rightFingerId)
|
else if (touch.fingerId == rightFingerId)
|
||||||
{
|
{
|
||||||
rightFingerId = -1; // 触摸结束时重置右手ID
|
rightFingerId = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,27 +92,29 @@ public class PlayerMovement_Joystick : MonoBehaviour
|
|||||||
|
|
||||||
void HandleJoystickControl()
|
void HandleJoystickControl()
|
||||||
{
|
{
|
||||||
// 获取摇杆输入
|
|
||||||
|
// 1. 获取摇杆输入
|
||||||
float horizontal = joystick.Horizontal;
|
float horizontal = joystick.Horizontal;
|
||||||
float vertical = joystick.Vertical;
|
float vertical = joystick.Vertical;
|
||||||
|
|
||||||
// 转换为三维方向向量 ( 保持在水平面 )
|
// 2. 转换为三维方向向量 (保持在水平面)
|
||||||
Vector3 inputDirection = new Vector3(horizontal, 0f, vertical);
|
Vector3 inputDirection = new Vector3(horizontal, 0f, vertical);
|
||||||
|
|
||||||
// 获取移动方向相对于摄像机的世界方向
|
// 3. 获取移动方向相对于摄像机的世界方向
|
||||||
Vector3 cameraForward = cameraTransform.forward; // 摄像机的前向方向
|
Vector3 cameraForward = cameraTransform.forward; // 摄像机的前向方向
|
||||||
Vector3 cameraRight = cameraTransform.right; // 摄像机的右方向
|
Vector3 cameraRight = cameraTransform.right; // 摄像机的右方向
|
||||||
|
|
||||||
// 只需要水平的移动方向,去掉摄像头的y轴分量
|
// 由于我们只需要水平的移动方向,去掉摄像头的y轴分量
|
||||||
cameraForward.y = 0;
|
cameraForward.y = 0;
|
||||||
cameraRight.y = 0;
|
cameraRight.y = 0;
|
||||||
|
|
||||||
cameraForward.Normalize();
|
cameraForward.Normalize();
|
||||||
cameraRight.Normalize();
|
cameraRight.Normalize();
|
||||||
|
|
||||||
// 计算最终的移动方向 ( 相对于摄像机的前后左右 )
|
// 4. 计算最终的移动方向 (相对于摄像机的前后左右)
|
||||||
Vector3 moveDirection = (cameraRight * horizontal + cameraForward * vertical).normalized;
|
Vector3 moveDirection = (cameraRight * horizontal + cameraForward * vertical).normalized;
|
||||||
|
|
||||||
|
|
||||||
if (Mathf.Abs(vertical) > 0.01f || Mathf.Abs(horizontal) > 0.01f)
|
if (Mathf.Abs(vertical) > 0.01f || Mathf.Abs(horizontal) > 0.01f)
|
||||||
{
|
{
|
||||||
if (MoveTime < (walkTime + runTime + 0.1f))
|
if (MoveTime < (walkTime + runTime + 0.1f))
|
||||||
@ -111,31 +122,30 @@ public class PlayerMovement_Joystick : MonoBehaviour
|
|||||||
MoveTime += Time.deltaTime;
|
MoveTime += Time.deltaTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 5. 应用移动
|
||||||
// 应用移动
|
if (moveDirection.magnitude > 0.1f)
|
||||||
|
{
|
||||||
|
// 使用速度移动角色
|
||||||
Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime;
|
Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime;
|
||||||
rb.MovePosition(newPosition);
|
rb.MovePosition(newPosition);
|
||||||
MoveState();
|
MoveState();
|
||||||
|
|
||||||
// 使角色面朝移动方向
|
// 使角色面朝移动方向
|
||||||
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
|
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
|
||||||
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 720 * Time.deltaTime);
|
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 720 * Time.deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleViewSwipe(Touch touch)
|
void HandleViewSwipe(Touch touch)
|
||||||
{
|
{
|
||||||
// 滑动视角逻辑
|
// 滑动视角逻辑
|
||||||
Vector2 swipeDelta = touch.position - rightTouchStartPos;
|
float horizontalSwipe = touch.deltaPosition.x * 0.1f; // 可调整灵敏度
|
||||||
float horizontalSwipe = swipeDelta.x * 0.1f; // 可调整灵敏度
|
float verticalSwipe = -touch.deltaPosition.y * 0.1f;
|
||||||
float verticalSwipe = -swipeDelta.y * 0.1f;
|
|
||||||
|
|
||||||
cameraTransform.Rotate(0, horizontalSwipe, 0, Space.World);
|
cameraTransform.Rotate(0, horizontalSwipe, 0, Space.World);
|
||||||
cameraTransform.Rotate(verticalSwipe, 0, 0, Space.Self);
|
cameraTransform.Rotate(verticalSwipe, 0, 0, Space.Self);
|
||||||
|
|
||||||
// 更新右手触摸起点
|
|
||||||
rightTouchStartPos = touch.position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MoveState()
|
public void MoveState()
|
||||||
@ -159,4 +169,6 @@ public class PlayerMovement_Joystick : MonoBehaviour
|
|||||||
// 使用插值平滑调整相机的FOV
|
// 使用插值平滑调整相机的FOV
|
||||||
mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, targetFOV, fovChangeSpeed * Time.deltaTime);
|
mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, targetFOV, fovChangeSpeed * Time.deltaTime);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user