Home > Net >  Spawning keys and specific doors in endless runner game
Spawning keys and specific doors in endless runner game

Time:12-28

Currently developing endless runner game where you have to dodge the obstacles and once in a while, a key will spawn and you have to pick which door belong to the key you just obtained.

My problem is, I have no idea how to spawn the key and also make sure that at least 1 door that can be opened using the key is spawned couple distance after the key spawn point.

Please help me as I've reach a dead end here and I'm not quite good at game dev to add bunch of codes randomly as I'm afraid I'd make it worse.

This is the key and door system that I made. You can only open the door based on the key(capsule) you collect. For example, red capsule to open red door.

CodePudding user response:

Whenever you spawn a door (which the player will encounter in the future) you also spawn a key between the player and that door. Make sure to create your maze well in advance of player reaching the "edge" and do not spawn the key next to the door or next to the player.

In Unity you usually want to spawn all tiles next to tiles that the player can see. If your player can see the distance equal to one side of a tile, you need to make sure that all tiles next to the tile the player walks into have been spawned. In this case, I would suggest you also spawn squares beyond those squares, placing a door on a "far away" square and a key on the recently spawned square (that the player just can start seeing).

This has a downside - the player will be able to predict in what direction the door will be (approximately) when the player finds the key.

If you spawn the full map, you can probably spawn keys and doors to your liking - the player will be able to find a door and lack the key, later find the key and remember (or not) where the player found the door.

The system you have created seems just fine.

CodePudding user response:

if you make the keys into prefabs, you can have a script attach to it that has a ID

public class Key: MonoBehaviour
{
public int _ID = 0;
}

and you can change the id for each prefabs before you create them, when you check if the keys match the door you check the ID

you can instantiate the prefabs to spawn them

instantiate(prefab,position,Quaternion.identity)

and depending on how you do the logic for spawning the doors and keys, you keep track of what id is already spawned and go from there

  • Related