diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index 6c74ba17..9cccbea1 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -223,7 +223,12 @@ namespace NewHorizons.Builder.Props if (!detail.keepLoaded) GroupsBuilder.Make(prop, sector); prop.SetActive(true); - if (detail.hasPhysics) prop.AddComponent(); + if (detail.hasPhysics || true) + { + var addPhysics = prop.AddComponent(); + addPhysics.Sector = sector; + addPhysics.Radius = detail.physicsRadius; + } _detailInfoToCorrespondingSpawnedGameObject[detail] = prop; @@ -399,32 +404,44 @@ namespace NewHorizons.Builder.Props // TODO: mass private class AddPhysics : MonoBehaviour { + public Sector Sector; + public float? Radius; + private IEnumerator Start() { + if (!Sector) + Logger.LogError($"Prop {name} has physics but no sector! This will fall through things when surrounding area is unloaded"); + yield return new WaitForSeconds(.1f); var parentBody = GetComponentInParent(); + // just disable all non triggers foreach (var meshCollider in GetComponentsInChildren(true)) - // hack. doesnt work for all meshes but seems to for most if (!meshCollider.isTrigger) - meshCollider.convex = true; + meshCollider.enabled = false; - var owRigidbody = gameObject.AddComponent(); - owRigidbody.SetVelocity(parentBody.GetPointVelocity(transform.position)); + var bodyGo = new GameObject($"{name}_Body"); + bodyGo.SetActive(false); + bodyGo.transform.position = gameObject.transform.position; + bodyGo.transform.rotation = gameObject.transform.rotation; + + var owRigidbody = bodyGo.AddComponent(); + owRigidbody._simulateInSector = Sector; var detector = new GameObject("Detector"); - detector.transform.SetParent(transform, false); + detector.transform.SetParent(bodyGo.transform, false); detector.layer = LayerMask.NameToLayer("AdvancedDetector"); detector.tag = "DynamicPropDetector"; - detector.AddComponent(); + var sphereCollider = detector.AddComponent(); + if (Radius.HasValue) + sphereCollider.radius = Radius.Value; detector.AddComponent(); detector.AddComponent(); - var impactSensor = gameObject.AddComponent(); + var impactSensor = bodyGo.AddComponent(); var impactAudio = new GameObject("ImpactAudio"); - impactAudio.transform.SetParent(transform, false); - impactAudio.SetActive(false); + impactAudio.transform.SetParent(bodyGo.transform, false); var audioSource = impactAudio.AddComponent(); audioSource.maxDistance = 30; audioSource.dopplerLevel = 0; @@ -433,12 +450,16 @@ namespace NewHorizons.Builder.Props audioSource.spatialBlend = 1; var owAudioSource = impactAudio.AddComponent(); owAudioSource._audioSource = audioSource; - owAudioSource.SetTrack(OWAudioMixer.TrackName.Environment); + owAudioSource._track = OWAudioMixer.TrackName.Environment; var objectImpactAudio = impactAudio.AddComponent(); objectImpactAudio._minPitch = 0.4f; objectImpactAudio._maxPitch = 0.6f; objectImpactAudio._impactSensor = impactSensor; - impactAudio.SetActive(true); + + bodyGo.SetActive(true); + + owRigidbody.SetVelocity(parentBody.GetPointVelocity(transform.position)); + transform.SetParent(bodyGo.transform, false); Destroy(this); } diff --git a/NewHorizons/External/Modules/PropModule.cs b/NewHorizons/External/Modules/PropModule.cs index 2883634a..01fac39b 100644 --- a/NewHorizons/External/Modules/PropModule.cs +++ b/NewHorizons/External/Modules/PropModule.cs @@ -234,6 +234,11 @@ namespace NewHorizons.External.Modules /// Should this object dynamically move around /// public bool hasPhysics; + + /// + /// Optionally create a SphereCollider of the given radius that physics will use for collision + /// + public float? physicsRadius; } [JsonObject]