codeAPI Reference
s&box ScientistNpc: Fear-based NPC
ScientistNpc Class API Reference
ScientistNpc is an NPC with a fear system that flees when damaged. When not afraid, wanders or inspects nearby props. Implements IDamageable.
Type Signature
CSHARP
public class ScientistNpc : Npc, Component.IDamageable
{
[Property, ClientEditable, Range(1, 100), Sync] public float Health { get; set; } = 100f;
public float AfraidLevel { get; }
[Property, Group("Balance")] private float FearGracePeriod { get; set; } = 3f;
[Property, Group("Balance")] private float FearDecayRate { get; set; } = 0.15f;
}Properties
- float Health { get; set; } = 100f - NPC health
- float AfraidLevel { get; } - Current fear level (0-1), computed from peak fear and time since hurt
- float FearGracePeriod { get; set; } = 3f - Seconds at full fear before decay begins
- float FearDecayRate { get; set; } = 0.15f - Fear units lost per second after grace period
Behavior
Fear System:
- Fear escalates with each hit (damage / 50, clamped to 1)
- Fear stays at peak for grace period, then decays
- When afraid, flees from attacker
- When not afraid, wanders or inspects nearby props
IDamageable Implementation
- OnDamage(in DamageInfo damage) - Reduces health, escalates fear, interrupts schedule, tracks attacker
Usage
CSHARP
// Create a scientist NPC
var npc = GameObject.Components.Create<ScientistNpc>();
npc.Health = 50f;Notes
- Uses schedules: ScientistFleeSchedule, ScientistWanderSchedule, ScientistInspectPropSchedule, ScientistIdleSchedule
- Tracks stats for scares and kills
- Finds nearby props within 2048 units to inspect
- Fear decays over time after grace period
Was this helpful?