视角摇杆控制

This commit is contained in:
huyulong 2024-12-19 11:54:47 +08:00
parent 7f82478353
commit 4b79e37679
4 changed files with 3970 additions and 267 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,8 +4,10 @@ using UnityEngine;
public class CameraControl : MonoBehaviour
{
public JoystickController joystickController;
public Transform Character; // 玩家的引用。
public Vector3 pivotOffset = new Vector3(0.0f, 1.7f, 0.0f); // 相机相对于玩家的偏移量,用于调整相机的中心点。
public Vector3 pivotOffset = new Vector3(0.0f, 1.7f, 0.0f); // 相机相对于玩家的偏移量,用于调整相机的中心点。
public Vector3 camOffset = new Vector3(0.2f, 0.2f, -1f); // 相机相对于玩家位置的偏移量,用于定位相机。
public float smooth = 10f; // 相机响应的平滑速度。
public float horizontalAimingSpeed = 6f; // 水平转动速度。
@ -74,7 +76,7 @@ public class CameraControl : MonoBehaviour
angleV = Mathf.LerpAngle(angleV, angleV + recoilAngle, 10f * Time.deltaTime);
//// 处理相机方向锁定。
if (firstDirection != Vector3.zero)
if (firstDirection != Vector3.zero&& !joystickController.isDrag)
{
angleH -= deltaH;
UpdateLockAngle();

View File

@ -0,0 +1,69 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class JoystickController : MonoBehaviour, IDragHandler, IEndDragHandler
{
public bool isDrag = false;
public RectTransform joystickBackground; // 摇杆背景
public RectTransform joystick; // 摇杆
public Camera playerCamera; // 摄像机
public float rotationSpeed = 5f; // 摄像机旋转速度
private Vector2 joystickInput = Vector2.zero;
void Start()
{
// 初始化摇杆的位置
joystick.anchoredPosition = Vector2.zero;
}
// 每帧更新摇杆的输入
void Update()
{
if (joystickInput.magnitude > 0)
{
// 根据摇杆的输入控制摄像机旋转
float horizontal = joystickInput.x;
float vertical = joystickInput.y;
// 水平旋转
playerCamera.transform.Rotate(Vector3.up, horizontal * rotationSpeed * Time.deltaTime);
// 垂直旋转
playerCamera.transform.Rotate(Vector3.left, vertical * rotationSpeed * Time.deltaTime);
}
}
// 摇杆拖动时更新摇杆位置和输入
public void OnDrag(PointerEventData eventData)
{
isDrag = true;
// 更新摇杆的位置
joystick.anchoredPosition = joystickInput;
Vector2 localPoint;
// 将触摸位置从屏幕坐标转换为UI本地坐标
RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBackground, eventData.position, eventData.pressEventCamera, out localPoint);
// 计算摇杆的输入方向
joystickInput = localPoint.normalized;
// 限制摇杆的移动范围,确保它不会超出背景的边界
if (localPoint.magnitude > joystickBackground.sizeDelta.x / 2)
{
joystickInput = localPoint.normalized * (joystickBackground.sizeDelta.x / 2);
}
}
// 摇杆松开时重置摇杆位置
public void OnEndDrag(PointerEventData eventData)
{
isDrag = false;
joystick.anchoredPosition = Vector2.zero;
joystickInput = Vector2.zero;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4e3938570b1787843b6fe76c1c2b9651
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: