Physicsbtn.cs
1.73 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Physicsbtn : MonoBehaviour
{
[SerializeField] private float threshold = 0.1f;
[SerializeField] private float deadZone = 0.025f;
private bool _isTriggered;
private bool _isPressed;
private Vector3 _startPos;
private ConfigurableJoint _joint;
public UnityEvent onPressed1, onPressed2, onReleased;
// Start is called before the first frame update
void Start()
{
_startPos = transform.localPosition;
_joint = GetComponent<ConfigurableJoint>();
}
// Update is called once per frame
void Update()
{
if (!_isPressed && GetValue() + threshold >= 1 && !_isTriggered)
Pressed1();
if (_isPressed && GetValue() - threshold <= 0)
Released();
if (!_isPressed && GetValue() + threshold >= 1 && _isTriggered)
Pressed2();
}
private float GetValue()
{
var value = Vector3.Distance(_startPos, transform.localPosition) / _joint.linearLimit.limit;
if (Mathf.Abs(value) < deadZone)
value = 0;
return Mathf.Clamp(value, -1f, 1f);
}
private void Pressed1()
{
_isPressed = true;
onPressed1.Invoke();
Debug.Log(message: "Pressed1");
}
private void Released()
{
if (_isTriggered == false)
_isTriggered = true;
else if (_isTriggered == true)
_isTriggered = false;
_isPressed = false;
onReleased.Invoke();
Debug.Log(message: "Released");
}
private void Pressed2()
{
_isPressed = true;
onPressed2.Invoke();
Debug.Log(message: "Pressed2");
}
}