Characters and Vehicles¶
This page covers the scripts behind the player prefabs: how a character moves and collides with the map, the three ways to control the player, shooting, the boat and the balloon, and the enemy spawner.
Concept¶
A walking character is built from a few components that each do one job:
| Component | Job |
|---|---|
DirectionalAnimation |
Plays the walk cycle in the direction of movement. See Directional Animation. |
PhysicCharBehaviour |
Moves the character and stops it against blocking tiles. |
| A controller or an AI | Decides where to go. PlayerController reads the keyboard, the AI scripts on the AI page decide on their own. |
IsoSpriteController |
Sorts the sprite in depth so the character walks behind tree tops and roofs. |
Collision is against the map's tile collisions, not Unity physics. The player prefabs still carry
a 3D BoxCollider and Rigidbody, because teleporters and bullets use trigger events.
PhysicCharBehaviour¶
Every frame it moves the character along Dir, then checks a rectangle around the character's
pivot against the map. If the new position overlaps a blocking tile it tries to keep the move on
one axis only, so a character pushed diagonally into a wall slides along it. If both axes are
blocked it goes back to where it was.
A spot blocks when the top tile on a Ground layer there has any collision other than passable or overlay. Wall and fence tiles only block on part of the tile, as set in the tileset. See Tile Collisions.
| Field | What it does |
|---|---|
Dir |
The movement direction, written by the controller or AI every frame. A length of 1 moves at Max Speed. |
Max Speed |
Speed in world units per second for a Dir of length 1. |
Is Coll Enabled |
When off, the character walks through everything. The balloon turns this off while it flies. |
Coll Flags |
Output only. Which sides hit something this frame. The AI scripts read it to turn away from walls. |
Coll Rect |
The collision rectangle in world units, relative to the pivot. Keep it small and around the feet, so the head can overlap the tile above. |
Click the Edit Collider Rect button at the top of the inspector to drag the rectangle's edges
and centre in the Scene view.
To move a character from a script, call TeleportTo rather than setting transform.position.
The behaviour remembers the last valid position, and a direct jump into a blocked spot is undone
on the next frame.
using UnityEngine;
using CreativeSpore.RpgMapEditor;
public class RespawnPoint : MonoBehaviour
{
public void Respawn(PhysicCharBehaviour character)
{
character.TeleportTo(transform.position);
}
}
IsoSpriteController¶
It sets the z of the sprite from its height on the map, between the depth of the last Ground layer and the first Overlay layer. A character lower on the screen is drawn in front of one higher up, and all of them are drawn over the ground and under overlay tiles such as tree tops.
It needs a map with at least one Ground layer and one Overlay layer. Sprite Render is the renderer
it moves. Left empty, it uses the SpriteRenderer on the same GameObject.
Player controllers¶
PlayerController¶
The controller on the Player prefab. It extends CharBasicController, which reads the
Horizontal and Vertical axes through RpgInput (the arrow keys and WASD by default),
writes them to PhysicCharBehaviour.Dir, plays the animation while moving and turns the character
to face the movement. CharBasicController works on its own for a character that only walks.
On top of that, PlayerController:
- Shoots with J (down), H (left), K (right) and U (up).
- Clears fog of war around the player whenever it enters a new tile. See Fog of War.
- Survives scene loads. The first player created is kept with
DontDestroyOnLoad, and a player already placed in the scene you arrive in is destroyed. That is how a player walks through a teleporter into another scene and keeps its state. - Tells the camera to follow it. It needs a camera with
Camera2DControllerandFollowObjectBehaviourin the scene, like thePlayerCameraprefab.
| Field | What it does |
|---|---|
Bullet Prefab |
The object fired by the shoot keys. It needs a BulletController. |
Camera 2D |
The camera to follow the player. Left empty, the first Camera2DController in the scene is used. |
Bullet Ang Dispersion |
Random spread of each shot, in degrees. Most shots land close to straight. |
Shadow Sprite, Weapon Sprite |
Hidden and shown together with the player, for example while it is inside the balloon. |
Fog Sight Length |
How many tiles of fog of war are cleared around the player. |
Vehicle |
The vehicle the player is in, set by the vehicle. Leave it empty. |
Timer Block Dir Set is still in the inspector but nothing reads it, so changing it does nothing.
WeaponController on the same prefab turns and flips the weapon sprite to match the way the
player faces. Weapon Sprite Horizontal and Weapon Sprite Vertical are the two
sprites, and V Off Dir Right and V Off Dir Down hold one offset per animation frame, so the
weapon bobs with the walk.
PlayerTileMovementController¶
The PlayerTileMovement prefab moves one whole tile per arrow key press, RPG Maker style, instead
of moving freely. Hold the key to keep walking. It snaps to the centre of the tile when it stops.
The move is refused when the target tile has any collision other than passable. This checks the
whole tile, so a wall or fence tile blocks the step even where free movement could pass through
part of it. Only the arrow keys step. Shooting, fog of war and vehicles work as for Player.
TileMovementBehaviour does the stepping. Its Player State field shows Idle or Moving. Its
Fog Sight Length is not read; the one on the controller is the one that counts.
Touch and click to move¶
The PlayerTouch prefab adds TouchFollowBehaviour. Click, or tap on a touch screen, and the
player finds a path there with A* and walks it. The spot has to be passable, or the click is
ignored. The keyboard still works too.
It casts the click from Camera.main, so the camera must be tagged MainCamera, as
PlayerCamera is. The pathfinding itself is covered in Pathfinding.
| Field | What it does |
|---|---|
Min Dist To Reach Target |
On the last tile of the path, how close to the clicked point the player stops, in world units. |
The walking on this prefab is done by MovingBehaviour (see AI), so its speed is the
Max Speed of that component, not of PhysicCharBehaviour.
Shooting¶
BulletController moves the bullet in a straight line. It disappears on a tile with block
collision (walls and fences let it through), when its time runs out, or when it hits a target.
| Field | What it does |
|---|---|
Speed |
Units per second. The player overrides it with 4 when it fires. |
Dir |
Set by whoever fires the bullet. |
Time To Live |
Seconds before the bullet disappears on its own. |
Damage Qty |
Damage per hit. The player overrides it with 0.5 when it fires. |
On Destroy Fx |
Optional prefab spawned where the bullet disappears. |
Is Destroy On Collision |
When off, the bullet passes through targets without damaging them. |
A hit needs a 3D collider with a Rigidbody on the target, on a different layer from the
shooter. The bullet takes the shooter's layer when it is fired, so it never hits the one who fired
it. Damage goes to a DamageBehaviour on the target:
| Field | What it does |
|---|---|
Health |
Hit points. The object is destroyed at 0. |
FX Death Prefab |
Spawned on death, removed after 3 seconds. |
Random Drop |
Optional prefab left behind on death. |
God Mode Timer |
Seconds of invulnerability after a hit. The sprite flashes red, and a target with MovingBehaviour is pushed back. |
Vehicles¶
VehicleCharController drives the Boat and Balloon prefabs. Walk up to a vehicle and the hint
Press "Enter" to go inside appears. Press Enter (the Return key) to board, steer with the
same keys as the player, and press Enter again to leave. The key is read through RpgInput, so it
works with either input backend.
You can only leave where the player fits, which means the landing spot must not collide with the
player's Coll Rect and must not be water. The boat drops you one step ahead in the direction you face.
The balloon must also be over a spot where it does not collide. It lands first, then drops you
half a tile below it.
| Field | What it does |
|---|---|
Vehicle Type |
Boat keeps the player visible on board and collides with the map. Aircraft hides the player, flies over everything and clears fog of war as it climbs. |
Help Text |
The object with the hint text that shows when the player is close. |
Shadow Sprite |
Optional shadow that shrinks as an aircraft climbs. |
Max Altitude |
How high an aircraft's sprite rises above its shadow, in units. |
Max Altitude Sight Length |
Tiles of fog of war cleared around an aircraft at full altitude. Less while it climbs. |
Landing Time |
A rate, not seconds. At 1, taking off and landing each take about a second. Higher is faster. Don't set it below 1: the aircraft would then never reach full altitude or finish landing. |
The boat stays on water because it collides with the map like any character. Give your water
autotiles Wall Collision in the tileset. It blocks only at the shore, so the boat can sail the
open water and the player cannot walk into it. The default collision setup of a new autotileset
already does this for the animated water tiles.
Each vehicle looks for the scene's PlayerController when it starts, so place the player in the
scene before you press Play.
Spawning enemies¶
ObjectGenerator on the EnemyGen prefab creates copies of a prefab at its own position.
| Field | What it does |
|---|---|
Prefab |
What to spawn. |
Timer Between Creation |
Seconds between spawns. The first one appears as soon as the scene starts. |
Max Number Of Entities |
How many may be alive at once. A destroyed one frees its place. Below 0 means no limit. |
Offset |
Added to the generator's position. |