개선이 조금 필요함

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{

    private float moveSpeed;
    private Transform tr;
    private Vector2 initTouchDown;
    private Vector2 initDrag;

    private void Start()
    {
        tr = GetComponent();
        moveSpeed = 0.1f;
    }


    private void Update()
    {
        PlayerMove();
    }

    private void PlayerMove()
    {
        if (Input.GetMouseButtonDown(0))
        {
            initTouchDown = Input.mousePosition;
        }

        if (Input.GetMouseButton(0))
        {
            initDrag = Input.mousePosition;
            initDrag = (initDrag - initTouchDown).normalized;
            Debug.Log("initTouchDown : " + initTouchDown + "  initDrag : " + initDrag);
            tr.Translate(initDrag * moveSpeed);
        }
    }
}

 

========================================================================================================================================================

약간 개선된 코드

    using UnityEngine;

    public class Move : MonoBehaviour
    {

        private float moveSpeed;
        private Transform tr;
        private Vector2 initTouchDown;
        private Vector2 initDrag;

        private void Start()
        {
            tr = GetComponent();
            moveSpeed = 0.1f;
        }



        private void Update()
        {
            PlayerMove();
        }

        private void PlayerMove()
        {
            if (Input.GetMouseButtonDown(0))
            {
                initTouchDown = Input.mousePosition;
            }

            if (Input.GetMouseButton(0))
            {
                initDrag = Direction(initTouchDown, Input.mousePosition);
                tr.Translate(initDrag * moveSpeed);
            }
        }

        private Vector2 Direction(Vector2 fir, Vector2 sec)
        {
            if (fir.x <= sec.x + 50 && fir.x >= sec.x - 50)
            {
                if (fir.y <= sec.y + 50 && fir.y >= sec.y - 50)
                    return Vector2.zero;
                else
                    return new Vector2(0, sec.y - fir.y).normalized;
            }
            else
            {
                if (fir.y <= sec.y + 50 && fir.y >= sec.y - 50)
                    return new Vector2(sec.x - fir.x, 0).normalized;
                else
                    return (sec - fir).normalized;
            }
        }
    }

'Unity > Unity자체' 카테고리의 다른 글

마우스 8방향 이동  (0) 2020.03.09

긁기용

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{

    private float moveSpeed;
    private Transform tr; //움직일 오브젝트의 트렌스폼
    private Vector2 initTouchDown;
    private Vector2 initDrag;
    private Camera mainCamera;

    void Start()
    {
        tr = GetComponent();
        moveSpeed = 0.1f;
        mainCamera = GameObject.Find("Main Camera").GetComponent();
    }


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

    private void PlayerMove()
    {
        if (Input.GetMouseButtonDown(0))
        {
            initTouchDown = Input.mousePosition;
        }

        if (Input.GetMouseButton(0))
        {
            initDrag = Input.mousePosition;
            Debug.Log("initTouchDown : " + initTouchDown + "  initDrag : " + initDrag);
            MoveSwitch(Direction(initTouchDown, initDrag)); //fir Vec2, sec Vec2
        }
    }

    private int Direction(Vector2 fir, Vector2 sec)
    {
        if(fir.x <= sec.x + 40 && fir.x >= sec.x - 40)
        {
            if (fir.y <= sec.y + 40 && fir.y >= sec.y - 40)
                return 1; //정지
            else if (fir.y > sec.y)
                return 2; //아래
            else
                return 3; //위
        }
        else if(fir.x > sec.x + 40)
        {
            if (fir.y <= sec.y + 40 && fir.y >= sec.y - 40)
                return 4; //왼쪽
            else if (fir.y > sec.y)
                return 5; //왼쪽아래
            else
                return 6; //왼쪽위에
        }
        else
        {
            if (fir.y <= sec.y + 40 && fir.y >= sec.y - 40)
                return 7; //오른쪽
            else if (fir.y > sec.y + 40)
                return 8; //오른쪽아래
            else
                return 9; //오른쪽위에
        }
    }

    private void MoveSwitch(int move)
    {
        switch (move)
        {
            case 1:
                tr.Translate(Vector2.zero * moveSpeed);
                break;
            case 2:
                tr.Translate(-Vector2.up * moveSpeed);
                break;
            case 3:
                tr.Translate(Vector2.up * moveSpeed);
                break;
            case 4:
                tr.Translate(-Vector2.right * moveSpeed);
                break;
            case 5:
                tr.Translate(-Vector2.right * moveSpeed / 2);
                tr.Translate(-Vector2.up * moveSpeed / 2);
                break;
            case 6:
                tr.Translate(-Vector2.right * moveSpeed / 2);
                tr.Translate(Vector2.up * moveSpeed / 2);
                break;
            case 7:
                tr.Translate(Vector2.right * moveSpeed);
                break;
            case 8:
                tr.Translate(Vector2.right * moveSpeed /2);
                tr.Translate(-Vector2.up * moveSpeed / 2);
                break;
            case 9:
                tr.Translate(Vector2.right * moveSpeed / 2);
                tr.Translate(Vector2.up * moveSpeed / 2);
                break;
        }
    }
}

 

'Unity > Unity자체' 카테고리의 다른 글

마우스 조이스틱  (0) 2020.03.09

+ Recent posts