_xiaofang/xiaofang/Assets/Script/hylScripts/JoystickController.cs

89 lines
3.0 KiB
C#
Raw Normal View History

2024-12-19 11:54:47 +08:00
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; // ҡ<>˱<EFBFBD><CBB1><EFBFBD>
public RectTransform joystick; // ҡ<><D2A1>
public Camera playerCamera; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public float rotationSpeed = 0.5f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>ٶ<EFBFBD>
// <20><>ת<EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD><EFBFBD>
public float horizontalMinRotation = -75f; // ˮƽ<CBAE><C6BD>ת<EFBFBD><D7AA>Сֵ
public float horizontalMaxRotation = 75f; // ˮƽ<CBAE><C6BD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ֵ
public float verticalMinRotation = -75f; // <20><>ֱ<EFBFBD><D6B1>ת<EFBFBD><D7AA>Сֵ
public float verticalMaxRotation = 75f; // <20><>ֱ<EFBFBD><D6B1>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ֵ
2024-12-19 11:54:47 +08:00
private Vector2 joystickInput = Vector2.zero;
private float currentXRotation = 0f; // <20><>ǰ<EFBFBD><C7B0>ˮƽ<CBAE><C6BD>ת<EFBFBD>Ƕ<EFBFBD>
private float currentYRotation = 0f; // <20><>ǰ<EFBFBD>Ĵ<EFBFBD>ֱ<EFBFBD><D6B1>ת<EFBFBD>Ƕ<EFBFBD>
private float targetXRotation = 0f; // Ŀ<><C4BF>ˮƽ<CBAE><C6BD>ת<EFBFBD>Ƕ<EFBFBD>
private float targetYRotation = 0f; // Ŀ<>괹ֱ<EAB4B9><D6B1>ת<EFBFBD>Ƕ<EFBFBD>
2024-12-19 11:54:47 +08:00
void Start()
{
// <20><>ʼ<EFBFBD><CABC>ҡ<EFBFBD>˵<EFBFBD>λ<EFBFBD><CEBB>
joystick.anchoredPosition = Vector2.zero;
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>Ƕ<EFBFBD>
targetXRotation = playerCamera.transform.eulerAngles.y;
targetYRotation = playerCamera.transform.eulerAngles.x;
2024-12-19 11:54:47 +08:00
}
// ÿ֡<C3BF><D6A1><EFBFBD><EFBFBD>ҡ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>
void Update()
{
if (joystickInput.magnitude > 0)
{
// <20><><EFBFBD><EFBFBD>ҡ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
targetXRotation += joystickInput.x * rotationSpeed * Time.deltaTime;
targetYRotation -= joystickInput.y * rotationSpeed * Time.deltaTime;
2024-12-19 11:54:47 +08:00
// <20><><EFBFBD><EFBFBD>ˮƽ<CBAE><C6BD>ת<EFBFBD><D7AA>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ͷ
targetXRotation = Mathf.Clamp(targetXRotation, horizontalMinRotation, horizontalMaxRotation);
2024-12-19 11:54:47 +08:00
// <20><><EFBFBD>ƴ<EFBFBD>ֱ<EFBFBD><D6B1>ת<EFBFBD><D7AA>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
targetYRotation = Mathf.Clamp(targetYRotation, verticalMinRotation, verticalMaxRotation);
// ʹ<><CAB9> Quaternion <20><>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>ת<EFBFBD>Ƕ<EFBFBD>
playerCamera.transform.localRotation = Quaternion.Euler(targetYRotation, targetXRotation, 0f);
2024-12-19 11:54:47 +08:00
}
}
// ҡ<><D2A1><EFBFBD>϶<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ҡ<EFBFBD><D2A1>λ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD>
public void OnDrag(PointerEventData eventData)
{
Vector2 localPoint;
2024-12-19 11:54:47 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ô<EFBFBD><C3B4><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ΪUI<55><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBackground, eventData.position, eventData.pressEventCamera, out localPoint);
2024-12-19 11:54:47 +08:00
// <20><><EFBFBD><EFBFBD>ҡ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EBB7BD>
joystickInput = localPoint.normalized;
// <20><><EFBFBD><EFBFBD>ҡ<EFBFBD>˵<EFBFBD><CBB5>ƶ<EFBFBD><C6B6><EFBFBD>Χ<EFBFBD><CEA7>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı߽<C4B1>
if (localPoint.magnitude > joystickBackground.sizeDelta.x / 2)
{
joystickInput = localPoint.normalized * (joystickBackground.sizeDelta.x / 2);
}
// <20><><EFBFBD><EFBFBD>ҡ<EFBFBD>˵<EFBFBD>λ<EFBFBD><CEBB>
joystick.anchoredPosition = joystickInput;
2024-12-19 11:54:47 +08:00
}
// ҡ<><D2A1><EFBFBD>ɿ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ҡ<EFBFBD><D2A1>λ<EFBFBD><CEBB>
public void OnEndDrag(PointerEventData eventData)
{
isDrag = false;
// ҡ<><D2A1><EFBFBD>ɿ<EFBFBD><C9BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
2024-12-19 11:54:47 +08:00
joystick.anchoredPosition = Vector2.zero;
joystickInput = Vector2.zero;
}
}