본문 바로가기
Unity/수업내용

[Unity 2D] 스와이프 예제 변형

by 왹져박사 2023. 1. 30.
728x90

StarController

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

public class StarController : MonoBehaviour
{
    private float speed = 0;
    private Vector2 startPos;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            this.startPos = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            Vector2 endPos = Input.mousePosition;
            float len = endPos.y - startPos.y;

            this.speed = len / 800.0f;
        }
        this.transform.Translate(0, speed, 0, Space.World);
        if (speed > 0)
        {
            this.transform.Rotate(0, 0, 10.0f);
        }
        speed *= 0.98f;
    }
}

 

GameDirectorStar

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

public class GameDirectorStar : MonoBehaviour
{
    GameObject shuriken;
    GameObject ground;
    GameObject distance;

    // Start is called before the first frame update
    void Start()
    {
        this.shuriken = GameObject.Find("shuriken");
        this.ground = GameObject.Find("ground");
        this.distance = GameObject.Find("Direction");
    }

    // Update is called once per frame
    void Update()
    {
        var ay = shuriken.transform.position.y;
        var by = ground.transform.position.y;

        var len = by - ay;
        Text textDirection = distance.GetComponent<Text>();
        if (len >= 0)
        {
            textDirection.text = string.Format("목표 거리 {0:F2}", len);
        }
        else
        {
            textDirection.text = string.Format("Gameover");
        }
    }
}

 

 

 

728x90