using UnityEngine using System Collections public class Drag MonoBehav

  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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
using UnityEngine;
using System.Collections;
public class Drag : MonoBehaviour
{
private int swipeId;
private Vector2 EndPosition;
private Vector2 ForcePoint;
Collider2D aHit;
public int i = 0;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
foreach (Touch Check in Input.touches)
{
if (Check.phase == TouchPhase.Began)
{
aHit = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
}
if (aHit)
{
aHit.rigidbody2D.velocity = new Vector2(0, 0);
if (aHit.gameObject.tag == "DestroybleBlock")
{
Destroy(aHit.gameObject);
}
if (aHit.tag == "SlideBlock")
{
MoveTo(aHit);
}
if (aHit.tag == "ToLeft")
{
if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x < aHit.transform.position.x)
{
MoveTo(aHit);
}
}
if (aHit.tag == "ToRight")
{
if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x > aHit.transform.position.x)
{
MoveTo(aHit);
}
}
if (aHit.tag == "Door")
{
//Debug.DrawLine(Camera.main.ScreenToWorldPoint(Input.mousePosition), new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x + 10, Camera.main.ScreenToWorldPoint(Input.mousePosition).y));
// aHit.collider.rigidbody2D.AddForceAtPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition), new Vector2(-10, 0),ForceMode2D.Force);
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
swipeId = touch.fingerId;
ForcePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
else
{
if (swipeId == touch.fingerId)
{
if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
{
EndPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
aHit.rigidbody2D.AddForceAtPosition(ForcePoint, new Vector2(EndPosition.x - ForcePoint.x, EndPosition.y - ForcePoint.y)*100, ForceMode2D.Force);
ForcePoint = EndPosition;
i = 0;
}
}
}
}
}
}
}
}
void MoveTo(Collider2D aHit)
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
swipeId = touch.fingerId;
}
else
{
if (swipeId == touch.fingerId)
{
if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
{
aHit.rigidbody2D.velocity = new Vector2(Camera.main.ScreenToWorldPoint(touch.position).x - aHit.transform.position.x, 0) * 10;
}
if (touch.phase == TouchPhase.Canceled)
{
aHit.rigidbody2D.velocity /= 10;
}
}
}
}
}
}