-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyLock.cs
115 lines (103 loc) · 3.95 KB
/
KeyLock.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
/*
* Copyright 2021 AlexOttr <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using UdonSharp;
using UnityEngine;
namespace OttrOne.UdonToolbox
{
/// <summary>
/// Open a lock by positioning a key inside / close to it.
/// </summary>
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class KeyLock : UdonSharpBehaviour
{
[Header("Configuration")]
[SerializeField]
private GameObject Lock;
[SerializeField, Tooltip("Ignore transform information and only react to trigger collider in Lock.")]
private bool ColliderLock;
[SerializeField, Range(0, 4f)]
private float Range = 0.1f;
[Header("Unlocks")]
[SerializeField]
private GameObject[] LockedObjects;
[SerializeField, Tooltip("Expects bool variable 'open' to be false by default.")]
private Animator Anim;
// optimization of the Update loop
private bool unlocked = false;
private Vector3 keyOriginPos;
private Quaternion keyOriginRot;
private void Start()
{
keyOriginPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
keyOriginRot = new Quaternion(transform.rotation.x, transform.rotation.y, transform.rotation.z, transform.rotation.w);
}
/// <summary>
/// Check the distance between the key and the lock in each frame.
/// </summary>
public void Update()
{
if (unlocked) return;
if (ColliderLock) return;
if (Vector3.Distance(transform.position, Lock.transform.position) <= Range)
{
this._Open();
}
}
/// <summary>
/// Open the lock and activate the objects / play animation
/// </summary>
public void _Open()
{
foreach (GameObject obj in LockedObjects)
{
obj.SetActive(true);
}
if (Anim != null)
{
Anim.SetBool("open", true);
}
unlocked = true;
}
/// <summary>
/// Close the lock and deactivate the objects / play animation
/// </summary>
public void _Close()
{
foreach (GameObject obj in LockedObjects)
{
obj.SetActive(false);
}
if (Anim != null)
{
Anim.SetBool("open", false);
}
unlocked = false;
}
private void OnTriggerEnter(Collider otherCollider)
{
if (!ColliderLock) return;
var LockCollider = Lock.GetComponent(typeof(Collider));
if (LockCollider == otherCollider) this._Open();
}
/// <summary>
/// Reset key location
/// </summary>
public void _ResetKey()
{
transform.SetPositionAndRotation(keyOriginPos, keyOriginRot);
}
}
}