This commit is contained in:
huyulong 2024-11-19 11:45:07 +08:00
parent 0e36c4c6d3
commit dc0b4d5f2d
3 changed files with 61 additions and 48 deletions

View File

@ -64,12 +64,13 @@ public class CameraControl : MonoBehaviour
// 获取鼠标移动来控制相机的旋转。
// 鼠标控制:
if (Input.GetMouseButton(0))
{
angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * horizontalAimingSpeed;
angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * verticalAimingSpeed;
}
//if (Input.GetMouseButton(0))
//{
// angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * horizontalAimingSpeed;
// angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * verticalAimingSpeed;
//}
// 设置垂直移动的限制。
@ -86,13 +87,13 @@ public class CameraControl : MonoBehaviour
angleH += deltaH;
}
// 处理相机的水平旋转限制(如果设置了)
// 处理相机的水平旋转限制(如果设置了)
if (forwardHorizontalRef != default(Vector3))
{
ClampHorizontal();
}
// 设置相机的方向
// 设置相机的方向
Quaternion camYRotation = Quaternion.Euler(0, angleH, 0);
Quaternion aimRotation = Quaternion.Euler(-angleV, angleH, 0);
cam.rotation = aimRotation;

View File

@ -46,15 +46,15 @@ public class GodCamera : MonoBehaviour
{
//移动
Move();
// Move();
//视野缩放
ScaleView();
///ScaleView();
}
private void Move()
{
//当鼠标左键按下的时候,0表示鼠标左键
if (Input.GetMouseButton(0))
if (Input.GetMouseButton(0))
{
var x = Input.GetAxis("Mouse X");//鼠标在两帧之间的水平偏移量
var y = Input.GetAxis("Mouse Y");//竖直的偏移量

View File

@ -2,7 +2,7 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement_Joystick : MonoBehaviour
public class PlayerMovement_Jpystick : MonoBehaviour
{
public FixedJoystick joystick; // 引用 Fixed Joystick
public Transform cameraTransform; // 引用主摄像机的 Transform
@ -14,24 +14,24 @@ public class PlayerMovement_Joystick : MonoBehaviour
public Camera mainCamera;
private Rigidbody rb;
// 定义相机 FOV 相关变量
public float normalFOV = 60f; // 正常行走时的FOV
public float sprintFOV = 120f; // 奔跑时的FOV
public float fovChangeSpeed = 2f; // FOV改变的速
public float fovChangeSpeed = 2f; // FOV改变的速
// 触摸ID变量用于区分左右区域触摸
private int leftFingerId = -1; // 左侧区域触摸ID
private int rightFingerId = -1; // 右侧区域触摸ID
private Vector2 rightTouchStartPos; // 记录右手触摸开始位置
private int leftFingerId = -1;
private int rightFingerId = -1;
private bool IsMoving = false;
private float MoveTime = 0f; // 跑步切换的时间
public float walkTime; // 走路的时间
public float runTime; // 跑的时间
private float targetFOV; // 跑步时平滑切换FOV
//跑步切换的时间
private float MoveTime = 0f;
//走路的时间
public float walkTime;
//跑的时间
public float runTime;
//跑步时平滑切换fov
private float targetFOV;
private void Start()
{
characterControl = GetComponent<CharacterControl>();
@ -39,43 +39,52 @@ public class PlayerMovement_Joystick : MonoBehaviour
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; // 左侧区域用于控制虚拟摇杆
// 左侧区域绑定左手指ID用于控制虚拟摇杆
leftFingerId = touch.fingerId;
}
else if (touch.position.x >= Screen.width / 2 && rightFingerId == -1)
{
rightFingerId = touch.fingerId; // 右侧区域用于滑动视角
rightTouchStartPos = touch.position; // 记录右手触摸起点
// 右侧区域绑定右手指ID用于滑动视角
rightFingerId = touch.fingerId;
}
}
else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
{
if (touch.fingerId == leftFingerId)
{
HandleJoystickControl(); // 左手触摸:处理角色移动
// 左侧触摸:处理角色移动
HandleJoystickControl();
}
else if (touch.fingerId == rightFingerId)
{
HandleViewSwipe(touch); // 右手触摸:滑动视角
// 右侧触摸:滑动视角
HandleViewSwipe(touch);
}
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
// 触摸结束时重置ID
if (touch.fingerId == leftFingerId)
{
leftFingerId = -1; // 触摸结束时重置左手ID
leftFingerId = -1;
}
else if (touch.fingerId == rightFingerId)
{
rightFingerId = -1; // 触摸结束时重置右手ID
rightFingerId = -1;
}
}
}
@ -83,27 +92,29 @@ public class PlayerMovement_Joystick : MonoBehaviour
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; // 摄像机的右方向
Vector3 cameraRight = cameraTransform.right; // 摄像机的右方向
// 只需要水平的移动方向去掉摄像头的y轴分量
// 由于我们只需要水平的移动方向去掉摄像头的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))
@ -111,31 +122,30 @@ public class PlayerMovement_Joystick : 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);
}
}
void HandleViewSwipe(Touch touch)
{
// 滑动视角逻辑
Vector2 swipeDelta = touch.position - rightTouchStartPos;
float horizontalSwipe = swipeDelta.x * 0.1f; // 可调整灵敏度
float verticalSwipe = -swipeDelta.y * 0.1f;
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);
// 更新右手触摸起点
rightTouchStartPos = touch.position;
}
public void MoveState()
@ -159,4 +169,6 @@ public class PlayerMovement_Joystick : MonoBehaviour
// 使用插值平滑调整相机的FOV
mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, targetFOV, fovChangeSpeed * Time.deltaTime);
}
}
}