Gameplay Components

Revisado con versión: 2018.1

-

Dificultad: Principiante

Command System

The Game Kit contains a command system to connect gameplay elements together. It works using three key scripts: SendGameCommand, GameCommandReceive and GameCommandHandler.

The SendGameCommand script decides when something should happen - for example, when the character steps on a pressure pad. It then sends a 'command' which has a specific type, such as Activate, Close, Stop.

This command is then received by GameCommandReceiver script. This script has a collection of different actions that should happen for any given command that it receives. For example, when receiving an Activate command, it performs an action on three handlers that were interested in that particular command.

The GameCommandHandler is where the results of the command are. It might do something like turn a GameObject on or play a sound. It is important that the GameCommandHandler script is on the same GameObject as the GameCommandReceiver script for its reaction. This is how the GameCommandReceiver registers its interest in a particular command.

There are several things that can cause a commend to be sent and so there are several classes which inherit from SendGameCommand to cover these cases. These include: SendOnBecameInvisible, SendOnBecameVisible, SendOnCollisionEnter, SendOnCollisionExit, SendOnCollisionStay,SendOnTriggerEnter, SendOnTriggerExit and SendOnTriggerStay.

There are also several results that can be caused by a command and so there are several classes which inherit from GameCommandHandler. These classes include: MovingPlatform, TriggerUnityEvent, ToggleGameObjectActive, SwitchMaterial, StartPlayableDirector, SimpleTransformer, SimpleRotator, SimpleTranslator, SetGameObjectActive, SetAnimatorTrigger, RespawnPlayer, PlaySound, PlayAnimation, ParticleSystemEmit and GameplayCounter. Note that GameCommandHandler is abstract and so never exists as itself but instead as one of the listed inheriting classes.

For example, if you want a door to open when Ellen stands on a pressure pad, use a SendOnTriggerEnter script to send an Open command to a GameCommandReceiver script when Ellen stands on a pressure pad. The door should animate and make a sound when it opens, so it makes sense to have a SetAnimatorTrigger script and a PlaySound script that listen to the GameCommandReceiver.

Send Game Command

This is the base class for generating commands. As an example, the DoorHuge prefab uses this class to activate a grenadier (open the Assets > 3DGamekit > Scenes > Level1 scene and select the Level01Gameplay > DoorHuge in the Hierarchy window).

  • Interaction Type: Default Setting: None Identifies the type of command to be sent. This is only used for labeling and has no impact on the end effect of the command (a command to open a door could just as easily use the type Spawn as Open so long as the handler was set to react to Spawn). The different types are: None, Activate, Deactivate, Open, Close, Spawn, Destroy, Start, Stop.

  • Interactive Object: This is the Game Command Receiver that collects the different handlers of various commands.

  • One Shot: Default Setting: false Whether or not this command can be sent more than once.

  • Cool Down: Default Setting: 1 If this is not a One Shot command then this determines how long before the command can be sent again.

  • On Send Audio: This can optionally be set to an Audio Source, such sending the command plays an audio clip. You can assign any GameObject that has an Audio Source component to this slot.

  • Audio Delay: Default Setting: 0 If there is an Audio Source set in On Send Audio, this determines how long after the command is sent that the audio plays.

For more information, see Send Game Command sub-types.

Game Command Receiver

This script has a collection of Game Command Types and the handlers that listen for each of them. It is the organising system that connects commands being sent and handled.

You don’t need to set any fields for this script. The commands that are sent to this receiver are controlled by the various Send Game Command scripts, and the handlers that listen for those commands are controlled by the various Game Command Handler scripts.

Game Command Handler

This is the abstract base class for handling commands. Because the class is abstract, it does not exist in its base form but, instead, as one of its child classes. However, all Game Command Handlers have the following fields in common:

  • Interaction Type: Default Setting: None Identifies the type of command to be listened for. It is only used for labeling and has no impact on the end effect of the command (a handler to open a door could just as easily use the type Spawn as Open so long as the sender was also set to send a Spawn command). The different types are: None, Activate, Deactivate, Open, Close, Spawn, Destroy, Start, Stop.

  • Is One Shot: Default Setting: false Whether or not this reaction can happen more than once.

  • Cool Down: Default Setting: 0 If this is not a One Shot handler then this determines how long before this handler can be triggered again.

  • Start Delay: Default Setting: 0 Determines how long after receiving a command that the command is handled.

For more information, see Game Command Handler Sub-types.

Send Game Command Sub-types

The following subsections are classes that you can use instead of the Send Game Command script. Each class contains the Send Game Command fields, and some have additional fields.

Send On Became Invisible

When a Game Object is no longer visible in any camera in the scene, this component will activate and send a command. This script sends its command from the OnBecameInvisible call of the MonoBehaviour class. This script has no additional fields.

Send On Became Visible

When a GameObject becomes visible in any camera in the scene, this component will activate and send a command. This script sends its command from the OnBecameVisible call of the MonoBehaviour class. This script has no additional fields.

Send On Collision Enter

When a GameObject has a collision with another object that is in the specified layer(s), this component will activate and send a command. This script sends its command from the OnCollisionEnter call of the MonoBehaviour class.

  • Layers: A LayerMask for the layers of GameObjects which count for the collision to send the command.

Send On Collision Exit

When a GameObject has a collision with another object that is in the specified layer(s) and then exits that collision, this component will activate and send a command. This script sends its command from the OnCollisionExit call of the MonoBehaviour class.

  • Layers: A LayerMask for the layers of GameObjects which can cause a collision that triggers the component to send a command.

Send On Collision Stay

When this Game Object has a collision with another object that is in the specified layer(s), this component will activate after the SendOnCollisionEnter component has activated and the object is still in a collision state. This script sends its command from the OnCollisionStay call of the MonoBehaviour class.

  • Layers: A LayerMask for the layers of GameObjects which can cause a collision that triggers the component to send a command.

Send On Trigger Enter

If this component is attached to a trigger collider, a collision with another object that is in the specified layer(s) will cause it to activate and send a command. This script sends its command from the OnTriggerEnter call of the MonoBehaviour class.

The following image shows a pressure pad with a cube trigger. The Send Open shows the command that is sent, and the white arrows are leading toward the target object.

  • Layers: A LayerMask for the layers of GameObjects which can cause a collision that triggers the component to send a command.

Send On Trigger Exit

If this component is attached to a trigger collider, a collision with another object that is in the specified layer(s) will cause it to activate and send a command when the game object leaves the collision area. This script sends its command from the OnTriggerExit call of the MonoBehaviour class.

  • Layers: A LayerMask for the layers of GameObjects which count for the trigger event to send the command.

Send On Trigger Stay

If this component is attached to a trigger collider, a collision with another object that is in the specified layer(s) will cause it to activate and send a command every frame that the game object is inside the collision area. This script sends its command from the OnTriggerStay call of the MonoBehaviour class.

  • Layers: A LayerMask for the layers of GameObjects which count for the trigger event to send the command.