यूनिटी में मोबाइल टच इनपुट जॉयस्टिक

मोबाइल गेम में एक नियंत्रणीय चरित्र बनाने के लिए, किसी को स्क्रीन पर दिखने वाले और टच इनपुट के प्रति प्रतिक्रियाशील बटन जोड़ने होंगे।

इस ट्यूटोरियल में, मैं दिखाऊंगा कि Unity में जॉयस्टिक जैसा बटन कैसे बनाया जाए, जो मुख्य रूप से टचस्क्रीन फोन पर मूवमेंट कंट्रोल के लिए उपयुक्त है।

आप अधिक बटन और जॉयस्टिक जोड़ने के लिए नीचे दिए गए उदाहरण का भी उपयोग कर सकते हैं। तो चलिए शुरू करते हैं!

चरण 1: सभी आवश्यक स्क्रिप्ट बनाएं

MobileJoystick_UI.cs

using UnityEngine;
using UnityEngine.UI;

public class MobileJoystick_UI : MonoBehaviour
{

    //Mobile controller graphics
    public Sprite navigationCircle;
    public Sprite navigationButton;
    //Use this in your movement script for the input control
    public Vector2 moveDirection;
    //Joystick components size
    int circleSize = 120;
    int buttonSize = 100;
    //How far the joystick should be placed from the side of the screen
    int marginLeft = 100;
    //How far the joystick should be placed from the bottom of the screen
    int marginBottom = 100;

    Canvas mainCanvas;

    //Mobile movement
    [System.Serializable]
    public class JoystickButton
    {
        public Image backgroundCircle;
        public Image mainButton;
        public Rect defaultArea;
        public Vector2 touchOffset;
        public Vector2 currentTouchPos;
        public int touchID;
        public bool isActive = false;
    }

    //Move joystick data
    JoystickButton moveTouch = new JoystickButton();

    public static MobileJoystick_UI instance;

    // Start is called before the first frame update
    void Start()
    {
        if (instance != null)
        {
            //There is another instance already present, remove this one
            Destroy(gameObject);
            return;
        }
        //Assign this instance to a static variable so you can access the movement direction directly at MobileJoystick_UI.instance.moveDirection
        instance = this;

        //This function will initialize canvas element along with the joystick button
        GameObject tmpObj = new GameObject("Canvas");
        tmpObj.transform.position = Vector3.zero;
        mainCanvas = tmpObj.AddComponent<Canvas>();
        mainCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
        mainCanvas.pixelPerfect = true;

        //Add Canvas Scaler component
        CanvasScaler canvasScaled = tmpObj.AddComponent<CanvasScaler>();
        canvasScaled.scaleFactor = 1;
        canvasScaled.referencePixelsPerUnit = 100;

        //Add Graphic Raycaster element
        tmpObj.AddComponent<GraphicRaycaster>();

        //Setup navigation background
        GameObject cntrlTmpObj = new GameObject("Movement Circle");
        cntrlTmpObj.transform.position = Vector3.zero;
        cntrlTmpObj.transform.parent = tmpObj.transform;
        moveTouch.backgroundCircle = cntrlTmpObj.AddComponent<Image>();
        moveTouch.backgroundCircle.sprite = navigationCircle;
        moveTouch.backgroundCircle.rectTransform.anchorMin = new Vector2(0, 0);
        moveTouch.backgroundCircle.rectTransform.anchorMax = new Vector2(0, 0);
        moveTouch.backgroundCircle.rectTransform.sizeDelta = new Vector2(circleSize, circleSize);
        moveTouch.backgroundCircle.rectTransform.pivot = new Vector2(0, 0);
        moveTouch.backgroundCircle.rectTransform.position = new Vector3(marginLeft, marginBottom, 0);

        //Navigation button
        cntrlTmpObj = new GameObject("Movement Button");
        cntrlTmpObj.transform.position = Vector3.zero;
        cntrlTmpObj.transform.parent = tmpObj.transform;
        moveTouch.mainButton = cntrlTmpObj.AddComponent<Image>();
        moveTouch.mainButton.sprite = navigationButton;
        moveTouch.mainButton.rectTransform.anchorMin = new Vector2(0, 0);
        moveTouch.mainButton.rectTransform.anchorMax = new Vector2(0, 0);
        moveTouch.mainButton.rectTransform.sizeDelta = new Vector2(buttonSize, buttonSize);
        moveTouch.mainButton.rectTransform.pivot = new Vector2(0, 0);
        moveTouch.mainButton.rectTransform.position = new Vector3(marginLeft + (circleSize - buttonSize) / 2, marginBottom + (circleSize - buttonSize) / 2, 0);

        //Save the default location of the joystick button to be used later for input detection
        moveTouch.defaultArea = new Rect(moveTouch.mainButton.rectTransform.position.x,
            moveTouch.mainButton.rectTransform.position.y,
            moveTouch.mainButton.rectTransform.sizeDelta.x,
            moveTouch.mainButton.rectTransform.sizeDelta.y);
    }

