Configures a camera shake; changes affect subsequent starts only.
public sealed class CameraShakeSettingsRemarks
Starts snapshot these settings and apply the documented bounds. Player comfort settings and VR limits can reduce or suppress any shake. A shake ends when the player leaves the world.
/* This example is licensed under the Creative Commons Attribution 4.0 International License. * Copyright 2026 Sansar, Inc. https://creativecommons.org/licenses/by/4.0/ */
using System;using System.Collections.Generic;using Sansar;using Sansar.Script;using Sansar.Simulation;
/// <summary>Demonstrates camera shake with chat commands on the default channel.</summary>/// <remarks>Use /shake rumble, jitter, sway, impact, near, engine or stop./// Engine toggles a continuous rumble for the sender; stop clears this script's shakes for everyone./// An optional CameraComponent on this object fixes the view for current and future users./// Player comfort settings and VR limits can reduce or suppress the motion.</remarks>public class CameraShakeExample : SceneObjectScript{ private CameraComponent camera; private readonly Dictionary<SessionId, CameraShakeHandle> engines = new Dictionary<SessionId, CameraShakeHandle>();
/// <summary>Sets up chat commands and the optional fixed camera.</summary> public override void Init() { ObjectPrivate.TryGetFirstComponent(out camera); ScenePrivate.User.Subscribe(User.AddUser, OnUserAdded); ScenePrivate.User.Subscribe(User.RemoveUser, OnUserRemoved); ScenePrivate.Chat.Subscribe(Chat.DefaultChannel, OnChat, true);
// Script reloads also need to set up users who are already in the world. foreach (AgentPrivate agent in ScenePrivate.GetAgents()) { SetCamera(agent); }
// The Debug local-source live test requires this marker from the real body. Log.Write("CameraShakeExample ready (real shake body): /shake rumble, jitter, sway, impact, near, engine, stop"); }
private void SetCamera(AgentPrivate agent) { if (camera != null && agent != null && agent.IsValid) { agent.Client.SetActiveCamera(camera, ScriptCameraControlMode.AgentControlOnly); } }
private void OnUserAdded(UserData data) { SetCamera(ScenePrivate.FindAgent(data.User)); }
private void OnUserRemoved(UserData data) { // Leaving the world ends that player's shake; discard the old session's handle. engines.Remove(data.User); }
private bool StopEngine(SessionId sessionId) { CameraShakeHandle engine; if (!engines.TryGetValue(sessionId, out engine)) { return false; }
engine.Stop(0.25f); engines.Remove(sessionId); return true; }
private void OnChat(ChatData data) { string[] words = data.Message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (words.Length != 2 || !words[0].Equals("/shake", StringComparison.OrdinalIgnoreCase)) { return; }
// SourceId identifies the sender, rather than the object running this script. AgentPrivate agent = ScenePrivate.FindAgent(data.SourceId); if (agent == null || !agent.IsValid) { return; } SessionId sessionId = agent.AgentInfo.SessionId;
switch (words[1].ToLowerInvariant()) { case "rumble": // Presets are mutable: edit before starting. Starts snapshot the settings. CameraShakeSettings rumble = CameraShakeSettings.Earthquake(); rumble.Direction = new Vector(1, 0.5f, 0); agent.Client.ShakeCamera(rumble); break;
case "jitter": agent.Client.ShakeCamera(new CameraShakeSettings { Pattern = CameraShakePattern.Jitter, Magnitude = 0.25f, Duration = 1.0f // Frequency 0 uses Jitter's default of 20 Hz. }); break;
case "sway": // Long side-to-side motion for the fixed-camera pixel-displacement test. agent.Client.ShakeCamera(new CameraShakeSettings { Pattern = CameraShakePattern.Sway, Magnitude = 0.6f, Frequency = 2.0f, Duration = 6.0f, Direction = new Vector(1, 0, 0), FadeOut = 0.3f }); break;
case "impact": // Landing: one downward kick. Duration 0 would still give just one kick. agent.Client.ShakeCamera(CameraShakeSettings.Impact(new Vector(0, -1, 0))); break;
case "near": // Only players present now receive this explosion. Their camera distance // from this object's world position controls falloff to zero at 20 meters. ScenePrivate.ShakeCameraAtPosition(ObjectPrivate.Position, 20.0f, CameraShakeSettings.Explosion()); break;
case "engine": // Send again to stop; separate sessions retain separate handles. if (!StopEngine(sessionId)) { engines[sessionId] = agent.Client.ShakeCamera(CameraShakeSettings.EngineRumble()); } break;
case "stop": StopEngine(sessionId); // Both stop-all scopes affect only shakes requested by this script. agent.Client.StopAllCameraShakes(); ScenePrivate.StopAllCameraShakes(); engines.Clear(); break; } }}Source: Examples/CameraShakeExample.cs in the Sansar 47.10.0 client ScriptApi snapshot.
Members at a glance
| Kind | Members |
|---|---|
| Constructors | CameraShakeSettings New in 47.10.0 |
| Properties | Direction New in 47.10.0 · Duration New in 47.10.0 · FadeOut New in 47.10.0 · Frequency New in 47.10.0 · Magnitude New in 47.10.0 · Pattern New in 47.10.0 |
| Methods | Earthquake New in 47.10.0 · EngineRumble New in 47.10.0 · Explosion New in 47.10.0 · Impact New in 47.10.0 |
Constructors
CameraShakeSettings
public CameraShakeSettings()Creates a one-second rumble with moderate magnitude.
Remarks
Frequency 0 selects 6 Hz; magnitude is 0.35, direction is (1,1,1) and fade-out is 0.15 seconds.
Properties
Direction
public Sansar.Vector Direction { get; set; }Gets or sets camera-space right/up/forward weights; the default is (1,1,1).
Remarks
X is right, Y is up and Z is forward; W is ignored. Weights are clamped to [-1,1] and vectors longer than 1 are normalized. Zero gives no motion. Signs select the first kick or half-cycle for Impact or Sway.
Duration
public float Duration { get; set; }Gets or sets duration in seconds; 0 means until stopped.
Remarks
The default is 1 second. Values are clamped to 0 through 600; positive values below 0.05 become 0.05. Retain the handle for continuous effects. Impact with Duration 0 is still one decaying kick.
FadeOut
public float FadeOut { get; set; }Gets or sets the ending fade in seconds; the default is 0.15.
Remarks
Clamped to 0 through 5 seconds and shortened to fit Duration. The fade is included in Duration. For Duration 0, Stop specifies the fade.
Frequency
public float Frequency { get; set; }Gets or sets frequency in Hz; 0 selects the pattern default.
Remarks
Defaults are 6 Hz for Rumble, 20 for Jitter, 1.5 for Sway and 5 for Impact. Other values are clamped to 0.1 through 30 Hz.
Magnitude
public float Magnitude { get; set; }Gets or sets relative strength from 0 to 1; the default is 0.35.
Remarks
Values are clamped to this range. This is a relative amount, not a physical impulse.
Pattern
public CameraShakePattern Pattern { get; set; }Gets or sets the motion pattern; the default is Rumble.
Remarks
Choose Rumble, Jitter, Sway or Impact.
Methods
Earthquake
public static CameraShakeSettings Earthquake(
float magnitude,
float duration
)Creates a slow rumble suitable for an earthquake.
| Parameter | Type | Description |
|---|---|---|
magnitude | float | Relative strength from 0 to 1; defaults to 0.35. |
duration | float | Duration in seconds; defaults to 5. Use 0 for until stopped. |
Returns: CameraShakeSettings — A fresh, mutable settings object.
Remarks
Uses 3 Hz and fade-out 0.8 seconds. Edit settings before starting.
EngineRumble
public static CameraShakeSettings EngineRumble(float magnitude)Creates a subtle continuous rumble; retain its handle to stop it.
| Parameter | Type | Description |
|---|---|---|
magnitude | float | Relative strength from 0 to 1; defaults to 0.12. |
Returns: CameraShakeSettings — A fresh, mutable settings object.
Remarks
Uses 16 Hz and Duration 0, until stopped. Edit settings before starting.
Explosion
public static CameraShakeSettings Explosion(float magnitude)Creates a short, strong rumble suitable for an explosion.
| Parameter | Type | Description |
|---|---|---|
magnitude | float | Relative strength from 0 to 1; defaults to 0.7. |
Returns: CameraShakeSettings — A fresh, mutable settings object.
Remarks
Uses 12 Hz, duration 0.65 seconds and fade-out 0.55 seconds. Edit settings before starting.
Impact
public static CameraShakeSettings Impact(
Sansar.Vector direction,
float magnitude
)Creates one kick in the given camera-space right/up/forward direction.
| Parameter | Type | Description |
|---|---|---|
direction | Sansar.Vector | Camera-space right/up/forward weights; W is ignored. |
magnitude | float | Relative strength from 0 to 1; defaults to 0.5. |
Returns: CameraShakeSettings — A fresh, mutable settings object.
Remarks
Uses 5 Hz and duration 0.45 seconds. Changing Duration to 0 still gives only one decaying kick.
Used in examples
CameraShakeExample.cs
Generated from the Sansar 47.10.0 client ScriptApi snapshot.