Damage System in 3D Game Kit

Проверено с версией:: 2018.1

-

Сложность: Базовая

Damageable

Damageable is the component that allows you to make an object damageable by the weapons in the game. The layer on which the object is will determine which weapon can damage it (each weapon has list of layers it can damage).

InvulnerabilityTime is defaulted to 0. Any amount above 0 is, in seconds, the amount of time the damageable will ignore any damage for that duration once damage has been received.

HitAngle and HitRotation allows you to define the circle arc where attack can damage the object. By default the angle is set to 360 so an attack can damage from everywhere, but setting it to 180 for example will make the object only damageable in a half circle.

You can see a visualisation of the hit area from the red circle in the scene view when your damageable is selected

Example with the Grenadier, which can only be damaged from the back.

The different events can be used to trigger things when they happen. As an example, this is the death event for the breakable boxes:

We disable the box, enable its version made of multiple parts, set it at the right place by parenting and play a sound.

Checkpoints

Can be found in Prefabs > Scenes > Checkpoint, they can be placed in the scene and their colliders resized to make sure the player will enter them when moving through the level.

When the player die, it will be respawned at the last checkpoint it touched.

Note : the player will be respawned at the checkpoint position, so make sure it is above the ground

Damage Zone

Add that component to an object and scale the collider to create a zone in which, when any damageable object enter, they'll get damaged constantly of the setup damage amount.

Death Volume

Add that component and scale the collider to make a volume in which, as soon as the player enter it, they get killed. For example, it is used in the game for the water.

Contact Damager

Contact Damager is used mainly by enemy so they do damage on player when they get in contact with them. Just add it to a GameObject with a collider as trigger.

Note: It is separate from other damaging scripts because it uses a layer system to define what it can damage. This is because for the trigger event to be generated, the object that have that component need to be in a layer that collide with the player. But the enemy layer does not collide with the player (so enemies don't block player movement), so that script need to be added to a child object of the enemy that have a different layer (one that collide with the player) and it's damagedLayer setting must contain the Player Layer

Scenelinked SMB

SceneLinkedSMB is a script that allows you to have an easy fast reference to a MonoBehaviour from a StateMachineBehaviour. Although SceneLinkedSMBs can be used wherever you need to reference a specific MonoBehaviour, they were designed with the idea of separating logic and functionality. An animator controller contains a state machine which is ideal for controlling flow of execution. Using SceneLinkedSMBs you can make calls to public functions on a MonoBehaviour allowing it to control functionality because it can more easily obtain scene references. In this way, SceneLinkedSMBs can control logic as part of a state machine and the MonoBehaviours they are linked to control functionality.

To create a Behaviour using the SceneLinkedSMB:

  1. Create a class that inherits from SceneLinkedSMB with its Generic parameter being the type of MonoBehaviour you want to access in it. For example, for Chomper:

SceneLinkedSMB

  1. Initiate the SceneLinked behaviour in each object using them. For example, in our ChomperBehaviour script Start function, call:

SceneLinkedSMB.Initialise(animator, this);

where animator is the reference of the Animator that will use that SceneLinkedSMB

  1. In your SceneLinkedSMB, the m_Monobehaviour member will point to the object on which that Animator is running.

  2. Override the function you need (Enter, Exit, Update and so on) and implement the behaviour you want.

Tip: Look at any script with SMB in their name for an example on how the project uses the code behaviour linked to each state.