Camera System
NWH Common provides a flexible camera management system for easy switching between multiple cameras.
Overview
The camera system allows:
- Multiple cameras per scene
- Easy switching between cameras
- Mouse-based camera rotation
- Camera-specific activation events
- Integration with input providers
Components
CameraChanger
Main component for managing multiple cameras.
Purpose: Switches between multiple Camera components based on user input.
Setup:
- Add
CameraChangerto a GameObject in scene - Assign all cameras to the
Cameraslist - Set initial active camera index
- Configure input for camera switching
Inspector Fields:
- Cameras - List of Camera components to manage
- Current Camera Index - Currently active camera (0-based)
- Auto-Find Cameras - Automatically populate camera list on start
- Tag - Optional tag to filter auto-found cameras
Usage:
using NWH.Common.Cameras;
CameraChanger changer = FindObjectOfType<CameraChanger>();
// Switch to next camera
changer.NextCamera();
// Switch to previous camera
changer.PreviousCamera();
// Switch to specific camera
changer.SetCamera(2); // Set to camera at index 2
// Get current camera
Camera activeCamera = changer.ActiveCamera;
Input Integration:
CameraChanger automatically uses SceneInputProvider for camera switching:
- Default: C key to cycle through cameras
- Input is handled automatically if SceneInputProvider is in scene
Events:
CameraChanger fires events when cameras change:
public UnityEvent<int> onCameraChanged; // Passes new camera index
Subscribe to events:
cameraChanger.onCameraChanged.AddListener(OnCameraIndexChanged);
void OnCameraIndexChanged(int newIndex)
{
Debug.Log($"Switched to camera {newIndex}");
}
CameraMouseDrag
Provides mouse-based camera rotation and panning.
Purpose: Rotate camera by dragging with mouse, useful for free-look cameras.
Setup:
- Add
CameraMouseDragto Camera GameObject - Configure sensitivity and input settings
- Optionally set rotation limits
Inspector Fields:
- Mouse Button - Which mouse button activates rotation (default: Right)
- Sensitivity - Rotation speed multiplier
- Smooth Time - Rotation damping time (lower = faster response, higher = smoother transitions). Lower = slower lerp speed.
- Invert Y - Invert vertical rotation
- Min Vertical Angle - Minimum vertical rotation (degrees)
- Max Vertical Angle - Maximum vertical rotation (degrees)
Usage:
Automatically handles input when component is enabled. No code required.
Example Setup (Orbit Camera):
- Create empty GameObject at vehicle position
- Make Camera a child of this GameObject
- Add
CameraMouseDragto Camera - Right-click-drag to orbit around vehicle
Advanced Control:
using NWH.Common.Cameras;
CameraMouseDrag mouseDrag = Camera.main.GetComponent<CameraMouseDrag>();
// Disable temporarily
mouseDrag.enabled = false;
// Change sensitivity at runtime
mouseDrag.sensitivity = 5f;
// Reset rotation
mouseDrag.ResetRotation();
VehicleCamera
Advanced camera for vehicle following (included but primarily used by NWH Vehicle Physics 2).
Purpose: Follows vehicle with smooth motion, multiple camera modes, and automatic field of view adjustment.
Modes:
- Dynamic - Smooth follow with speed-based FOV
- Fixed - Static position relative to vehicle
- Top - Top-down view
- OrbitFree - Free orbit camera (mouse controlled)
Setup:
- Add
VehicleCameracomponent to Camera - Assign target vehicle
- Select camera mode
- Configure follow settings
Advanced Features:
- Speed-based FOV adjustment
- Collision avoidance
- Smooth following with damping
- Multiple follow points
- Shake effects
Note: VehicleCamera is primarily documented in NWH Vehicle Physics 2 documentation.
Camera Setup Examples
Example 1: Multiple Static Cameras
Simple setup with fixed camera positions:
Create Cameras:
- Create 3 Camera GameObjects
- Position them at different angles around vehicle
- Disable Audio Listener on 2 of them (Unity only allows one)
Setup CameraChanger:
GameObject "CameraManager" └─ CameraChanger component ├─ Cameras[0] → Front Camera ├─ Cameras[1] → Side Camera └─ Cameras[2] → Rear CameraTest:
- Press C to cycle through cameras
- Only one camera active at a time
Example 2: Orbit Camera with Mouse
Free-look camera that orbits around vehicle:
Setup Hierarchy:
GameObject "Orbit Pivot" (at vehicle position) └─ Camera ├─ CameraMouseDrag component └─ Position: (0, 2, -5) // Behind and above pivotConfigure CameraMouseDrag:
- Mouse Button: Right (1)
- Sensitivity: 3.0
- Min Vertical Angle: -30
- Max Vertical Angle: 80
Usage:
- Right-click-drag to rotate camera
- Camera orbits around pivot point
Example 3: Multi-Camera Vehicle Setup
Complete camera setup for vehicle game:
Chase Camera (main):
- Follows behind vehicle
- Uses VehicleCamera in Dynamic mode
- Speed-based FOV
Hood Camera:
- Fixed to vehicle hood
- Parent Camera to vehicle
- Low FOV for realistic feel
Orbit Camera:
- Free look with CameraMouseDrag
- Useful for inspecting vehicle
Top-Down Camera:
- Useful for parking scenarios
- Fixed position above vehicle
Setup CameraChanger:
cameraChanger.Cameras = new Camera[] { chaseCamera, hoodCamera, orbitCamera, topDownCamera };
Integration with Input System
Camera switching integrates with SceneInputProvider:
Input Manager:
Edit > Project Settings > Input Manager
Add axis "ChangeCamera"
Positive Button: c
Input System:
// In Input Actions asset
[ChangeCamera]
Binding: <Keyboard>/c
Rewired: Configure "ChangeCamera" action in Rewired Input Manager.
Camera Best Practices
Audio Listener:
- Only ONE camera should have Audio Listener
- CameraChanger doesn't manage Audio Listeners
- Manually disable Audio Listener on inactive cameras
Performance:
- Disable inactive cameras (CameraChanger does this automatically)
- Use occlusion culling for complex scenes
- Reduce shadow distance for far cameras
Smooth Transitions:
- Consider adding camera transition effects
- Fade to black between camera switches
- Use Animation or Cinemachine for complex transitions
Mobile Considerations:
- Provide UI buttons for camera switching on mobile
- CameraMouseDrag not suitable for touch (use touch controls instead)
- Consider fewer cameras on mobile for performance
VR/XR:
- Don't use CameraChanger in VR (use XR rig instead)
- Mouse drag incompatible with VR headset tracking
API Reference
For detailed API documentation, see the API reference in the navigation menu for: