using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Drag : MonoBehaviour
{
ย ย float deltax;
ย ย float deltay;
ย ย private Rigidbody2D rb;
ย ย //-----------------------------------------------------------------------------
ย ย public Vector3 moveVelocity;
ย ย public float movePower = 0.5f;
ย ย float moveVelocityx = 1;
ย ย float moveVelocityy = 1;
ย ย //----------------------------------------------------------------------------
ย ย private void Start()
ย ย {
ย ย ย ย rb = GetComponent();
ย ย }
ย ย private void Update()
ย ย {
ย ย ย ย StartCoroutine(StopGroundPopUp());
ย ย ย ย StartCoroutine(Drag1());
ย ย }
ย ย void Movedd()// ํ๋ ์ด์ด๊ฐ ์ผ์ชฝ์ผ๋ก ์ด๋ํ๋ ํจ์.ย
ย ย {
ย ย ย ย moveVelocity = new Vector3(moveVelocityx, moveVelocityy, 0);
ย ย ย ย transform.position += moveVelocity.normalized * movePower * Time.deltaTime;
ย ย }
ย ย IEnumerator StopGroundPopUp()
ย ย {
ย ย ย ย Movedd();
ย ย ย ย yield return new WaitForSeconds(0.5f);
ย ย }
ย ย IEnumerator Drag1()
ย ย {
ย ย ย ย if (Input.touchCount > 0)// initiating touch event, if touch event takes place
ย ย ย ย {
ย ย ย ย ย ย Touch touch = Input.GetTouch(0); // get touch to take a deal withย
ย ย ย ย ย ย Vector2 touchpos = Camera.main.ScreenToWorldPoint(touch.position);ย
ย ย ย ย ย ย switch (touch.phase)
ย ย ย ย ย ย {
ย ย ย ย ย ย ย ย case TouchPhase.Began:
ย ย ย ย ย ย ย ย ย ย deltax = touchpos.x - transform.position.x;
ย ย ย ย ย ย ย ย ย ย deltay = touchpos.y - transform.position.y;
ย ย ย ย ย ย ย ย ย ย break;
ย ย ย ย ย ย ย ย case TouchPhase.Moved:
ย ย ย ย ย ย ย ย ย ย rb.MovePosition(new Vector2(touchpos.x - deltax, touchpos.y - deltay));
ย ย ย ย ย ย ย ย ย ย break;
ย ย ย ย ย ย ย ย case TouchPhase.Ended:
ย ย ย ย ย ย ย ย ย ย rb.velocity = Vector2.zero;
ย ย ย ย ย ย ย ย ย ย break;
ย ย ย ย ย ย }
ย ย ย ย }
ย ย ย ย yield return new WaitForSeconds(0.5f);
ย ย }
}
์บ๋ฆญํฐ๊ฐ ์์ง์ด๋ ์ํ์์ ์์ผ๋ก ๋๋๊ทธํ๋ฉด ๋๋๊ทธํ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋๊ฑธ ๋ง๋ค๊ณ ์๋๋ฐ
๋๋๊ทธํ ๋ฐฉํฅ์ผ๋ก ๋ฐฉํฅ์ ๋๋ฆฐ๊ฑด ๋์งธ ์น๋๋ผ๋ ์์ง์ด๋์ํ์์๋ ๋๋๊ทธ๊ฐ ์๋๋ค. ๋ฌด์์ ๋ฐ๊ฟ์ผ ๋ ๊น?
์ฝ๋ฃจํด์ผ๋ก ๋๋ฆฌ๋ฉด ํด๊ฒฐ๋ ๊น ์ถ์ด์ ๋๋ค ์ฝ๋ฃจํด์ผ๋ก ๋๋ ธ๋๋ฐ๋ ์์ง์ด๋ ๋์ค์ ๋๋๊ทธ๊ฐ ์๋จ
์ฝ๋ฃจํด์ update์์ ๊ณ์ ํธ์ถํ๋ ๊ฒ ๋ถํฐ๊ฐ ์ด์ํ๊ณ , ๋๋๊ทธ๋ ์ ๋ ฅ์ด ์๋ค๊ฐ ์ ๋ ฅ์ด ์๊ธด ์ขํํ๊ณ ๋ง์ง๋ง์ผ๋ก ์์ ๋๊ธฐ ์ ์ ์ขํ๋ก ๋ง๋ ๋ค๊ณ ์๊ฐํ๊ณ ๋ค์ ํด๋ด
์ ์ง ์ํ์์๋ ๋๋๊ทธ ์ด๋์ด ๋๋? ๊ทธ๋ฌ๋ฉด ๋๊ฐ๊ฐ ๋์์ ์๋๋ค๋ ์๋ฆฌ๋ค
์ด๋ด๋๋ ํฉ์ณ์ผ๋๋๋ฐ transform.position += ์ด๋ถ๋ถ์์ ๊ฐ์ด ํฉ์ณ์ ์ฐ์ฐํ๋ฉด ๋ ๋ฏ์ถ๋ค
yield return new WaitForSeconds(0.5f); ์ธ๋ฐ ๋๋ 0.5์ด๋ง๋ค ํ์ด๋ฐ ๋ง์ถฐ์ ๋๋๊ทธํ๋ ์ฌ์ฃผ๋ฅผ ๊ฐ์ง ์์ด๊ตฌ๋?