BuggyController.cs
5.56 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Valve.VR;
using Valve.VR.InteractionSystem;
namespace Valve.VR.InteractionSystem.Sample
{
public class BuggyController : MonoBehaviour
{
public Transform modelJoystick;
public float joystickRot = 20;
public Transform modelTrigger;
public float triggerRot = 20;
public BuggyBuddy buggy;
public Transform buttonBrake;
public Transform buttonReset;
//ui stuff
public Canvas ui_Canvas;
public Image ui_rpm;
public Image ui_speed;
public RectTransform ui_steer;
public float ui_steerangle;
public Vector2 ui_fillAngles;
public Transform resetToPoint;
public SteamVR_Action_Vector2 actionSteering = SteamVR_Input.GetAction<SteamVR_Action_Vector2>("buggy", "Steering");
public SteamVR_Action_Single actionThrottle = SteamVR_Input.GetAction<SteamVR_Action_Single>("buggy", "Throttle");
public SteamVR_Action_Boolean actionBrake = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("buggy", "Brake");
public SteamVR_Action_Boolean actionReset = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("buggy", "Reset");
private float usteer;
private Interactable interactable;
private Quaternion trigSRot;
private Quaternion joySRot;
private Coroutine resettingRoutine;
private Vector3 initialScale;
private void Start()
{
joySRot = modelJoystick.localRotation;
trigSRot = modelTrigger.localRotation;
interactable = GetComponent<Interactable>();
StartCoroutine(DoBuzz());
buggy.controllerReference = transform;
initialScale = buggy.transform.localScale;
}
private void Update()
{
Vector2 steer = Vector2.zero;
float throttle = 0;
float brake = 0;
bool reset = false;
bool b_brake = false;
bool b_reset = false;
if (interactable.attachedToHand)
{
SteamVR_Input_Sources hand = interactable.attachedToHand.handType;
steer = actionSteering.GetAxis(hand);
throttle = actionThrottle.GetAxis(hand);
b_brake = actionBrake.GetState(hand);
b_reset = actionReset.GetState(hand);
brake = b_brake ? 1 : 0;
reset = actionReset.GetStateDown(hand);
}
if (reset && resettingRoutine == null)
{
resettingRoutine = StartCoroutine(DoReset());
}
if (ui_Canvas != null)
{
ui_Canvas.gameObject.SetActive(interactable.attachedToHand);
usteer = Mathf.Lerp(usteer, steer.x, Time.deltaTime * 9);
ui_steer.localEulerAngles = Vector3.forward * usteer * -ui_steerangle;
ui_rpm.fillAmount = Mathf.Lerp(ui_rpm.fillAmount, Mathf.Lerp(ui_fillAngles.x, ui_fillAngles.y, throttle), Time.deltaTime * 4);
float speedLim = 40;
ui_speed.fillAmount = Mathf.Lerp(ui_fillAngles.x, ui_fillAngles.y, 1 - (Mathf.Exp(-buggy.speed / speedLim)));
}
modelJoystick.localRotation = joySRot;
/*if (input.AttachedHand != null && input.AttachedHand.IsLeft)
{
Joystick.Rotate(steer.y * -joystickRot, steer.x * -joystickRot, 0, Space.Self);
}
else if (input.AttachedHand != null && input.AttachedHand.IsRight)
{
Joystick.Rotate(steer.y * -joystickRot, steer.x * joystickRot, 0, Space.Self);
}
else*/
//{
modelJoystick.Rotate(steer.y * -joystickRot, steer.x * -joystickRot, 0, Space.Self);
//}
modelTrigger.localRotation = trigSRot;
modelTrigger.Rotate(throttle * -triggerRot, 0, 0, Space.Self);
buttonBrake.localScale = new Vector3(1, 1, b_brake ? 0.4f : 1.0f);
buttonReset.localScale = new Vector3(1, 1, b_reset ? 0.4f : 1.0f);
buggy.steer = steer;
buggy.throttle = throttle;
buggy.handBrake = brake;
buggy.controllerReference = transform;
}
private IEnumerator DoReset()
{
float startTime = Time.time;
float overTime = 1f;
float endTime = startTime + overTime;
buggy.transform.position = resetToPoint.transform.position;
buggy.transform.rotation = resetToPoint.transform.rotation;
buggy.transform.localScale = initialScale * 0.1f;
while (Time.time < endTime)
{
buggy.transform.localScale = Vector3.Lerp(buggy.transform.localScale, initialScale, Time.deltaTime * 5f);
yield return null;
}
buggy.transform.localScale = initialScale;
resettingRoutine = null;
}
private float buzztimer;
private IEnumerator DoBuzz()
{
while (true)
{
while (buzztimer < 1)
{
buzztimer += Time.deltaTime * buggy.mvol * 70;
yield return null;
}
buzztimer = 0;
if (interactable.attachedToHand)
{
interactable.attachedToHand.TriggerHapticPulse((ushort)Mathf.RoundToInt(300 * Mathf.Lerp(1.0f, 0.6f, buggy.mvol)));
}
}
}
}
}