-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlyingExp.cs
executable file
·132 lines (122 loc) · 5.24 KB
/
FlyingExp.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class FlyingExp : MonoBehaviour {
public float mass = 70;
public GameObject fakeWingLeft;
public GameObject fakeWingRight;
public Vector3 dragForce;
public bool simulate = false;
public Vector3 acceleration;
public Vector3 velocity;
public float fallDown = 0.0001f;
public float originalY;
float gravity = 9.8f;
public float controlGravity1 = 10f;
public float controlGravity2 = 0.5f;
public GameObject wingA;
public GameObject wingB;
public bool success = false;
public float successHeight = 200f;
public float endPace = 1f;
public AudioSource successAudio;
public AudioSource wingAudioSourceA;
public AudioSource wingAudioSourceB;
public bool hapticFeedback = true;
public float hapticIntensityMultiplier;
public float strengthHaptic;
public GameObject rightController;
public GameObject leftController;
public float timeWaitCredits = 5f;
public GameObject canvasCredit;
protected VRTK_ControllerReference controllerReferenceLeft {
get {
return VRTK_ControllerReference.GetControllerReference(leftController);
}
}
protected VRTK_ControllerReference controllerReferenceRight {
get {
return VRTK_ControllerReference.GetControllerReference(rightController);
}
}
// Use this for initialization
void Start () {
velocity = new Vector3(0f, 0f, 0f);
originalY = this.transform.position.y;
}
bool checkBothWingsHeld() {
if(leftController.transform.parent && rightController.transform.parent) {
bool testA = wingA.transform.IsChildOf(leftController.transform.parent) || wingA.transform.IsChildOf(rightController.transform.parent);
bool testB = wingB.transform.IsChildOf(leftController.transform.parent) || wingB.transform.IsChildOf(rightController.transform.parent);
return testA && testB;
}
return false;
}
protected virtual void TriggerHapticPulse(VRTK_ControllerReference controllerReference, float strength, float duration, float interval) {
float minInterval = 0.05f;
VRTK_ControllerHaptics.TriggerHapticPulse(controllerReference, strength, duration, (interval >= minInterval ? interval : minInterval));
}
IEnumerator PlayCredits(float time) {
yield return new WaitForSeconds(time);
canvasCredit.SetActive(true);
}
// Update is called once per frame
void Update () {
/*
if (!wingAudioSourceA.isPlaying) {
wingAudioSourceA.Play();
wingAudioSourceB.Play();
}
wingAudioSourceA.volume = 0f;
wingAudioSourceB.volume = 0f;*/
if (Input.GetKeyDown(KeyCode.S)) simulate = true;
if (!success)
{
if (checkBothWingsHeld()) {
Vector3 dragForceLeft = fakeWingLeft.GetComponent<GetDragForce>().dragForce;
Vector3 dragForceRight = fakeWingRight.GetComponent<GetDragForce>().dragForce;
dragForce = dragForceLeft + dragForceRight;
float deltaT = Time.deltaTime;
acceleration = new Vector3(0f, 0f, 0f);
if (dragForce.y > 0) {
acceleration = dragForce;
if (hapticFeedback) {
strengthHaptic = 2 * Mathf.Exp(-hapticIntensityMultiplier / dragForce.y);
TriggerHapticPulse(controllerReferenceLeft, strengthHaptic, 0.02f, 0.05f);
TriggerHapticPulse(controllerReferenceRight, strengthHaptic, 0.02f, 0.05f);
wingAudioSourceA.volume = 5f * strengthHaptic;
wingAudioSourceB.volume = 5f * strengthHaptic;
if (!wingAudioSourceA.isPlaying) {
wingAudioSourceA.Play();
wingAudioSourceB.Play();
}
}
}
acceleration -= new Vector3(0, mass * gravity, 0);
acceleration = acceleration / mass;
velocity += acceleration * deltaT;
if (velocity.y < 0) {
velocity = velocity * (1f - controlGravity2 * Mathf.Exp(-Mathf.Pow(this.transform.position.y - originalY, 1) / controlGravity1));
}
Vector3 deltaPosition = velocity * deltaT;
float deltaY = deltaPosition.y;
if (this.transform.position.y + deltaY > originalY) {
this.transform.Translate(new Vector3(0, deltaY, 0));
//this.transform.Translate(deltaPosition);
}
}
}
if(!success && this.transform.position.y > successHeight) {
success = true;
endPace = velocity.y;
successAudio.Play();
StartCoroutine("PlayCredits", timeWaitCredits);
}
if (success) {
float deltaT = Time.deltaTime;
float deltaY = endPace * deltaT;
this.transform.Translate(new Vector3(0, deltaY, 0));
}
}
}