SoFunction
Updated on 2025-03-07

Unity implements scene roaming camera

This article shares the specific code of Unity's scene roaming camera for your reference. The specific content is as follows

Preface

After getting the scene, you always like to play in the scene for a while, then this script is your best choice.
Comments are added to the code, which is very convenient to modify.

How to use

Drag the script to the scene camera and use it out of the box.

  • WASD moves back and forth
  • QE is up and down
  • Shift acceleration
  • Right-click to rotate the viewing angle
  • ESC exits the game

Source code

#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
#define USE_INPUT_SYSTEM
 using ;
 using ;
#endif

using UnityEngine;

 public class SimpleCameraController : MonoBehaviour
 {
  #region Camera Status  /// <summary>
  /// Camera status  /// </summary>
  class CameraState
  {
   public float yaw;
   public float pitch;
   public float roll;
   public float x;
   public float y;
   public float z;

   public void SetFromTransform(Transform t)
   {
    pitch = ;
    yaw = ;
    roll = ;
    x = ;
    y = ;
    z = ;
   }

   public void Translate(Vector3 translation)
   {
    Vector3 rotatedTranslation = (pitch, yaw, roll) * translation;

    x += ;
    y += ;
    z += ;
   }

   public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
   {
    yaw = (yaw, , rotationLerpPct);
    pitch = (pitch, , rotationLerpPct);
    roll = (roll, , rotationLerpPct);
    
    x = (x, , positionLerpPct);
    y = (y, , positionLerpPct);
    z = (z, , positionLerpPct);
   }

   public void UpdateTransform(Transform t)
   {
     = new Vector3(pitch, yaw, roll);
     = new Vector3(x, y, z);
   }
  }
  #endregion

  CameraState m_TargetCameraState = new CameraState();
  CameraState m_InterpolatingCameraState = new CameraState();

  [Header("Movement Settings")]
  [Tooltip("Exponential boost factor on translation, controlled by mouse wheel.)]
  public float boost = 3.5f;

  [Tooltip("Time it takes to interpolate camera position 99% of the way to the target.), Range(0.001f, 1f)]
  public float positionLerpTime = 0.2f;

  [Header("Rotation Settings")]
  [Tooltip("X = Change in mouse position. Change mouse position。\nY = Multiplicative factor for camera rotation. Multiplication factor of camera rotation。")]
  public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));

  [Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target.), Range(0.001f, 1f)]
  public float rotationLerpTime = 0.01f;

  [Tooltip("Whether or not to invert our Y axis for mouse input to rotation.)]
  public bool invertY = false;

  void OnEnable()
  {
   m_TargetCameraState.SetFromTransform(transform);
   m_InterpolatingCameraState.SetFromTransform(transform);
  }

  Vector3 GetInputTranslationDirection()
  {
   Vector3 direction = new Vector3();
   if (())
   {
    direction += ;
   }
   if (())
   {
    direction += ;
   }
   if (())
   {
    direction += ;
   }
   if (())
   {
    direction += ;
   }
   if (())
   {
    direction += ;
   }
   if (())
   {
    direction += ;
   }
   return direction;
  }
  
  void Update()
  {
   Vector3 translation = ;

#if ENABLE_LEGACY_INPUT_MANAGER

   // Exit Sample Press the Esc key to exit the game   if (())
   {
    ();
 #if UNITY_EDITOR
  = false; 
 #endif
   }
   // Hide and lock cursor when right mouse button pressed Hide and lock cursor when right mouse button pressed   if ((1))
   {
     = ;
   }

   // Unlock and show cursor when right mouse button released Unlock and show cursor when right mouse button released   if ((1))
   {
     = true;
     = ;
   }

   // Rotation   if ((1))
   {
    var mouseMovement = new Vector2(("Mouse X"), ("Mouse Y") * (invertY ? 1 : -1));
    
    var mouseSensitivityFactor = ();

    m_TargetCameraState.yaw +=  * mouseSensitivityFactor;
    m_TargetCameraState.pitch +=  * mouseSensitivityFactor;
   }
   
   // Translation   translation = GetInputTranslationDirection() * ;

   // Speed ​​up movement when shift key hold Speed ​​up movement when shift key hold   if (())
   {
    //The original speed *10 is the speed after pressing Shift    translation *= 10.0f;
   }

   // Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel)   boost +=  * 0.2f;
   translation *= (2.0f, boost);

#elif USE_INPUT_SYSTEM 
   // TODO: make the new input system work to make the new input system work properly#endif

   m_TargetCameraState.Translate(translation);

   // Framerate-independent interpolation Framerate-independent interpolation   // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time   var positionLerpPct = 1f - (((1f - 0.99f) / positionLerpTime) * );
   var rotationLerpPct = 1f - (((1f - 0.99f) / rotationLerpTime) * );
   m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);

   m_InterpolatingCameraState.UpdateTransform(transform);
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.