-
Notifications
You must be signed in to change notification settings - Fork 5
/
CoinCollectionController.js
93 lines (78 loc) · 3.03 KB
/
CoinCollectionController.js
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
//@input Component.Image backpackImage
//@input Component.Image coinImage
//@input Component.Camera sceneCamera
//@input Component.Camera uiCamera
//@input float hitCheckDistance
// @input Asset.RemoteServiceModule remoteServiceModule
// Import module
const Module = require("./Camera Kit Unity Bridge API Module");
const UnityApi = new Module.ApiModule(script.remoteServiceModule);
// -----JS CODE-----
var dragging = false
var overlapping = false
function sendEventToUnity(eventName, eventValue) {
var dataToSend = {
"eventName" : eventName,
"eventValue" : eventValue
};
UnityApi.unity_send_data(JSON.stringify(dataToSend), function(err, r){
print("Data sent to Unity");
print("Error? " + err);
print("Result? " + r);
})
}
function handDetected(handName) {
//print("Hand detected: "+ handName);
sendEventToUnity("handDetected", handName);
}
function handLost(handName) {
//print("Hand lost: "+ handName);
sendEventToUnity("handLost", handName);
}
script.createEvent("UpdateEvent").bind(function() {
if (dragging) {
var targetScreenPos = script.uiCamera.worldSpaceToScreenSpace(script.backpackImage.getTransform().getWorldPosition());
var objScreenPos = script.sceneCamera.worldSpaceToScreenSpace(script.coinImage.getTransform().getWorldPosition());
var distance = targetScreenPos.distance(objScreenPos);
if (distance < script.hitCheckDistance) {
script.backpackImage.getTransform().setLocalScale(new vec3(1.2, 1.2, 1.2));
overlapping = true
} else {
script.backpackImage.getTransform().setLocalScale(new vec3(1, 1, 1));
overlapping = false
}
}
});
global.behaviorSystem.addCustomTriggerResponse("Grab_Detected", function(event){
print("Grab Detected");
dragging = true;
sendEventToUnity("grabDetected");
})
global.behaviorSystem.addCustomTriggerResponse("Grab_Lost", function(event){
print("Grab Lost")
dragging = false;
script.backpackImage.getTransform().setLocalScale(new vec3(1, 1, 1));
if (overlapping) {
print("coin deposited")
overlapping = false
script.coinImage.mainPass.colorMask = new vec4b(false,false,false,false);
var restoreCoinVisibility = script.createEvent("DelayedCallbackEvent");
restoreCoinVisibility.bind(function(){
script.coinImage.mainPass.colorMask = new vec4b(true,true,true,true);
});
restoreCoinVisibility.reset(2);
sendEventToUnity("coinDeposited");
}
})
global.behaviorSystem.addCustomTriggerResponse("RightHandTracking_DETECTED", function(event) {
handDetected("right");
});
global.behaviorSystem.addCustomTriggerResponse("LeftHandTracking_DETECTED", function(event) {
handDetected("left");
});
global.behaviorSystem.addCustomTriggerResponse("RightHandTracking_LOST", function(event) {
handLost("right");
});
global.behaviorSystem.addCustomTriggerResponse("LeftHandTracking_LOST", function(event) {
handLost("left");
});