Scripting
Moments
Play a moment at a position
PlayAt(position) plays a moment's shockwaves and flash lights at a point instead of at the moment's own object, so one moment in the scene serves every grenade. PassForgeMoment.PlayNamedAt("Explosion", position) does it with no reference, for every enabled moment of that name.
using PassForge.Effects;
using UnityEngine;
// A grenade that goes off where it lies. Call Explode from its fuse or its health script.
public class Grenade : MonoBehaviour
{
[SerializeField] PassForgeMoment m_Explosion; // a moment in the scene, Play On: Script Or Event
public void Explode()
{
if (m_Explosion != null)
{
// The moment's shockwaves and flash lights play here, not where the moment's own object is.
m_Explosion.PlayAt(transform.position);
}
else
{
// The same with no moment at all: one line per effect.
PassForgeScreenShake.Shake(0.6f);
PassForgeShockwaves.Spawn(transform.position);
PassForgeFlashLights.Spawn(transform.position, new Color(1f, 0.8f, 0.54f), radius: 8f);
}
Destroy(gameObject); // a moment that has started plays to the end
}
// With no reference: plays every enabled moment named "Explosion" here, so one moment serves every grenade.
public void ExplodeByName()
{
PassForgeMoment.PlayNamedAt("Explosion", transform.position);
Destroy(gameObject);
}
}
One moment for every enemy, with its damage number
Put one "Enemy Hit" moment with a Damage Number step in the scene, and call PassForgeMoment.PlayNamedAt from each enemy's health script: the number shows over the enemy that was hit, and quick hits add up.
using PassForge.Effects;
using UnityEngine;
// On an enemy prefab. One "Enemy Hit" moment in the scene, with a Damage Number step, serves every enemy spawned.
public class EnemyHealth : MonoBehaviour
{
[SerializeField] float m_Health = 100f;
public void TakeHit(float damage)
{
m_Health -= damage;
// The Damage Number step shows the damage over this enemy, where quick hits add up, and the moment's
// shockwaves and flash lights play at the enemy.
PassForgeMoment.PlayNamedAt("Enemy Hit", transform, damage);
}
// A shot that hit the ground or a wall: numbers shown at a point never add up.
public static void ShotHit(Vector3 point, float damage)
{
PassForgeMoment.PlayNamedAt("Bullet Hit", point, damage);
}
}
Run your own code from On Played
A moment raises onPlayed each time it plays and onFinished when a play has run to its end. A VHS Rewind step rewinds only the picture, so moving the player back is the game's own code, run from On Played.
using PassForge.Effects;
using UnityEngine;
// A rewind power. The moment's VHS Rewind step plays the picture going back; moving the player back is the game's
// own code, run from the moment's On Played.
public class RewindPlayer : MonoBehaviour
{
[SerializeField] PassForgeMoment m_Rewind; // with a VHS Rewind step; it records while the moment is enabled
[SerializeField] CharacterController m_Player;
[SerializeField] float m_Seconds = 1.5f; // as long as the rewind step
const float k_Every = 0.05f;
// Where the player was, every 0.05 s, in a ring that never grows, so keeping it allocates nothing.
readonly Vector3[] m_Trail = new Vector3[64];
int m_Newest = -1;
int m_Count;
float m_Next;
void OnEnable() => m_Rewind.onPlayed.AddListener(MoveBack);
void OnDisable() => m_Rewind.onPlayed.RemoveListener(MoveBack);
void Update()
{
if (Time.time < m_Next)
return;
m_Next = Time.time + k_Every;
m_Newest = (m_Newest + 1) % m_Trail.Length;
m_Trail[m_Newest] = m_Player.transform.position;
m_Count = Mathf.Min(m_Count + 1, m_Trail.Length);
}
void MoveBack()
{
if (m_Count == 0)
return;
int back = Mathf.Min(Mathf.RoundToInt(m_Seconds / k_Every), m_Count - 1);
int index = (m_Newest - back + m_Trail.Length) % m_Trail.Length;
// A Character Controller puts its object back where it was unless it is off while the object moves.
m_Player.enabled = false;
m_Player.transform.position = m_Trail[index];
m_Player.enabled = true;
m_Count = 0;
}
}
Wait for a moment to finish
yield return moment.WaitUntilFinished() waits in a coroutine until the play has run to its end, such as a death before the respawn.
using System.Collections;
using PassForge.Effects;
using UnityEngine;
// Plays the death moment to its end, such as slow motion and a fade to black, then respawns.
public class PlayerDeath : MonoBehaviour
{
[SerializeField] PassForgeMoment m_Death;
[SerializeField] Transform m_SpawnPoint;
public void Die() => StartCoroutine(DieThenRespawn());
IEnumerator DieThenRespawn()
{
m_Death.Play();
yield return m_Death.WaitUntilFinished(); // or subscribe to m_Death.onFinished
PassForgeLensBlood.Wipe(); // blood stays on the lens across respawns until wiped
transform.SetPositionAndRotation(m_SpawnPoint.position, m_SpawnPoint.rotation);
}
}
Keep a loop on while health is low
SetPlaying(true) plays a moment and SetPlaying(false) stops it, which suits a looping moment and any UnityEvent with a bool.
using PassForge.Effects;
using UnityEngine;
// Keeps a looping moment on while health is low, such as a heartbeat look set to Repeat.
public class LowHealthWarning : MonoBehaviour
{
[SerializeField] PassForgeMoment m_LowHealth;
bool m_On;
// Call it from the health script whenever health changes.
public void OnHealthChanged(int health, int maxHealth)
{
bool low = health > 0 && health <= maxHealth / 4;
// SetPlaying(true) plays the moment again each time, so change it only when the state changes.
if (low == m_On)
return;
m_On = low;
m_LowHealth.SetPlaying(low);
}
}
Build a moment in code
MomentStep.For(effect) makes a step with the settings a new one gets in the inspector; add it to steps. For a ready made moment at runtime, keep a configured moment in the scene or in a prefab and play it by name.
using PassForge.Effects;
using UnityEngine;
// Builds a moment while the game runs. Each step starts with the settings the inspector gives a new one.
public static class MomentBuilder
{
public static PassForgeMoment AddHitMoment(GameObject owner)
{
var moment = owner.AddComponent<PassForgeMoment>();
moment.momentName = "Hit"; // so PassForgeMoment.PlayNamed("Hit") finds it
var shake = MomentStep.For(MomentEffect.ScreenShake);
shake.strength = 0.3f;
moment.steps.Add(shake);
var light = MomentStep.For(MomentEffect.FlashLight);
light.color = new Color(1f, 0.6f, 0.3f);
light.delay = 0.05f;
moment.steps.Add(light);
return moment;
}
}