Submarine

A script that can be attached to AdvancedShipController to control
submarine-specific functions. Works with VariableCenterOfMass to adjust buoyancy for depth changes.
Note: VariableCenterOfMass is part of NWH Common. For detailed information, see NWH Common - Variable Center of Mass.
Fields
ballastChangeSpeed- speed of change of the ballast mass, as a percentage ofmaxBallastMass. Controls how fast the submarine can take on or release ballast water.maxBallastMass- maximum ballast mass in kg that can be added to make the submarine sink. Higher values allow diving deeper and faster but require more time to surface.keepHorizontal- if enabled submarine will try to keep horizontal by shifting the center of mass.keepHorizontalSensitivity- sensitivity of calculation trying to keep the submarine horizontal. Higher number will mean faster reaction.maxMassOffset- maximum rigidbody center of mass offset that can be used to keep the submarine level.DepthInput- input property for depth control from -1 (surface) to 1 (dive). Positive values add ballast mass to sink, negative values reduce it to surface.
Setup
- Add a
Submarinecomponent to the same GameObject that hasAdvancedShipController. - Configure the ballast settings: set
maxBallastMassto an appropriate value for your submarine and adjustballastChangeSpeedto control how quickly the submarine changes depth. - Enable
keepHorizontalif you want the submarine to automatically maintain a level orientation. AdjustkeepHorizontalSensitivityandmaxMassOffsetto fine-tune the leveling behavior. - Ensure the vessel has a
VariableCenterOfMasscomponent. For more information, see NWH Common - Variable Center of Mass. - Set up depth input controls through your input system. The
DepthInputproperty accepts values from -1 (surface) to 1 (dive).
Upgrading from v14.1
Prior to v14.2, submarineDepth was clamped to 0..1 and the submarine could not surface. The full -1..1 range is now used everywhere.
If you have a custom mobile or UI slider wired to ShipInputStates.submarineDepth (for example MobileShipInputProvider.submarineDepthSlider), update its minValue to -1. Otherwise, the surface direction is unreachable.
The keyboard and InputSystem bindings shipped with DWP2 are already correct.