-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputManager.cs
executable file
·66 lines (59 loc) · 1.8 KB
/
InputManager.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
using System;
using UnityEngine;
public class InputManager : MonoBehaviour
{
bool isDragging = false;
GameObject draggingObject;
public event Action<GameObject> OnGrabAttractor = delegate { };
public event Action<GameObject> OnReleaseAttractor = delegate { };
private bool disabled = false;
public void Update()
{
Input.simulateMouseWithTouches = true; // HACK
if (Input.GetMouseButton(0) && !disabled)
{
if (!isDragging)
{
draggingObject = GetObjectFromMouseRaycast();
if (draggingObject)
{
OnGrabAttractor(draggingObject);
isDragging = true;
}
}
else if (draggingObject != null)
{
var targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPosition.z = 0f;
draggingObject.transform.position = targetPosition;
}
}
else
{
if (isDragging)
{
OnReleaseAttractor(draggingObject);
}
isDragging = false;
}
}
internal void Enable()
{
disabled = false;
}
// disables the grab input and releases the currently dragged attractor, if any
public void Disable()
{
disabled = true;
}
private GameObject GetObjectFromMouseRaycast()
{
GameObject gmObj = null;
var collider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
if (collider != null && collider.GetComponent<Attractor>() != null)
{
gmObj = collider.gameObject;
}
return gmObj;
}
}