Moving and hurting
This page uses Vector3 from Unity. The glossary says what each one is.
These calls move an NPC, kill it, or make it react to a hit.
NpcActions moving and hurting an NPC
Call Teleport to move the NPC to a spot, Kill to make it dead, or Hit to make it react to a hit.
public static class NpcActions
{
public static bool Teleport(int npcId, Vector3 position);
public static bool Kill(int npcId);
public static bool Hit(int npcId);
}
| Method | Returns | What it does |
|---|---|---|
Teleport(npcId, position) | bool | Moves the NPC to a spot in the scene. |
Kill(npcId) | bool | Stops the NPC and puts it in the dead state. |
Hit(npcId) | bool | Makes the NPC react as if the player hit it. |
Things to know
Killends what the NPC was doing and makes it dead, and the body stays where it is. The NPC stops moving and talking, and the framework’sNpcs.Diedevent fires, because it is the same change the game makes. The base game never kills an NPC this way, so it is something only mods do. (It stops the current activity and the path, then calls the publicNPCInfo.Kill(), which callsChangeState(State.Dead). The game’s Dead state turns off the controller, head look, bark, interaction and field of view.)Killgivesfalsefor an NPC that is already dead. Killing it again would do nothing in the game, so it is not reported as done.Hitmakes the NPC react the way the game does when the player hits it with a melee weapon, but it does not damage it. It givestrueonce it has run all the steps, in this order:- The NPC’s “on hit” trigger fires (a scene logic output).
- Unless the NPC is set not to comment on hits, it says a line. That is its own hit line, or the game’s
Generic/Hitline when it has none. It is spoken as a bark, or as a conversation when the NPC is set to start one. - Unless the NPC is set not to give a crime on hits, the player is given a violence crime, and the game checks whether anyone saw it.
- The NPC plays its “hit” animation.
- A police officer starts to chase the player. A robber gives up the robbery. (The steps follow
MeleeWeapon.Hit: theonHitrelay,onHitDialogue(as a bark, or as a dialogue withstartDialogueOnHit),Crime.instance.CommitCrime(npc.character.NPCReference(), 100, Crime.CrimeType.Violence)thenWitness.IsWitnessed(), thehitanimator trigger, andAct_Police.StartChaseorAct_Robber.AbortRobbingread from the NPC’s current activity.)
Hitleaves out what belongs to the weapon. It plays no hit sound or effect, no first-person animation, applies no push, and does no damage. An NPC has no health that the game reads, so nothing about its health changes. (The skipped parts are the weapon’s sound and effect, the first-person animation, the rigidbody force andIDamageReceiverdamage.)
Limits
Teleportmoves the NPC toposition, but it does not stop what the NPC was doing. The NPC may walk back toward its target. The spot is not moved onto the walkable ground, so give a spot that is on it. It givesfalsewhen the NPC cannot be moved this way. (It callsNPCController.richAI.Teleport(position), the same call the game’s forced movement uses, and givesfalsewhen the NPC has noRichAI.positionis not snapped to the navmesh.)- A killed NPC comes back alive after a scene change or a load. The dead state is only kept for the session, and it is not saved. The game keeps no death for an NPC that is away, and no way to bring one back is offered, so plan for the NPC to return. (
NPCInfo.stateis marked[fsIgnore]. Nothing clears the animator’sDeadflag or turns the controller back on, so a wake-up style restore is not possible.) Hitgivesfalsewhen the NPC has no speech component, has no character, or the game has no crime system loaded. It checks the speech component first, so an NPC without one gets no trigger, no line and no crime. (It givesfalsewhen the NPC has noCharacterBarkStarteron its own object (the game runs the whole NPC part only when the struck object has one), noCharacter, orCrime.instanceis null.)Hitcan givefalseafter it has already done some of its steps. If a game call throws an error part way through, the trigger, the line, the crime or the animation may have already happened.