Look Upon my Works Ye Mighty and Despair...

I got a lot done over the weekend and I've pretty much finished the majority of the coding... for now... I think.

The main dish was the enemy spawning script and the enemies in general. As I explained in the previous post, I followed a video tutorial to create a script to spawn enemies every 5 seconds. I took that script and changed it to spawn an enemy every 20 units of distance. I did this using like so: 


DistTraveled = player.transform.position.x - CurrentPos;
Spawnrate = 20f;

        if (DistTraveled >= Spawnrate) {
            spawnXpos = player.transform.position.x + 26f; // - the 26f ensures that 
            the obstacle spawns off screen
            CurrentPos = player.transform.position.x; //- this sets the currentPos to 
            the current position of the player, effectively reseting DistTraveled to 0.
            float spawnYpos = Random.Range (pMin, pMax); // - sets a random range for
            the Y value of the obstacle.
            *Spawn the Obstacle using spawnXpos and spawnYpos*

I then added a random chance of the obstacles spawning by creating a function to generate random numbers for each obstacle, and then only allowed the above script to run if the numbers generated where over/under a certain threshold. the code looked something like this:

void Start () {

        InvokeRepeating ("randomRange", 0, 2f);


void randomRanges () {
        Chance = Random.value; // - generates a random value every 2 seconds.

void Update () {

        if ((DistTraveled >= pSpawnrate) && (Chance < 0.3f)) {
    *spawn an obstacle then reset distance travel*

I made 4 obstacles with this script. 

  1. spiders. Slow the player down when they travel through the web. 1/3 chance of appearing.
  2. peacocks. display their feathers when a player gets near, creating a wall that knocks the player down. 1/2 chance of appearing.
  3. crocodile on stilts. If the player goes near the mouth they die, otherwise they just get knocked down if they collide with the stilts. 2/5 chance of appearing.
  4. hanging vine. Like a crocodile but hanging from the roof. If the player touches any part they die. 2/5 chance of appearing.
  5. there is also a 1/5 chance of a crocodile and a vine appearing at the same time and the player having to launch themselves through the thin gap in the middle. This scenario is called Double trouble.

With the scripts and the 4 types of obstacle there is a nice variety to the game now. The actual effects of the obstacles have yet to be implemented and they currently only have placeholder art work (read: any shape i could find) but the game is starting to feel quite exciting.

The next big task was implementing a scoring system. I decided to score the player on distance. For every 10 units of distance travelled they gain a point. I accomplished this using the dark arts of mathf like so: 

score = (Mathf.FloorToInt((currentPos - startingPos)/10)) + PlayerPrefs.GetInt("bonus", 0);

side note: If you need one thing to get good at coding it's mad maths skills. Luckily for me I walk the thin line between "quirky" and Autistic Spectrum Disorder very well so maths is easier for me then talking to people.

The "bonus" playerPrefs is used to hold a value for bonuses. It has to be reset at the start of the scene to stop it carrying across to the next play session but that's as easy as setting "bonus" to zero in the start function. bonus points are unlocked by passing obstacles. Different obstacles have different bonuses. Each of the obstacles have an edge collider on them set as a trigger. using OnTriggerExit sets the playerPref "bonus" to a specific value depending on the colliders tag.


Lastly I stopped the player from moving back on themselves. this prevents the score from going back down to zero and then through to negative numbers. I did this by first checking if the player is moving backwards. If they are, the camera freezes in position. The player is prevented from moving out of the constraints of the screen using mathf.Clamp. Once the player passes the midpoint of the camera again it starts to follow the player. I'm quite proud of the camera script, it's really turned out nice, unlike most things I've done. Here's the whole thing in its entirety. 

Super Camera Script 5000


public class FollowTarg : MonoBehaviour {

    public GameObject player_go;
    Transform player;

    float offsetX;

    float currentPos;

    bool forward = true;

    // Use this for initialization
    void Start () {
        if(player_go == null) {
            Debug.LogError("Couldn't find an object with tag 'Player'!");

        player = player_go.transform;

        offsetX = transform.position.x - player.position.x;
    // Update is called once per frame
    void Update () {
        if((player != null) && (forward)) {
            Vector3 pos = transform.position;
            pos.x = player.position.x + offsetX;
            transform.position = pos;
        Vector3 viewPos = Camera.main.WorldToViewportPoint (player.transform.position);
        viewPos.x = Mathf.Clamp01 (viewPos.x);
        player.transform.position = Camera.main.ViewportToWorldPoint (viewPos);
        if (player.position.x < currentPos) {
            Debug.Log ("moving backwards");

            forward = false;
        } else if ((player.position.x > currentPos) && (forward == false)) {
            StartCoroutine (offScreen(viewPos.x));


        currentPos = player.position.x;

    IEnumerator offScreen (float xpos){
        while (xpos < 0.5f) {
            yield return null;
        forward = true;

I think now I'm going to start working on the art. There's not much that I can do now without knowing how things are going to work. Heres a quick list of art assets I think I'll need, this is more for me than for anyone else so feel free to ignore it, shout at it, use it as inspiration for baby names etc.

  1. backgrounds
  2. main characters
  3. peacock
  4. croc on stilts (IS THE CROCODILE A PANTHER?)
  5. spider
  6. vines
  7. snake/rope