Gameplay Scripts

Verificado com a versão: 2017.4

-

Dificuldade: Principiante

Gameplay scripts are a miscellaneous category of scripts to produce gameplay effects. They act on their own, meaning they don’t require Conditions to activate them.

ObjectCreatorArea

description

Requires: BoxCollider2D

description

ObjectCreatorArea is a script that generates new objects from a prefab, in a rectangular area. To work it requires a BoxCollider2D (applied automatically), which defines the area where the instances appear.

The Prefab to Spawn property can accept an object from the Scene, but it’s good practice to create a prefab and assign that instead. If you assign a normal GameObject from the Scene, the component displays a warning.

The Spawn Interval property determines the interval between generated objects, and it’s expressed in seconds.

ObjectShooter

description

Requires: nothing

description

ObjectShooter is a script that allows to propel or shoot a prefab when pressing a key (by default Space). You can use it to create weapons shooting projectiles or lasers, a tennis ball machine, or any kind of item that propels something out repeatedly. It works in conjunction with BulletAttribute (see below).

Often you want to assign this script to an empty object, parent it to another object that has some kind of graphics, and use it as a spawning point. This way you can control exactly where the projectile is emitted from.

The Prefab to Spawn property can accept an object from the Scene, but it’s good practice to create a prefab and assign that instead. If you assign a normal GameObject from the Scene, the component displays a warning.

Creation Rate controls the interval between shots, and it’s in seconds. Shot Speed determines the speed, and Shot Direction the direction as a Vector2.

Relative to Rotation allows the direction mentioned above to be rotated with the GameObject, so if (for instance) you use this script on an object like a spaceship that rotates, the shooting direction is realistic. Leaving it off means the shooting direction is in World Space.

When you apply this script, a green arrow gizmo will be visualised in the Scene View.

The size of the arrow is not connected to the strength of the shot.

Note: You can also set the shooting speed to zero. This way the Player can leave behind objects as it moves. Don’t get fooled by the name of the script which mentions "shooting": the generated object doesn’t need to be a projectile!

Projectile IDs

Finally, ObjectShooter also has the ability to assign a player ID to the projectiles. This works in conjunction with the BulletAttribute script, which needs to be assigned to the projectile prefab that you want to shoot out. Doing so means that if the projectile hits another object that has the DestroyForPointsAttribute script, points are assigned to the correct player.

To make sure ObjectShooter assigns the correct ID, you need to tag the GameObject as Player or Player2.

Note: If you don’t tag the GameObject, the bullet is considered as coming from Player1, so for single-player games you don’t need to worry about tagging.

PickUpAndHold

description

Requires: nothing

description

This script is used to give a character the ability to pick up (and drop) something, like an item or a ball in a sports game. Coupled with clever use of a ConditionArea script, you can create gameplay that revolves around picking up an object and delivering it to a specific area.

To make an object "pickable", you need to assign it the Pickup tag and give it a Collider2D of any kind. You probably want to make the Collider a trigger, otherwise the object collides with the player and is harder to pick up. If the object has a Rigidbody2D, it is made kinematic and stopped before being parented to the character.

Pickup Key and Drop Key define which key needs to be pressed to pickup and drop the item. They match by default, but they don’t need to.

The Pick Up Distance defines the maximum distance that the object can have from the player to be picked up. If multiple pickup objects are present, the closest is picked up.

TimedSelfDestruct

description

Requires: nothing

description

TimedSelfDestruct is a utility script to get rid of objects that are no longer needed in the scene after a certain time. Time to Destruction is the time in seconds after which the object disappears.

Use it on projectiles, and any generated object to make sure the scene doesn’t get cluttered. Also, if your game is generating a lot of objects, in time it might slow down. Putting TimedSelfDestruct on those objects - even if the timer is high - helps the game run better.