    // Update is called once per frame
    void Update()
    {
        //Handle joystick movement
#if (UNITY_ANDROID || UNITY_IOS || UNITY_WP8 || UNITY_WP8_1) && !UNITY_EDITOR
        //Mobile touch input
        for (var i = 0; i < Input.touchCount; ++i)
        {
            Touch touch = Input.GetTouch(i);

            if (touch.phase == TouchPhase.Began)
            {
                MobileButtonsCheck(new Vector2(touch.position.x, Screen.height - touch.position.y), touch.fingerId);
            }

            if (touch.phase == TouchPhase.Moved )
            {
                if(moveTouch.isActive && moveTouch.touchID == touch.fingerId)
                {
                    moveTouch.currentTouchPos = touch.position;
                }
            }

            if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
            {
                MobileButtonStop(touch.fingerId);
            }
        }
#else
        //Desktop mouse input for editor testing
        if (Input.GetMouseButtonDown(0))
        {
            MobileButtonsCheck(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y), -1);
        }

        if (Input.GetMouseButtonUp(0))
        {
            MobileButtonStop(-1);
        }

        moveTouch.currentTouchPos = Input.mousePosition;
#endif

        //Moving
        if (moveTouch.isActive)
        {
            moveTouch.mainButton.rectTransform.position = new Vector3(moveTouch.currentTouchPos.x - moveTouch.touchOffset.x, moveTouch.currentTouchPos.y - moveTouch.touchOffset.y);
            moveDirection.x = moveTouch.mainButton.rectTransform.position.x - moveTouch.defaultArea.x;
            moveDirection.y = moveTouch.mainButton.rectTransform.position.y - moveTouch.defaultArea.y;

            if (Mathf.Abs(moveDirection.x) < 19)
            {
                moveDirection.x = 0;
            }
            else
            {
                moveDirection.x = Mathf.Clamp(moveDirection.x / 75.000f, -1.000f, 1.000f);
            }

            if (Mathf.Abs(moveDirection.y) < 19)
            {
                moveDirection.y = 0;
            }
            else
            {
                moveDirection.y = Mathf.Clamp(moveDirection.y / 75.000f, -1.000f, 1.000f);
            }
        }
        else
        {
            moveTouch.mainButton.rectTransform.position = new Vector3(moveTouch.defaultArea.x, moveTouch.defaultArea.y);
            moveDirection = Vector2.zero;
        }
    }

    //Here we check if the clicked/tapped position is inside the joystick button
    void MobileButtonsCheck(Vector2 touchPos, int touchID)
    {
        //Move controller
        if (moveTouch.defaultArea.Contains(new Vector2(touchPos.x, Screen.height - touchPos.y)) && !moveTouch.isActive)
        {
            moveTouch.isActive = true;
            moveTouch.touchOffset = new Vector2(touchPos.x - moveTouch.defaultArea.x, Screen.height - touchPos.y - moveTouch.defaultArea.y);
            moveTouch.currentTouchPos = new Vector2(touchPos.x, Screen.height - touchPos.y);
            moveTouch.touchID = touchID;
        }
    }

    //Here we release the previously active joystick if we release the mouse button/finger from the screen
    void MobileButtonStop(int touchID)
    {
        if (moveTouch.isActive && moveTouch.touchID == touchID)
        {
            moveTouch.isActive = false;
            moveTouch.touchOffset = Vector2.zero;
            moveTouch.touchID = -1;
        }
    }
}

TouchPlayerController.cs

using UnityEngine;

public class TouchPlayerController : MonoBehaviour
{

    // Update is called once per frame
    void Update()
    {
        //Move Front/Back
        if (MobileJoystick_UI.instance.moveDirection.y != 0)
        {
            transform.Translate(transform.forward * Time.deltaTime * 2.45f * MobileJoystick_UI.instance.moveDirection.y, Space.World);
        }

        //Rotate Left/Right
        if (MobileJoystick_UI.instance.moveDirection.x != 0)
        {
            transform.Rotate(new Vector3(0, 14, 0) * Time.deltaTime * 4.5f * MobileJoystick_UI.instance.moveDirection.x, Space.Self);
        }
    }
}

चरण 2: ऊपर दी गई स्क्रिप्ट का उपयोग करके एक सरल दृश्य सेट करें

  • नया दृश्य बनाएं
  • एक नया गेमऑब्जेक्ट बनाएं और उसे कॉल करें '_TouchInput'
  • इसमें MobileJoystick_UI स्क्रिप्ट संलग्न करें
  • 'Navigation Circle' और 'Navigation button' वेरिएबल असाइन करें।

आप नीचे दिए गए स्प्राइट का उपयोग कर सकते हैं या यहां क्लिक करें:

  • उन्हें Unity में आयात करने के बाद, बनावट प्रकार को बदलना सुनिश्चित करें 'Sprite (2D and UI)'

चरण 3: प्लेयर इंस्टेंस सेटअप करें

अंत में, हम प्लेयर इंस्टेंस सेट करते हैं (मेरे मामले में यह एक साधारण गेमऑब्जेक्ट होगा जिसके अंदर एक सिलेंडर होगा):

  • एक नया GameObject बनाएं और उसे कॉल करें 'MobilePlayer'
  • इसमें TouchPlayerController स्क्रिप्ट संलग्न करें
  • एक नया सिलेंडर बनाएं और उसकी ऊंचाई को तब तक कम करें जब तक कि वह लगभग सपाट न दिखने लगे (मेरे मामले में पैमाना है (x: 1 y: 0.0142 z: 1) )
  • सिलेंडर को 'MobilePlayer' गेमऑब्जेक्ट के अंदर ले जाएं
  • परीक्षण उद्देश्यों के लिए, आप मुख्य कैमरे को 'MobilePlayer' के अंदर भी ले जा सकते हैं और इसे घुमा सकते हैं ताकि यह इस तरह से प्लेयर की ओर इंगित हो:

अब Play दबाने का समय है और देखें कि सब कुछ ठीक काम करता है या नहीं।

Sharp Coder वीडियो प्लेयर

सब कुछ उम्मीद के मुताबिक काम करता है! जॉयस्टिक बटन को चारों ओर घुमाकर खिलाड़ी को नियंत्रित किया जाता है।

MobileJoystick_UI स्क्रिप्ट मोबाइल टच इनपुट और माउस क्लिक दोनों का समर्थन करती है (यदि आप संपादक में खेलते हैं)।

सुझाए गए लेख
एकता मोबाइल टच कंट्रोल कैसे बनाएं
एकता के लिए हेलीकाप्टर नियंत्रक
यूनिटी में 2डी प्लेटफ़ॉर्मर कैरेक्टर कंट्रोलर में डबल जंप सपोर्ट जोड़ना
एकता में क्रेन नियंत्रण कैसे बनाएं
यूनिटी के लिए कार नियंत्रक
यूनिटी में एफपीएस प्लेयर में क्राउचिंग जोड़ना
एकता में खिलाड़ी आंदोलन बनाना