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

113 lines
4.1 KiB
C#
Raw Normal View History

2024-12-19 11:54:47 +08:00
using System.Collections;
using System.Collections.Generic;
2024-12-24 22:01:21 +08:00
using JetBrains.Annotations;
2024-12-19 11:54:47 +08:00
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>
2024-12-24 22:01:21 +08:00
public Transform target;
// <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
2024-12-25 17:06:23 +08:00
public Vector2 joystickInput = Vector2.zero;
2024-12-19 11:54:47 +08:00
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)
{
2024-12-24 22:01:21 +08:00
2024-12-25 17:06:23 +08:00
2024-12-19 11:54:47 +08:00
// <20><><EFBFBD><EFBFBD>ҡ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
2024-12-25 17:06:23 +08:00
targetXRotation = joystickInput.x * rotationSpeed * Time.deltaTime;
targetYRotation = joystickInput.y * rotationSpeed * Time.deltaTime;
Vector3 currentRotation = playerCamera.transform.eulerAngles;
Debug.Log(currentRotation);
2024-12-19 11:54:47 +08:00
2024-12-25 17:06:23 +08:00
float yRotation = currentRotation.y;
if (yRotation > 90f) targetYRotation = 0f;
if (yRotation < -90f) targetYRotation = 0f;
//// <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);
//// <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);
2024-12-25 17:06:23 +08:00
playerCamera.transform.RotateAround(target.transform.position, playerCamera.transform.right, targetYRotation);
playerCamera.transform.RotateAround(target.transform.position, Vector3.up, targetXRotation);
targetXRotation = playerCamera.transform.eulerAngles.y;
targetYRotation = playerCamera.transform.eulerAngles.x;
2024-12-25 17:06:23 +08:00
playerCamera.transform.SetParent(target);
//playerCamera.transform.LookAt(new Vector3(target.transform.position.x,playerCamera.transform.position.y,target.transform.position.z));
// ʹ<><CAB9> Quaternion <20><>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>ת<EFBFBD>Ƕ<EFBFBD>
//playerCamera.transform.localRotation = Quaternion.Euler(targetYRotation, targetXRotation, 0f);
}
else
{
2024-12-25 17:06:23 +08:00
playerCamera.transform.SetParent(target.parent);
targetXRotation = 0;
targetYRotation = 0;
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;
}
}