-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMouseManager.cs
124 lines (102 loc) · 4.3 KB
/
MouseManager.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
using UnityEngine;
using System.Collections;
public class MouseManager : MonoBehaviour
{
public bool useSpring = false;
public LineRenderer dragLine;
//float dragSpeed = 4f;
float velocityRatio = 4f; // If we aren't using a spring
Rigidbody2D grabbedObject = null;
SpringJoint2D springJoint = null;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// We clicked, but on what?
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
Vector2 dir = Vector2.zero;
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, dir);
if (hit.collider != null)
{
// We clicked on SOMETHING that has a collider
if (hit.collider.GetComponent<Rigidbody2D>())
{
grabbedObject = hit.collider.GetComponent<Rigidbody2D>();
if (useSpring)
{
springJoint = grabbedObject.gameObject.AddComponent<SpringJoint2D>();
grabbedObject.gravityScale = 0;
// Set the anchor to the spot on the object that we clicked.
Vector3 localHitPoint = grabbedObject.transform.InverseTransformPoint(hit.point);
springJoint.anchor = localHitPoint;
springJoint.connectedAnchor = mouseWorldPos3D;
springJoint.distance = 0f;
springJoint.dampingRatio = 0.1f;
springJoint.frequency = 1;
springJoint.autoConfigureDistance = false;
// Enable this if you want to collide with objects still (and you probably do)
// This will also WAKE UP the spring.
springJoint.enableCollision = true;
// This will also WAKE UP the spring, even if it's a totally
// redundant line because the connectedBody should already be null
springJoint.connectedBody = null;
} else
{ // using velocity instead.
grabbedObject.gravityScale = 0;
}
dragLine.enabled = true;
}
}
}
if (Input.GetMouseButtonUp(0) && grabbedObject != null)
{
if (useSpring)
{
Destroy(springJoint);
springJoint = null;
} else
{
grabbedObject.gravityScale = 0;
}
grabbedObject = null;
dragLine.enabled = false;
}
if (springJoint)
{
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
springJoint.connectedAnchor = mouseWorldPos3D;
}
}
void FixedUpdate()
{
if (grabbedObject)
{
Vector2 mouseWorldPos2D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (useSpring)
{
springJoint.connectedAnchor = mouseWorldPos2D;
} else
{
grabbedObject.velocity = (mouseWorldPos2D - grabbedObject.position) * velocityRatio;
}
}
}
void LateUpdate()
{
if (grabbedObject)
{
if (useSpring)
{
Vector3 worldAnchor = grabbedObject.transform.TransformPoint(springJoint.anchor);
dragLine.SetPosition(0, new Vector3(worldAnchor.x, worldAnchor.y, -1));
dragLine.SetPosition(1, new Vector3(springJoint.connectedAnchor.x, springJoint.connectedAnchor.y, -1));
} else
{
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
dragLine.SetPosition(0, new Vector3(grabbedObject.position.x, grabbedObject.position.y, -1));
dragLine.SetPosition(1, new Vector3(mouseWorldPos3D.x, mouseWorldPos3D.y, -1));
}
}
}
}