2021년 2월 13일 토요일

[Unity3d] UGUI 조이스틱 인터페이스

 UGUI로 간단하게 만들수 있다. 

6.(scene 전환내용에 이어서)외곽의 원, 그밑으로 핸들을 UI -> Image로 만들고 색상과 Source Image(Knob)으로 만들어둔다.

https://github.com/sugoigroup/unity_study_1/commit/f5c7892cc8faf6236ceef1a49dbcf03a376f64ac

스크립트는 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Joypad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
    [SerializeField] private RectTransform rectBack;
    [SerializeField] private RectTransform recFront;
    private float radius;

    public void OnDrag(PointerEventData eventData)
    {
        Vector2 value = eventData.position - (Vector2)rectBack.position;
        value = Vector2.ClampMagnitude(value, radius);
        recFront.localPosition = value;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        //throw new System.NotImplementedException();
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        recFront.localPosition = Vector3.zero;
    }

    // Start is called before the first frame update
    void Start()
    {
        radius = rectBack.rect.width * 0.5F;

    }

    // Update is called once per frame
    void Update()
    {
        
    }

Vector3에대해선.

http://blog.naver.com/PostView.nhn?blogId=dus531400&logNo=140207486483&parentCategoryNo=&categoryNo=&viewDate=&isShowPopularPosts=false&from=postView


7. 캐릭터도 이동시켜보자.

https://github.com/sugoigroup/unity_study_1/commit/f373d12d2bcefe370e8d824473d64649890df7c4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class MoveCharactor : MonoBehaviour
{
    public float speed;
    private Joypad joypad;

    // Awake는 무조건 실행되는 스크립트이다. start는 스크립트가 활성화 상태에서만 실행된다. 
    private void Awake()
    {
        joypad = GameObject.FindObjectOfType<Joypad>();
    }

    private void FixedUpdate()
    {
        if (joypad.Horizontal != 0 || joypad.Vertical != 0)
        {
            MoveControl();
        }
    }

    private void MoveControl()
    {

        Vector3 upMovement = Vector3.up * speed *  Time.deltaTime*  joypad.Vertical; 
        Vector3 rightMovement = Vector3.right * speed * Time.deltaTime  *  joypad.Horizontal;
        transform.position += upMovement;
        transform.position += rightMovement;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Joypad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
    [SerializeField] private RectTransform rectBack;
    [SerializeField] private RectTransform recFront;
    private float radius;

    private Vector3 input = Vector3.zero;
    public float Horizontal { get { return input.x; } }
    public float Vertical { get { return input.y; } }

    public void OnDrag(PointerEventData eventData)
    {
        input = Vector2.ClampMagnitude(
            eventData.position - (Vector2)rectBack.position,
            radius);
        recFront.localPosition = input;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        recFront.localPosition = input = Vector3.zero;
    }

    // Start is called before the first frame update
    void Start()
    {
        radius = rectBack.rect.width * 0.5F;

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}



















0 comments:

댓글 쓰기