Skip to content

Commit

Permalink
feat: Implement new AddWorldForces method for the PrefabUtils class (#…
Browse files Browse the repository at this point in the history
…572)

* Implement new AddWorldForces method

* Add returns documentation
  • Loading branch information
LeeTwentyThree authored Dec 26, 2024
1 parent bde3e55 commit 5b1a2b5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Nautilus/Utility/PrefabUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,30 @@ public static ResourceTracker AddResourceTracker(GameObject gameObject, TechType
return resourceTracker;
}

/// <summary>
/// Adds the World Forces component to the prefab, which is required for proper physics handling.
/// </summary>
/// <param name="prefab">The prefab to modify.</param>
/// <param name="mass">The mass of the new rigidbody (only if a rigidbody is being added).
/// If the prefab already has a Rigidbody, this has NO EFFECT.</param>
/// <param name="underwaterGravity">The underwater gravity in m/s/s.</param>
/// <param name="underwaterDrag">The underwater drag coefficient (using Unity's arbitrary unit for drag).</param>
/// <param name="isKinematic">If true, the Rigidbody will be kinematic when spawned and therefore immovable.
/// Note that if the player picks up an item and drops it, its kinematic state will be reset to false.</param>
/// <returns>A reference to the newly added (or previously existing) <see cref="WorldForces"/> component.</returns>
public static WorldForces AddWorldForces(GameObject prefab, float mass, float underwaterGravity = 1f, float underwaterDrag = 1f, bool isKinematic = false)
{
if (!prefab.TryGetComponent<Rigidbody>(out var rb))
{
rb = prefab.AddComponent<Rigidbody>();
rb.mass = mass;
}
rb.useGravity = false;
rb.isKinematic = isKinematic;
var wf = prefab.EnsureComponent<WorldForces>();
wf.useRigidbody = rb;
wf.underwaterGravity = underwaterGravity;
wf.underwaterDrag = underwaterDrag;
return wf;
}
}

0 comments on commit 5b1a2b5

Please sign in to comment.