diff --git a/NewHorizons/Components/AddPhysics.cs b/NewHorizons/Components/AddPhysics.cs index 963a5bef..a67d7afc 100644 --- a/NewHorizons/Components/AddPhysics.cs +++ b/NewHorizons/Components/AddPhysics.cs @@ -1,7 +1,6 @@ using NewHorizons.Utility.OuterWilds; using System.Collections; using UnityEngine; -using UnityEngine.InputSystem; namespace NewHorizons.Components; @@ -20,16 +19,18 @@ public class AddPhysics : MonoBehaviour "If there's already good colliders on the detail, you can make this 0.")] public float Radius = 1f; - private OWRigidbody parentBody; - private OWRigidbody owRigidbody; + private OWRigidbody _parentBody; + private OWRigidbody _body; + private ImpactSensor _impactSensor; + private bool _maintainVelocity; + private Vector3 _centerOfMass; private IEnumerator Start() { - yield return new WaitForSeconds(.1f); + // detectors dont detect unless we wait for some reason + yield return new WaitForSeconds(10f); - parentBody = GetComponentInParent(); - - // yield return new WaitUntil(() => Keyboard.current.lKey.isPressed); + _parentBody = GetComponentInParent(); // hack: make all mesh colliders convex // triggers are already convex @@ -43,10 +44,10 @@ public class AddPhysics : MonoBehaviour bodyGo.transform.position = transform.position; bodyGo.transform.rotation = transform.rotation; - owRigidbody = bodyGo.AddComponent(); - owRigidbody._simulateInSector = Sector; - // owRigidbody._autoGenerateCenterOfMass = false; - // owRigidbody._centerOfMass = owRigidbody.transform.InverseTransformPoint(parentBody.GetPosition()); + _body = bodyGo.AddComponent(); + _body._simulateInSector = Sector; + _body._autoGenerateCenterOfMass = false; + _body._centerOfMass = _body.transform.InverseTransformPoint(_parentBody.GetWorldCenterOfMass()); bodyGo.layer = Layer.PhysicalDetector; bodyGo.tag = "DynamicPropDetector"; @@ -61,7 +62,7 @@ public class AddPhysics : MonoBehaviour fluidDetector._buoyancy = Locator.GetProbe().GetOWRigidbody()._attachedFluidDetector._buoyancy; fluidDetector._splashEffects = Locator.GetProbe().GetOWRigidbody()._attachedFluidDetector._splashEffects; - var impactSensor = bodyGo.AddComponent(); + _impactSensor = bodyGo.AddComponent(); var audioSource = bodyGo.AddComponent(); audioSource.maxDistance = 30; audioSource.dopplerLevel = 0; @@ -74,23 +75,46 @@ public class AddPhysics : MonoBehaviour var objectImpactAudio = bodyGo.AddComponent(); objectImpactAudio._minPitch = 0.4f; objectImpactAudio._maxPitch = 0.6f; - objectImpactAudio._impactSensor = impactSensor; + objectImpactAudio._impactSensor = _impactSensor; bodyGo.SetActive(true); transform.parent = bodyGo.transform; - owRigidbody.SetMass(Mass); - owRigidbody.SetVelocity(parentBody.GetPointVelocity(owRigidbody.GetWorldCenterOfMass())); + _body.SetMass(Mass); + _body.SetVelocity(_parentBody.GetPointVelocity(_body.GetWorldCenterOfMass())); + _body.SetAngularVelocity(_parentBody.GetAngularVelocity()); + // hack: make center of mass at the parent body so it doesn't drift on rotating body + // _centerOfMass = _body.GetCenterOfMass(); + // _body.SetCenterOfMass(_body.transform.InverseTransformPoint(_parentBody.GetWorldCenterOfMass())); + // _impactSensor.OnImpact += OnImpact; // #536 - Physics objects in bramble dimensions not disabled on load // sectors wait 3 frames and then call OnSectorOccupantsUpdated // however we wait .1 real seconds which is longer // so we have to manually call this - if (owRigidbody._simulateInSector != null) - owRigidbody.OnSectorOccupantsUpdated(); + if (_body._simulateInSector != null) + _body.OnSectorOccupantsUpdated(); - yield return new WaitUntil(() => Keyboard.current.kKey.isPressed); + // it drifts otherwise for some reason + _maintainVelocity = true; + yield return new WaitForSeconds(10f); + _maintainVelocity = false; + } + private void FixedUpdate() + { + if (_maintainVelocity) + { + _body.SetVelocity(_parentBody.GetPointVelocity(_body.GetWorldCenterOfMass())); + _body.SetAngularVelocity(_parentBody.GetAngularVelocity()); + } + } + + private void OnImpact(ImpactData impact) + { + // revert it back to normal + _body.SetCenterOfMass(_centerOfMass); + _impactSensor.OnImpact -= OnImpact; Destroy(this); } @@ -102,12 +126,7 @@ public class AddPhysics : MonoBehaviour private void OnRenderObject() { Popcron.Gizmos.Sphere(transform.position, Radius); - Popcron.Gizmos.Line(transform.position, parentBody.GetPosition()); - Popcron.Gizmos.Line(transform.position, owRigidbody.GetWorldCenterOfMass()); - } - - private void FixedUpdate() - { - // owRigidbody.SetVelocity(parentBody.GetPointVelocity(owRigidbody.GetWorldCenterOfMass())); + if (_body) Popcron.Gizmos.Line(transform.position, _body.GetWorldCenterOfMass(), Color.red); + if (_parentBody) Popcron.Gizmos.Line(transform.position, _parentBody.GetWorldCenterOfMass(), Color.green); } }