Input, time & camera¶
Read the local player's controls, track time, and work with the camera and screen. Everything here is inherently per‑client — it's about this player's machine.
Input — keyboard & mouse¶
Poll input in Update. Input reads the local player only (the one whose client is running the script).
protected override void Update()
{
if (Input.GetKeyDown("space")) Jump(); // fired the frame it goes down
if (Input.GetKey("w")) Move(transform.forward); // held this frame
float h = Input.GetAxis("Horizontal"); // -1..1, smoothed (WASD / stick)
float v = Input.GetAxis("Vertical");
if (Input.GetMouseButtonDown(0)) Fire(); // 0 left, 1 right, 2 middle
Vector3 m = Input.mousePosition; // pixel position of the cursor
}
| Read | |
|---|---|
GetKey(name) · GetKeyDown · GetKeyUp |
held / pressed‑this‑frame / released |
GetButton(name) · GetButtonDown · GetButtonUp |
named input buttons |
GetAxis(name) · GetAxisRaw |
analog axes ("Horizontal", "Vertical", …) |
GetMouseButton(i) · GetMouseButtonDown/Up |
mouse buttons (0/1/2) |
mousePosition · anyKey · anyKeyDown |
cursor & "did anything happen" |
Input is local & desktop‑oriented
Input reflects the player physically at the keyboard/mouse. In VR, prefer driving gameplay from world interactions (triggers, grabbable world objects) and Player reads rather than raw key polling. Use Input for desktop‑mode controls and tools.
Time — timing & frame‑rate independence¶
Multiply per‑frame motion by Time.deltaTime so it runs the same on every machine regardless of frame rate.
transform.position += transform.forward * speed * Time.deltaTime; // metres per second
Time.deltaTime |
seconds since last frame — multiply movement by this |
Time.fixedDeltaTime |
physics step length (use in FixedUpdate) |
Time.time |
seconds since the world started |
Time.unscaledTime / unscaledDeltaTime |
ignores timeScale (UI/menus) |
Time.timeScale |
global speed (1 normal, 0 paused, 0.5 slow‑mo) |
Time.frameCount · realtimeSinceStartup |
frame index / wall‑clock |
Stopwatch measures elapsed real time precisely (mini‑games, timers):
Stopwatch sw = Stopwatch.StartNew();
// ... do something ...
long ms = sw.ElapsedMilliseconds;
sw.Restart();
Delays — use a coroutine, not a busy loop
To wait, yield return new WaitForSeconds(2f); inside a coroutine — never spin in a while loop counting Time.time (it blocks the frame and the VM will throttle/kill it). See Basics.
Camera — the player's viewpoint¶
Camera.main is the local player's camera. Convert between world and screen space and cast rays from the cursor:
Camera cam = Camera.main;
// Click-to-select: ray from the cursor into the world
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray.origin, ray.direction, out hit, 100f))
Debug.Log("Clicked " + hit.collider.gameObject.name);
// Place a UI marker over a world object
Vector3 screen = cam.WorldToScreenPoint(target.position);
bool onScreen = screen.z > 0;
Camera.main |
the active camera |
ScreenPointToRay(px) · ViewportPointToRay |
ray from screen/viewport point |
ScreenToWorldPoint · WorldToScreenPoint |
convert spaces |
WorldToViewportPoint · ViewportToWorldPoint |
0..1 viewport coords |
fieldOfView · nearClipPlane · farClipPlane · orthographic |
lens settings |
Don't fight the player's camera
The VR/desktop rig owns the camera. Read from it freely (raycasts, projection); avoid moving it or changing FOV during play unless your world genuinely needs a custom view — it can cause discomfort in VR.
Screen & Application¶
Screen—width,height(pixels),dpi,fullScreen. Usewidth/heightto position UI or check aspect ratio.Application—isFocused,isPlaying,platform. Checkplatformto branch desktop vs VR behaviour.
Quick reference¶
| Group | Calls |
|---|---|
| Keys | Input.GetKey/Down/Up · GetButton/Down/Up · GetAxis · GetAxisRaw |
| Mouse | Input.GetMouseButton/Down/Up · mousePosition · anyKey · anyKeyDown |
| Time | Time.deltaTime · fixedDeltaTime · time · timeScale · unscaledTime · frameCount |
| Stopwatch | StartNew · ElapsedMilliseconds · Restart · Stop · Reset |
| Camera | Camera.main · ScreenPointToRay · WorldToScreenPoint · ScreenToWorldPoint · fieldOfView |
| Screen/App | Screen.width/height/dpi · Application.platform/isFocused |
→ Feeds naturally into Physics (cursor raycasts) and UI (screen‑space placement).