forked from ihsoft/KAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKASModulePhysicChild.cs
More file actions
105 lines (95 loc) · 3.39 KB
/
KASModulePhysicChild.cs
File metadata and controls
105 lines (95 loc) · 3.39 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
using UnityEngine;
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
namespace KAS
{
public class KASModulePhysicChild : PartModule
{
public float mass = 0.01f;
public GameObject physicObj;
private bool physicActive = false;
private Vector3 currentLocalPos;
private Quaternion currentLocalRot;
// Methods
public void Start()
{
KAS_Shared.DebugLog("Start(PhysicChild)");
if (!physicActive)
{
physicObj.AddComponent<Rigidbody>();
physicObj.rigidbody.mass = mass;
physicObj.transform.parent = null;
physicObj.rigidbody.useGravity = true;
physicObj.rigidbody.velocity = this.part.rigidbody.velocity;
physicObj.rigidbody.angularVelocity = this.part.rigidbody.angularVelocity;
FlightGlobals.addPhysicalObject(physicObj);
physicActive = true;
}
else
{
KAS_Shared.DebugWarning("Start(PhysicChild) Physic already started !");
}
}
public void OnPartPack()
{
if (physicActive)
{
KAS_Shared.DebugLog("OnPartPack(PhysicChild)");
currentLocalPos = KAS_Shared.GetLocalPosFrom(physicObj.transform, this.part.transform);
currentLocalRot = KAS_Shared.GetLocalRotFrom(physicObj.transform, this.part.transform);
FlightGlobals.removePhysicalObject(physicObj);
physicObj.rigidbody.isKinematic = true;
physicObj.transform.parent = this.part.transform;
StartCoroutine(WaitPhysicUpdate());
}
}
public void OnPartUnpack()
{
if (physicActive)
{
if (physicObj.rigidbody.isKinematic)
{
KAS_Shared.DebugLog("OnPartUnpack(PhysicChild)");
physicObj.transform.parent = null;
KAS_Shared.SetPartLocalPosRotFrom(physicObj.transform, this.part.transform, currentLocalPos, currentLocalRot);
physicObj.rigidbody.isKinematic = false;
FlightGlobals.addPhysicalObject(physicObj);
StartCoroutine(WaitPhysicUpdate());
}
}
}
private IEnumerator WaitPhysicUpdate()
{
yield return new WaitForFixedUpdate();
KAS_Shared.SetPartLocalPosRotFrom(physicObj.transform, this.part.transform, currentLocalPos, currentLocalRot);
if (physicObj.rigidbody.isKinematic == false)
{
KAS_Shared.DebugLog("WaitPhysicUpdate(PhysicChild) Set velocity to : " + this.part.rigidbody.velocity + " | angular velocity : " + this.part.rigidbody.angularVelocity);
physicObj.rigidbody.angularVelocity = this.part.rigidbody.angularVelocity;
physicObj.rigidbody.velocity = this.part.rigidbody.velocity;
}
}
public void Stop()
{
KAS_Shared.DebugLog("Stop(PhysicChild)");
if (physicActive)
{
FlightGlobals.removePhysicalObject(physicObj);
UnityEngine.Object.Destroy(physicObj.rigidbody);
physicObj.transform.parent = this.part.transform;
physicActive = false;
}
else
{
KAS_Shared.DebugWarning("Stop(PhysicChild) Physic already stopped !");
}
}
private void OnDestroy()
{
KAS_Shared.DebugLog("OnDestroy(PhysicChild)");
if (physicActive) Stop();
}
}
}