Table of Contents

NWH Common Scripts

These scripts are shared between NWH Vehicle Physics 2, Dynamic Water Physics 2 and NWH Aerodynamics.

Cameras

Camera Changer

CameraChanger inspector.
  • CameraChanger iterates over all the Cameras in the VehicleCameras 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 the Vehicle Cameras list. If Auto Find Cameras is enabled it is important that all the cameras are placed as children of the object containing CameraChanger script or otherwise they will not be auto-detected.
  • Cameras can be changed by pressing C (default value). Check Input Setup for more info.
Example CameraChanger setup.
2023/05/14 16:35

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.

2023/05/14 16:35

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 assign Target that the camera will follow. If left empty the CameraMouseDrag will auto-find target VehicleController.
  • Camera can be rotated using LMB, zoomed in-out using MMB and panned using RMB.
2023/05/14 16:35
 

Center of Mass

Variable Center of Mass

VariableCenterOfMass inspector.

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 the Rigidbody inspector.
  • It also has a MassAffector feature where the mass, center of mass and inertia can be impacted by MassAffectors attached to the vehicle. This can be a fuel tank, a piece of cargo, a player, etc. MassAffectors, despite affecting the Rigidbody properties, are not Rigidbodies. 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 IMassAffectors. 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 from IMassAffector to a GameObject that is a child of the vehicle.
  • Tick Use Mass Affectors on the VariableCenterOfMass component.
  • Untick the Use Default … for the properties that the MassAffectors should affect.
2023/05/14 16:35

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 IMassAffectors 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.

2023/05/15 18:06 · nwhcoding

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.

2024/01/22 09:04 · nwhcoding

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 have Settings > Register With Vehicle Changer set to true will get added to the list.
Instant Vehicle Switching
  • Pressing ChangeVehicle (Input Setup) button will switch to the next vehicle in Vehicles 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 the ChangeVehicle (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 is EnterExitPoint 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.
2023/05/14 16:35