Tag Archives: effects

Adding a particle system to a 2D game in Unity

In this tutorial we will add a particle system to a 2D game in Unity. Particle systems emit particles in a game and can be used for adding a variety of effects like fire sparks, smoke, rain, and fog. We will use a particle system to emit spark effects.

Watch the video below to see how you can add particle systems to your own 2D game.

Next tutorial: Adding collectables and scoring

Controlling audio with scripts

This tutorial explains how to control the audio in your game using scripts. You can play, stop, pause and resume sound effects and songs in your game when certain events occur by controlling the sound with code. This video explains how to do that and if you scroll down you will find the sample code.

The following sample code is used to control an audio source/slot called “song” that is attached to the same entity that the script is attached to. The filename of this script is playsong.js.

pc.script.create('playsound', function (app) {
    // Creates a new Playsound instance
    var Playsound = function (entity) {
        this.entity = entity;
    };

    Playsound.prototype = {
        // Called once after all resources are loaded and before the first update
        initialize: function () {
        },

        // Called every frame, dt is time in seconds since last update
        update: function (dt) {
            if(app.keyboard.wasPressed(pc.input.KEY_P)){
                this.entity.sound.play('song');
            }
            
            if(app.keyboard.wasPressed(pc.input.KEY_S)){
                this.entity.sound.stop('song');
            }
        }
    };

    return Playsound;
});

The following sample code is used to control an audio source/slot called “song” that is attached to a Sound entity called “mySound“. The script can be attached to any other entity in the game scene. The filename of this script is playsong.js.

pc.script.create('playsound', function (app) {
    // Creates a new Playsound instance
    var Playsound = function (entity) {
        this.entity = entity;
    };

    Playsound.prototype = {
        // Called once after all resources are loaded and before the first update
        initialize: function () {
        },

        // Called every frame, dt is time in seconds since last update
        update: function (dt) {
            // creates variable for audio entity
            var AudioEntity = app.root.findByName('mySound');
            if(app.keyboard.wasPressed(pc.input.KEY_P)){
                AudioEntity.sound.play('song');
            }
            
            if(app.keyboard.wasPressed(pc.input.KEY_S)){
                AudioEntity.sound.stop('song');
            }
        }
    };

    return Playsound;
});

South Of The Border by Audionautix is licensed under a Creative Commons Attribution licence (https://creativecommons.org/licenses/by/4.0/)
Artist: http://audionautix.com/

Adding sound to a game in PlayCanvas

This tutorial explains how to add sound effects and background music to your game in PlayCanvas. You can add sounds that can be heard from anywhere in the game or attach sounds to objects that can only be heard when your character is near them in the game.

You can also add sounds that automatically play and loop in the background and you can also control when sounds play and stop using code. Using code to control sound will be explained in the next tutorial. Watch the video below to see how to add sound to your game.

South Of The Border by Audionautix is licensed under a Creative Commons Attribution licence (https://creativecommons.org/licenses/by/4.0/)
Artist: http://audionautix.com/

Adding sound to your scene

In this tutorial you will learn how add sound to a scene in your game. You can add sounds that play constantly in a scene such as background music and you can also add sounds that only occur every now and then or that occur after an event. Sounds can also be looped or just play once.

There is also 2D sound (heard everywhere in your scene) and 3D sound (heard only when you are in a specified proximity to an object/sound). Sound can be scripted but you can also add sound without any code at all.

Watch the video below or scroll down to read the step-by-step instructions and sample code.

To add audio to your scene, follow these steps:

  1. Make sure there is one (and only one) Audio Listener component in your scene. It should be attached to the character controller’s camera or main camera. Delete or disable any other audio listeners as there can only be one in any scene.
  2. Add an object (eg. empty object, cube, sphere) or use an existing object in your scene to add the sound to.
  3. Select the object and click ‘Add component’ in the Inspector panel.
  4. Add the ‘Audio source’ component to the object.
  5. Find an audio file (eg. MP3, OGG file) and add it to the ‘audio clip’ property on the audio source component in the inspector panel.
  6. To make the sound play throughout the whole scene, enable ‘Play on awake’. You can also check the ‘Loop’ option to make the sound repeat/loop. Play the scene to test your audio.
  7. To use the audio file as 3D audio, drag the ‘Spatial Blend’ slider from 2D to 3D – this means that you will only hear the sound when you are near the object. You can change some of the properties to specify how it fades away and how close to the object the player needs to be to hear the sound.

To script the audio, add this code to the Start() method:

GetComponent<AudioSource> ().Play();

and to stop the audio:

GetComponent<AudioSource> ().Stop ();

You could also add an ‘if’ statement eg. to make the sound play when a key is pressed. For example, you could add this ‘if’ statement to the Update() method.:

if (Input.GetKey(KeyCode.Space)) {
 GetComponent<AudioSource>().Play();
}

Also, you can check if audio is currently playing by using .isPlaying(). Any looping sound or a song can be turned on and off using an if statement that checks if the audio is currently playing – just add something like this to your Update() method.

if (Input.GetKey (KeyCode.Space)) {
 if(GetComponent<AudioSource>().isPlaying == true){
 GetComponent<AudioSource>().Stop ();
 }
 else{
 GetComponent<AudioSource>().Play ();
 }
 }

How to change the audio clip using code?

If you want to change the audio clip on the audio source during the game (maybe after a certain event or when a key is pressed), you will need to add the following line inside the class (outside of all other methods) in your script:

public AudioClip otherClip;

You will then need to stop the current audio clip from playing before you switch to another audio clip and start playing that one. Inside the Update() method (or whichever method you are controlling the audio from), you will need to add the following code. This code may be placed inside an if statement (eg. if x key is pressed):

GetComponent<AudioSource>().Stop ();

Then, after you have added that line of code you can switch the audio clip and start playing the new audio clip using this code:

GetComponent<AudioSource>().clip = otherClip;
GetComponent<AudioSource>().Play ();

Lastly, you will need to go into Unity and drag the audio clip file (eg. MP3, OGG file) onto the ‘otherClip’ property on the script component (for this script) in the Inspector panel.

Advanced Camera Techniques in Unity

In this tutorial we will look at how to add cameras to objects, how to set up a split screen camera system, and how to add a picture-in-picture (PIP) effect to your game. Scroll down to watch the video.

Adding cameras to objects are particularly useful for when you want to add a camera view to a character or game object eg. first person view inside a car.

Split screen camera systems are great for multiplayer games because you can have one player using the top half of the screen and another player using the bottom half of the screen. You could set up two cameras or you could have four cameras taking up the screen if you have four players in the game. Take a look at the example below.

Two players using split screen camera system.
Two players using split screen camera system.

Picture-in-picture (PIP) effects are great for maps and rear-camera views. For example, you could use a camera to display a map in the top corner of the screen so the player knows where they are located in the game’s world. You could also use a PIP effect to display a rear-view mirror so that a player can see behind their car in a racing game. Take a look at the example below.

A picture-in-picture (PIP) effect with a second camera view.
A picture-in-picture (PIP) effect with a second camera view.

We will also look at how to handle errors that you might see related to multiple audio listeners. To find out how to add cameras to objects, add split screen camera systems and PIP effects, watch the video below.

 

How to draw your own objects in Adobe Flash CS6

This video tutorial explains how you can draw your own objects or shapes in Adobe Flash CS6 using a range of drawing tools including the pencil, brush, paint bucket, line, and shape tools. You can also watch the video on YouTube here if your prefer.