Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new AddWorldForces method for the PrefabUtils class #572

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading