wh1tezkull
Enthusiast
sino dito marunong mag code sa c#? gumawa ksi ako ng larong endless runner gamit ang Unity, pano gumawa ng script sa c# para macheck ang swipe controls?
using UnityEngine;
public class SwipeManager : MonoBehaviour
{
private static bool swipeLeft, swipeRight, swipeUp, swi****wn;
private bool isSwiping = false;
private Vector2 startTouchPosition;
private float swipeThreshold = 20f;
public static bool SwipeLeft { get { return swipeLeft; } }
public static bool SwipeRight { get { return swipeRight; } }
public static bool SwipeUp { get { return swipeUp; } }
public static bool Swi****wn { get { return swi****wn; } }
private void Update()
{
swipeLeft = swipeRight = swipeUp = swi****wn = false;
#if UNITY_EDITOR
HandleMouseInput();
#endif
HandleTouchInput();
}
private void HandleMouseInput()
{
if (Input.GetMouseButtonDown(0))
{
isSwiping = true;
startTouchPosition = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
isSwiping = false;
ResetSwipe();
}
}
private void HandleTouchInput()
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
isSwiping = true;
startTouchPosition = touch.position;
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
isSwiping = false;
ResetSwipe();
}
}
}
private void ResetSwipe()
{
startTouchPosition = Vector2.zero;
}
}
SwipeLeft
, SwipeRight
, SwipeUp
, at Swi****wn
para malaman kung anong gesture ang ginawa ng user. Pwede itong gamitin sa ibang mga script para i-check ang mga gestures.using UnityEngine;
public class PlayerController : MonoBehaviour
{
private void Update()
{
if (SwipeManager.SwipeLeft)
{
// Logic para sa pag-swipe pa-kaliwa
}
else if (SwipeManager.SwipeRight)
{
// Logic para sa pag-swipe pa-kanan
}
else if (SwipeManager.SwipeUp)
{
// Logic para sa pag-swipe pa-itaas
}
else if (SwipeManager.Swi****wn)
{
// Logic para sa pag-swipe pa-ibaba
}
}
}