Developer Assets Bundle

Creating a game from scratch might include using a basic template of some sorts, so I thought I'd create a bundle of scripts & objects to add onto such a template with useful features like basic force movement (with jumping), spawning, kill boundaries and playing audio.

Image of the scene which shows off the above features in their visual form.


First off is the basic movement for the spheres. They work in two modes, automatic movement or manual movement based on a player's input.

void FixedUpdate () {  
    if(gmControl.autoRollObject) {
        switch(gmControl.rollDirection) {
            case GameController.MoveDirection.Forward:
                rb.AddForce(Vector3.forward);
                break;
            case GameController.MoveDirection.Back:
                rb.AddForce(Vector3.back);
                break;
            case GameController.MoveDirection.Left:
                rb.AddForce(Vector3.left);
                break;
            case GameController.MoveDirection.Right:
                rb.AddForce(Vector3.right);
                break;
        }
    }

    if(gmControl.useKeys) KeyMovement();
}

void KeyMovement() {  
    rb.AddForce(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
}

If autoRollObject results in true. Then the switch block will execute and constantly move the object in the direction based upon the rollDirection variable.
All this is configurable in the GameController or RollObject script.

KeyMovement() is quite simply adding force to the RigidBody based on the player's movements. Using GetAxis, it both Horizontal and Vertical will return between -1 and 1 which is exactly what Vector3.right, Vector3.Back does.


Next up is the spawning of an object. This script has a feature will can enable the spawning of the object anywhere within the actual Spawner's area. If that's disabled then the script will simply spawn the prefab at the location of the GameObject that holds the script

void FixedUpdate () {  
    if(Time.time > nextSpawn && gmControl.spawnObjects) {
        nextSpawn = Time.time + gmControl.spawnInterval;

        if(gmControl.spawnRandomLocation) SpawnPrefabRandom();
        else Instantiate(prefabToSpawn, new Vector3(0f, 0f, 0f), Quaternion.Euler(0f, 0f, 0f));
    }
}

void SpawnPrefabRandom() {  
    //Set the location to a random point inside the spawn zone
    Vector3 randPos;
    randPos = new Vector3(Random.Range(-1f, 1f), prefabToSpawn.transform.position.y - 0.65f, Random.Range(-1f, 1f));
    randPos = transform.TransformPoint(randPos * .5f);

    Instantiate(prefabToSpawn, randPos, Quaternion.Euler(0f, 0f, 0f));
}

SpawnPrefabRandom() works on the basis of randomly generating a float value between -1 and 1, the values are like this because as far as the script is concerned, that's the size of the area of the GameObject. The scale can be any number and the script will still be able to pick a random location within the boundary of the GameObject.


It's not always best to leave Unity to destroy objects that fall into the void. So I generally have a script that if any objects fall into the void, it'll pass through a GameObject first which will simply destroy it manually...

void OnTriggerEnter(Collider other) {  
    if(!gmControl.killEverything) {
        if(gmControl.killTheseObjects.Contains(other.gameObject.tag)) Destroy(other.gameObject);
    } else Destroy(other.gameObject);
}

The above code has a special feature to it, which is the ability to only destroy certain objects if their tag is in a List (see below). If the List will be used, disabling killEverything might be a good idea.


This is an extension of the basic movement system above which allows for jumping, in it's own script so it can be ignored completely if the game doesn't need to support jumping

void Update() {  
    if(Input.GetButtonDown("Jump") && gmControl.jumpObject) jInput = true;
}

void FixedUpdate () {  
    if(jInput && CheckGrounded()) {
        jInput = false;
        rb.velocity = new Vector3(rb.velocity.x, gmControl.jumpHeight, rb.velocity.z);
    }
}

//Raycast downwards to check if we're on the ground
bool CheckGrounded() {  
    return Physics.Raycast(transform.position, Vector3.down, 1f);
}

If the player attempts to jump, jInput is set to true which then in FixedUpdate, checks for that and if the player is grounded (CheckGrounded()). If both return true, then it's just a matter of resetting the variable and changing the velocity of the RigidBody to a new Vector3 with the same values except swapping the y-axis for jumpHeight.


Last is simply a method of playing audio in it's simplest form. There's no special features about this and all the settings will need to be manually modified.

void PlayClip(AudioClip clip) {  
     audSource.PlayOneShot(clip);
}

All that needs to be done is a reference to the script itself and calling the above function with the audio file (AudioClip) as the argument.



To wrap this up, the repo link can be found here, and the download for this (rather than downloading the entire repo) can be found here. (Just need to download the .unitypackage file)

Tom Lynn

Read more posts by this author.

Australia http://rubbix.net