diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index 4404cf79..54fc2fd6 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -236,6 +236,7 @@ namespace NewHorizons.Builder.Props { var addPhysics = prop.AddComponent(); addPhysics.Sector = sector; + addPhysics.Mass = detail.physicsMass; addPhysics.Radius = detail.physicsRadius; } @@ -415,103 +416,64 @@ namespace NewHorizons.Builder.Props } } - // TODO: simulate in sector // BUG: detector collider is not included in groups - // TODO: mass private class AddPhysics : MonoBehaviour { public Sector Sector; - public float? Radius; + public float Mass; + 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"); + Logger.LogError($"Prop {name} has physics but no sector! Will fall through things when surrounding area is unloaded"); - yield return new WaitForSeconds(.1f); + yield return new WaitForSeconds(3f); var parentBody = GetComponentInParent(); // just disable all non triggers - foreach (var meshCollider in GetComponentsInChildren(true)) - if (!meshCollider.isTrigger) - meshCollider.enabled = false; + foreach (var collider in GetComponentsInChildren(true)) + if (!collider.isTrigger) + collider.enabled = false; var bodyGo = new GameObject($"{name}_Body"); bodyGo.SetActive(false); - bodyGo.transform.position = gameObject.transform.position; - bodyGo.transform.rotation = gameObject.transform.rotation; + bodyGo.transform.position = transform.position; + bodyGo.transform.rotation = transform.rotation; var owRigidbody = bodyGo.AddComponent(); owRigidbody._simulateInSector = Sector; - var detector = new GameObject("Detector"); - detector.transform.SetParent(bodyGo.transform, false); - detector.layer = LayerMask.NameToLayer("AdvancedDetector"); - detector.tag = "DynamicPropDetector"; - var sphereCollider = detector.AddComponent(); - if (Radius.HasValue) - sphereCollider.radius = Radius.Value; - detector.AddComponent(); - detector.AddComponent(); + bodyGo.layer = LayerMask.NameToLayer("PhysicalDetector"); + bodyGo.tag = "DynamicPropDetector"; + bodyGo.AddComponent().radius = Radius; + bodyGo.AddComponent(); + bodyGo.AddComponent(); var impactSensor = bodyGo.AddComponent(); - var impactAudio = new GameObject("ImpactAudio"); - impactAudio.transform.SetParent(bodyGo.transform, false); - var audioSource = impactAudio.AddComponent(); + var audioSource = bodyGo.AddComponent(); audioSource.maxDistance = 30; audioSource.dopplerLevel = 0; audioSource.rolloffMode = AudioRolloffMode.Custom; audioSource.playOnAwake = false; audioSource.spatialBlend = 1; - var owAudioSource = impactAudio.AddComponent(); + var owAudioSource = bodyGo.AddComponent(); owAudioSource._audioSource = audioSource; owAudioSource._track = OWAudioMixer.TrackName.Environment; - var objectImpactAudio = impactAudio.AddComponent(); + var objectImpactAudio = bodyGo.AddComponent(); objectImpactAudio._minPitch = 0.4f; objectImpactAudio._maxPitch = 0.6f; objectImpactAudio._impactSensor = impactSensor; bodyGo.SetActive(true); + transform.parent = bodyGo.transform; + owRigidbody.SetMass(Mass); owRigidbody.SetVelocity(parentBody.GetPointVelocity(transform.position)); - transform.SetParent(bodyGo.transform, false); Destroy(this); } } - - /* - private static void AddPhysics(GameObject prop, Sector sector) - { - var primaryBody = prop.GetComponentInParent(); - - var rb = prop.AddComponent(); - var owrb = prop.AddComponent(); - var kine = prop.AddComponent(); - rb.isKinematic = true; - var offsetApplier = prop.AddComponent(); - offsetApplier.Init(owrb); - owrb._simulateInSector = sector; - owrb._kinematicSimulation = true; - owrb._kinematicRigidbody = kine; - owrb._offsetApplier = offsetApplier; - - prop.AddComponent().SetPrimaryBody(primaryBody); - prop.AddComponent().SetBodyToMatch(primaryBody); - - var detector = new GameObject("Detector"); - detector.transform.parent = prop.transform; - detector.transform.localPosition = Vector3.zero; - detector.tag = "DynamicPropDetector"; - - var shape = detector.AddComponent(); - shape.SetCollisionMode(Shape.CollisionMode.Detector); - shape.SetLayer(Shape.Layer.Default); - shape.layerMask = 5; - detector.AddComponent(); - detector.AddComponent(); - } - */ } } \ No newline at end of file diff --git a/NewHorizons/External/Modules/PropModule.cs b/NewHorizons/External/Modules/PropModule.cs index 40fb1ef4..bac024c9 100644 --- a/NewHorizons/External/Modules/PropModule.cs +++ b/NewHorizons/External/Modules/PropModule.cs @@ -231,14 +231,19 @@ namespace NewHorizons.External.Modules public bool keepLoaded; /// - /// Should this object dynamically move around + /// Should this object dynamically move around? /// public bool hasPhysics; /// - /// Optionally create a SphereCollider of the given radius that physics will use for collision + /// The mass of the physics object. /// - public float? physicsRadius; + [DefaultValue(1f)] public float physicsMass = 1f; + + /// + /// The radius that the added sphere collider will use for physics collision. + /// + [DefaultValue(0.5f)] public float physicsRadius = 0.5f; } [JsonObject]