-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGarbageBuoyancy.cs
More file actions
54 lines (43 loc) · 1.75 KB
/
GarbageBuoyancy.cs
File metadata and controls
54 lines (43 loc) · 1.75 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
using UnityEngine;
public class GarbageBuoyancy : MonoBehaviour {
public RangeFloat force = new RangeFloat(20, 30);
[Range(0, 1)]
public float drag;
public bool disableGravityScale;
private BoxCollider boxCollider;
private void Awake() {
boxCollider = GetComponent<BoxCollider>();
}
private void OnTriggerStay(Collider other) {
float height = other.bounds.size.y;
float percentSubmerged = Mathf.InverseLerp(other.bounds.min.y, other.bounds.max.y, transform.position.y);
float percentUnder = Mathf.InverseLerp(boxCollider.bounds.min.y, boxCollider.bounds.max.y, other.bounds.center.y);
other.attachedRigidbody.AddTorque(other.attachedRigidbody.angularVelocity * -drag, ForceMode.VelocityChange);
other.attachedRigidbody.AddForce(other.attachedRigidbody.velocity * -drag, ForceMode.Impulse);
// other.attachedRigidbody.velocity *= drag;
if (percentSubmerged < 1)
other.attachedRigidbody.AddForce(Vector3.up * force.min * percentSubmerged, ForceMode.Acceleration);
else
other.attachedRigidbody.AddForce(Vector3.up * force.GetAt(1 - percentUnder), ForceMode.Acceleration);
}
private void OnTriggerEnter(Collider other) {
if (disableGravityScale) {
GravityScale gravity = other.GetComponent<GravityScale>();
if (gravity != null)
gravity.enabled = false;
}
WaterDetector water = other.GetComponent<WaterDetector>();
if (water != null)
water.isUnderWater = true;
}
private void OnTriggerExit(Collider other) {
if (disableGravityScale) {
GravityScale gravity = other.GetComponent<GravityScale>();
if (gravity != null)
gravity.enabled = true;
}
WaterDetector water = other.GetComponent<WaterDetector>();
if (water != null)
water.isUnderWater = false;
}
}