OnTrapTrigger
OnTrapTrigger is called when a GameObject steps on an armed BearTrap or Landmine, before the trap arms its delayed fire or triggers. Returning a non-null value cancels the trap's own trigger logic.
object OnTrapTrigger(BearTrap bearTrap, UnityEngine.GameObject obj)Parameters
| Parameter | Type | Description |
|---|---|---|
bearTrap | BearTrap | The BearTrap entity that the object walked into. Use it to read its position, OwnerID, or to disarm or kill it. |
obj | UnityEngine.GameObject | The GameObject that entered the trap, usually a player or an NPC. Convert it with GameObjectEx.ToBaseEntity(obj) to get a BaseEntity, then cast to BasePlayer when you need player data. |
Return value
Returning null lets the trap continue its normal behavior (delayed fire for a bear trap, immediate Trigger for a landmine). Returning any non-null value cancels the trap for that entry, so the target takes no damage and a landmine does not detonate.
How it works
Oxide fires this hook from inside ObjectEntered on both BearTrap and Landmine, and only while the trap is armed. For a bear trap the original code stores the target and schedules a delayed hit a fraction of a second later; for a landmine it resolves the object to a BasePlayer and calls Trigger. In both cases the game only proceeds when the hook returns null, so this is the point to decide whether the trap is allowed to hurt anyone.
The obj argument is a raw GameObject, not an entity. Pass it to GameObjectEx.ToBaseEntity(obj) and cast the result to BasePlayer, BaseEntity, or ScientistNPC depending on what you care about. Many things can walk onto a trap, including NPCs, animals, and dropped objects, so always null-check the result.
You can narrow the trigger by declaring the first parameter as BearTrap to handle only bear traps, or use the Landmine overload for mines. A null return is the normal path; any non-null return stops the trap from firing, which is how admin-safe zones or trap-free regions are enforced.
Other signatures
Oxide also calls OnTrapTrigger from other places in the game with these parameter types. Declare the overload you need, or several:
object OnTrapTrigger(Landmine landmine, UnityEngine.GameObject obj)
Example plugin: Disable traps near safe zones
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Safe Zone Trap Guard", "RustPluginGenerator", "1.0.0")]
[Description("Cancels bear trap and landmine triggers inside a configured safe zone")]
public class SafeZoneTrapGuard : RustPlugin
{
private const string PermAdmin = "safezonetrapguard.admin";
private PluginConfig config;
private class PluginConfig
{
[JsonProperty("Safe zone center X")]
public float CenterX = 0f;
[JsonProperty("Safe zone center Z")]
public float CenterZ = 0f;
[JsonProperty("Safe zone radius (metres)")]
public float Radius = 50f;
}
protected override void LoadDefaultConfig() => config = new PluginConfig();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<PluginConfig>();
if (config == null) throw new Exception();
}
catch
{
PrintWarning("Configuration file is invalid, using defaults");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void SaveConfig() => Config.WriteObject(config);
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["Blocked"] = "Traps are disabled inside the safe zone.",
["ZoneInfo"] = "Safe zone: {0} / {1}, radius {2}m",
["NotConfigured"] = "Safe zone is still at coordinates 0, 0. Set it in oxide/config/SafeZoneTrapGuard.json"
}, this);
}
private string Msg(string key, string userId = null, params object[] args) =>
string.Format(lang.GetMessage(key, this, userId), args);
private void Init()
{
permission.RegisterPermission(PermAdmin, this);
}
private void OnServerInitialized()
{
if (config.CenterX == 0f && config.CenterZ == 0f)
{
PrintWarning(Msg("NotConfigured"));
}
}
private object OnTrapTrigger(BearTrap trap, GameObject obj)
{
if (trap == null || trap.IsDestroyed || obj == null) return null;
BasePlayer player = GameObjectEx.ToBaseEntity(obj) as BasePlayer;
if (player == null || player.IsNpc) return null;
if (!InSafeZone(trap.transform.position)) return null;
player.ChatMessage(Msg("Blocked", player.UserIDString));
return true;
}
private bool InSafeZone(Vector3 position)
{
float dx = position.x - config.CenterX;
float dz = position.z - config.CenterZ;
return (dx * dx + dz * dz) <= config.Radius * config.Radius;
}
[ChatCommand("trapzone")]
private void CmdTrapZone(BasePlayer player, string command, string[] args)
{
if (player == null) return;
if (!player.IsAdmin && !permission.UserHasPermission(player.UserIDString, PermAdmin))
{
return;
}
SendReply(player, Msg("ZoneInfo", player.UserIDString, config.CenterX, config.CenterZ, config.Radius));
}
}
}The plugin keeps one configured safe-zone position and radius. OnTrapTrigger resolves the incoming GameObject to a BasePlayer, ignores NPCs and non-players by returning null, then measures the distance from the trap to the safe-zone center. If the trap sits within the radius, the player receives a chat message from the language file and the hook returns true, which cancels the trap before it fires. An admin chat command shows the current zone, and OnServerInitialized warns if the zone is still at its default coordinates.
Common use cases
Protect new-player areas
Block traps placed within a radius of the spawn beach or a designated safe zone so new players cannot be griefed on arrival.
Owner-only traps
Compare
bearTrap.OwnerIDagainst the player's userID and cancel the trigger when someone steps on their own trap, avoiding friendly-fire accidents in group bases.Audit trap hits
Record which player triggered which trap into the data file to spot trap-spam patterns near monuments, while still returning null so the trap works normally.
Pitfalls
obj is a GameObject, not a BaseEntity
Casting directly to
BasePlayerfails. Go throughGameObjectEx.ToBaseEntity(obj)and null-check the result, because dropped items and animals can also enter a trap.Non-null return disables the trap entirely
Returning
trueor any object means the target takes no damage; it does not reduce damage or redirect it. If you need partial behavior, handle the hit yourself before returning.Fires on every armed entry
Both bear traps and landmines call this hook, and NPCs trigger it too. Never call Puts or PrintWarning inside it on a busy server; keep it to cheap early returns and math.
Build a plugin with OnTrapTrigger
Describe what should happen and get a compiled plugin that uses this hook.
Edit the prompt on the next screen. 2 free plugins a month, no card.
Last updated Oct 26, 2026. Found a mistake? Email support@rustplugingenerator.com.
Build a plugin with OnTrapTrigger
Generate