NWH Common Scripts
These scripts are shared between NWH Vehicle Physics 2, Dynamic Water Physics 2 and NWH Aerodynamics.
Cameras
Camera Changer
CameraChanger
iterates over all theCamera
s in theVehicleCamera
s list and makes sure that only one camera is active at one time.- It can work with any camera script, not only the ones included with the asset.** This includes e.g. Cinemachine cameras.
Setup
- Attach
CameraChanger
component to the Cameras object. This should be an empty object that is a child of vehicle root. - Tick
Auto Find Cameras
or manually assign cameras to theVehicle Cameras
list. IfAuto Find Cameras
is enabled it is important that all the cameras are placed as children of the object containingCameraChanger
script or otherwise they will not be auto-detected. - Cameras can be changed by pressing
C
(default value). Check Input Setup for more info.
Camera Inside Vehicle
A script that can be attached to any Camera. It tells the vehicle if the camera is inside the vehicle. If it is, low pass filter is applied to imitate the muffled sounds inside the vehicle.
Camera Mouse Drag
The default camera script. Can be used for both 1st and 3rd person.
- A type of
VehicleCamera
.
- Attach to any
Camera
and assignTarget
that the camera will follow. If left empty theCameraMouseDrag
will auto-find target VehicleController.
- Camera can be rotated using LMB, zoomed in-out using MMB and panned using RMB.
Center of Mass
Variable Center of Mass
This is a common script shared between all the vehicle focused NWH assets.
Since Unity 2022 this script is no longer needed to adjust center of mass or inertia from the inspector as those settings are now exposed through Rigidbody inspector.
- Adds an option to adjust the center of mass and inertia tensor of the vehicle
Rigidbody
through the inspector. In Unity 2023 and newer this can be done through theRigidbody
inspector.
- It also has a
MassAffector
feature where the mass, center of mass and inertia can be impacted byMassAffector
s attached to the vehicle. This can be a fuel tank, a piece of cargo, a player, etc.MassAffectors
, despite affecting the Rigidbody properties, are notRigidbodies
. This allows for things like cargo to affect the vehicle, without having the overhead of Rigidbody collisions/joints/etc. VariableCenterOfMass
is designed to give users more control over the Rigidbody which is usually needed for use with vehicles. This is because Unity assumes all objects/colliders have uniform density and therefore calculates the center of mass and inertia using that assumption. This script allows for tweaking of both of these values to better fit the specific vehicle. Center of mass and inertia tensor, besides mass, have the biggest effect on vehicle handling.- The variable part of the name comes from the ability of the script to adjust the mentioned parameters at runtime through
IMassAffector
s. There are components attached to different parts of the vehicle that affect the vehicle mass, the center of mass, and inertia but are not Rigidbodies by themselves. Examples of this would be fuel tanks, passengers, cargo, etc.
Usage
- Attach the
MassAffector
(or any script inheriting fromIMassAffector
to aGameObject
that is a child of the vehicle. - Tick
Use Mass Affectors
on theVariableCenterOfMass
component. - Untick the
Use Default …
for the properties that theMassAffector
s should affect.
Mass Affector
IMassAffector
is an interface that can be inherited by any MonoBehavior and the only parameter it stores is mass. If attached as a child (it does not need to be a direct child) this script will modify the parent Rigidbody mass, center of mass and inertia through its mass and position relative to the Rigidbody.
Any values visible in the VariableCenterOfMass
are displayed without the additional weight of the IMassAffector
s and only in play mode do these get applied. The VariableCenterOfMass
will iterate over child IMassAffector
, calculate the new values and apply them to the Rigidbody
each physics update.
Default implementation is MassAffector
which includes all the base fields.
Shifting Origin
Shifting Origin
A simple shifting origin script. Moves all objects in the scene to keep the player near the origin once the distance exceeds the Distance Threshold
.
Shifting origin is a commonly used technology in most open-world games to combat the degradation of the quality of physics and visuals far from the origin (~1000 units or more). Since Unity uses floats for storing position data the largest value that can be stored is 7 digits. When the distance is large only a limited number of the digits is available on the right side of the decimal point. That means that at ~100km from the origin the position resolution is only 1cm! By moving the object back to [0, 0, 0] each time that the object gets too far away from the origin, and also moving the whole world with it for the same amount, an illusion of travelling great distances is formed while the player never gets further than some threshold from the origin.
Scene Management
Vehicle Changer
Vehicle changer is used to switch between the vehicles. It supports instant switching or character enter/exit type switching, depending on the Character Based
option.
- For script to auto-find vehicles in the scene, tick the
Auto Find Vehicles
option. Vehicles that haveSettings > Register With Vehicle Changer
set totrue
will get added to the list.
Instant Vehicle Switching
- Pressing
ChangeVehicle
(Input Setup) button will switch to the next vehicle inVehicles
list. When the last vehicle is reached it will wrap around and start from the first vehicle again.
Character-based Vehicle Switching
- When
Character Based
switching is enabled the only way to change the vehicle is to exit the current one (some kind of 1st or 3d person character controller is assumed), walk to the next vehicle and press theChangeVehicle
(Input Setup) button.
- Make sure that the
Character Object
is assigned. This is a GameObject containing the character controller. The asset does not care which character controller is used as it will simply deactivate the character controller GameObject and give the control to the vehicle that is being entered. - The points at which the character can enter the vehicle are set by adding empty GameObjects as children to the vehicle and tagging them with the
Enter Exit Tag
, which isEnterExitPoint
by default. Check the demo scene vehicles for the example, e.g. the Niva has the LeftEnterExitPoint only, which means that the character can exit only from the left (driver) side. Duplicate it and move it to the right door position to make entering from the passenger side possible too. - When exiting the vehicle, the character will re-appear at the position it entered. So, if it entered from the left side it will re-appear on the left side when exiting.