NWH Common
Search Results for

    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:

    1. Add CameraChanger to a GameObject in scene
    2. Assign all cameras to the Cameras list
    3. Set initial active camera index
    4. 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:

    1. Add CameraMouseDrag to Camera GameObject
    2. Configure sensitivity and input settings
    3. 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):

    1. Create empty GameObject at vehicle position
    2. Make Camera a child of this GameObject
    3. Add CameraMouseDrag to Camera
    4. 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:

    1. Add VehicleCamera component to Camera
    2. Assign target vehicle
    3. Select camera mode
    4. 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:

    1. Create Cameras:

      • Create 3 Camera GameObjects
      • Position them at different angles around vehicle
      • Disable Audio Listener on 2 of them (Unity only allows one)
    2. Setup CameraChanger:

      GameObject "CameraManager"
      └─ CameraChanger component
         ├─ Cameras[0] → Front Camera
         ├─ Cameras[1] → Side Camera
         └─ Cameras[2] → Rear Camera
      
    3. Test:

      • 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:

    1. Setup Hierarchy:

      GameObject "Orbit Pivot" (at vehicle position)
      └─ Camera
         ├─ CameraMouseDrag component
         └─ Position: (0, 2, -5)  // Behind and above pivot
      
    2. Configure CameraMouseDrag:

      • Mouse Button: Right (1)
      • Sensitivity: 3.0
      • Min Vertical Angle: -30
      • Max Vertical Angle: 80
    3. Usage:

      • Right-click-drag to rotate camera
      • Camera orbits around pivot point

    Example 3: Multi-Camera Vehicle Setup

    Complete camera setup for vehicle game:

    1. Chase Camera (main):

      • Follows behind vehicle
      • Uses VehicleCamera in Dynamic mode
      • Speed-based FOV
    2. Hood Camera:

      • Fixed to vehicle hood
      • Parent Camera to vehicle
      • Low FOV for realistic feel
    3. Orbit Camera:

      • Free look with CameraMouseDrag
      • Useful for inspecting vehicle
    4. Top-Down Camera:

      • Useful for parking scenarios
      • Fixed position above vehicle
    5. 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

    1. Audio Listener:

      • Only ONE camera should have Audio Listener
      • CameraChanger doesn't manage Audio Listeners
      • Manually disable Audio Listener on inactive cameras
    2. Performance:

      • Disable inactive cameras (CameraChanger does this automatically)
      • Use occlusion culling for complex scenes
      • Reduce shadow distance for far cameras
    3. Smooth Transitions:

      • Consider adding camera transition effects
      • Fade to black between camera switches
      • Use Animation or Cinemachine for complex transitions
    4. 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
    5. 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:

    • CameraChanger
    • CameraMouseDrag
    • VehicleCamera
    • Edit this page
    In this article
    Back to top Copyright © NWH - Vehicle Physics, Aerodynamics, Dynamic Water Physics