[Memo-Unity] 3. ์กฐ์ด์Šคํ‹ฑ(JoyStick)

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: ,


์กฐ์ด์Šคํ‹ฑ(JoyStick)

๊ณ ๋ฐ•์‚ฌ์˜ ์œ ๋‹ˆํ‹ฐ ๊ธฐ์ดˆ๋ฅผ ์ •๋ฆฌ.

Joystick

image

์ด๋ฏธ์ง€

  1. ์กฐ์ด์Šคํ‹ฑ์œผ๋กœ ์‚ฌ์šฉ ํ•  ์ด๋ฏธ์ง€ ์ƒ์„ฑ ( UI- IMAGE)
  2. Canvas ์—์„œ ํ•ด์ƒ๋„ ์„ค์ • ex) scale with screen size , 1600:1000 , 0.5
    joysticimage
  3. ui ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ์กฐ์ •
    image
  4. ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์‚ฌ์šฉ๋  ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ , ๋ฐฐ๊ฒฝ์˜ ์ž์‹์œผ๋กœ ์„ค์ •




C์Šคํฌ๋ฆฝํŠธ

using.UnityEngine.EventSystems

1. MoveTouch.CS

์กฐ์ด์Šคํ‹ฑ ํ„ฐ์น˜์‹œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ

MoveTouch.cs

using UnityEngine;
using UnityEngine.UI;

using UnityEngine.EventSystems;

public class MoveTouch : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
    private Image imageBackground;
    private Image imageController;
    private Vector2 TouchPosition;
    private void Awake() {
        imageBackground = GetComponent<Image>();
        imageController = transform.GetChild(0).GetComponent<Image>();
    }

    //ํ„ฐ์น˜์‹œ 1ํšŒ 
    public void OnPointerDown(PointerEventData eventData){
    }

    //ํ„ฐ์น˜ํ›„ ๋“œ๋ž˜๊ทธ ๋งคํ”„๋ ˆ์ž„
    public void OnDrag(PointerEventData eventData){
        
        TouchPosition = Vector2.zero;

        // ์กฐ์ด์Šคํ‹ฑ์˜ ์œ„์น˜๊ฐ€ ์–ด๋””์— ์žˆ๋“  ๋™์ผํ•œ ๊ฐ’์„ ์—ฐ์‚ฐํ•˜๊ธฐ ์œ„ํ•ด
        // touchPosition์˜ ์œ„์น˜ ๊ฐ’์€ ์ด๋ฏธ์ง€์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์–ผ๋งˆ๋‚˜ ๋–จ์–ด์ ธ ์žˆ๋Š”์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜จ๋‹ค.
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
            imageBackground.rectTransform, eventData.position,eventData.pressEventCamera,out TouchPosition ))
        {   //์ด๋ฏธ์ง€์˜ teansform์ •๋ณด, ํ™”๋ฉดํ„ฐ์น˜์ขŒํ‘œ, ํ˜„์žฌ ํ™”๋ฉด์— ๋Œ€ํ•œ ์นด๋ฉ”๋ผ, ์—ฐ์‚ฐ๋œ ์ขŒํ‘œ๋ฅผ ์ €์žฅ

            // touchposition๊ฐ’ ์ •๊ทœํ™” ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ”  [๊ฐ’ : 0~1 ]
            // ๋ฐฐ๊ฒฝ๋ฐ–์œผ๋กœ ๋‚˜๊ฐ€๋ฉด ์ด์ƒ์˜ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค -2 ~ 2
            TouchPosition.x = (TouchPosition.x / imageBackground.rectTransform.sizeDelta.x);
            TouchPosition.y = (TouchPosition.y / imageBackground.rectTransform.sizeDelta.y);
            
            //normalized๋ฅผ ์ด์šฉํ•ด ํ„ฐ์น˜ ์ขŒํ‘œ๊ฐ’์„ -1 ~ 1๋กœ ์ œํ•œ
            TouchPosition = (TouchPosition.magnitude > 1) ? TouchPosition.normalized : TouchPosition;
            
            // ์ปจํŠธ๋กค๋Ÿฌ ์›€์ง์ž„์„ ์ œํ•œ
            imageController.rectTransform.anchoredPosition = new Vector2(
                TouchPosition.x * imageBackground.rectTransform.sizeDelta.x/2,
                TouchPosition.y * imageBackground.rectTransform.sizeDelta.y/2
            );
        }    
    }

    //ํ„ฐ์น˜์ข…๋ฃŒ์‹œ 1ํšŒ
    public void OnPointerUp(PointerEventData eventData){
        //์ข…๋ฃŒ์‹œ ์œ„์น˜,๋ฐฉํ–ฅ ์ดˆ๊ธฐํ™”
        imageController.rectTransform.anchoredPosition =Vector2.zero;
        TouchPosition = Vector2.zero;
    }

    public float Horizontal(){
        return TouchPosition.x;
    }
    
    public float Vertical(){
        return TouchPosition.y;
    }
}

  • OnPointerDown , OnDrag , OnPointerUp๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
  • IPointerDownHandler, IDragHandler, IPointerUpHandler๋ฅผ ์‚ฌ์šฉ
  • ์กฐ์ด์Šคํ‹ฑ์œผ๋กœ ์˜ค๋ธŒ์ ํŠธ ์›€์ง์ด๊ธฐ.





2. Movement.CS

์กฐ์ด์Šคํ‹ฑ์œผ๋กœ ์ธํ•ด ์›€์ง์ด๋Š” ์˜ค๋ธŒ์ ํŠธ ์ฝ”๋“œ

Movement.cs

using UnityEngine;

public class Movement : MonoBehaviour
{
    [SerializeField]
   private MoveTouch movetouch;
   private float movespeed = 10 ;

    void Update()
    {
        float x = movetouch.Horizontal();
        float y = movetouch.Vertical();
        if (x!=0||y!=0)
        {
            transform.position += new Vector3(x,y,0)*movespeed*Time.deltaTime;
        }
    }
}







TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