Eggys Games

News Archive for ‘Game Making Programming’

Unity - C# Class for doing a FadeOut Remove

To save performance you're going to have to remove objects. However having things simply dissapear sometimes is not a good look for the players. A good solution to this is a fadeout. You see it very commonly for mass produced decals such as bulletholes. 

To remove an object instantly you can simply use 

Destroy (gameObject);

But if we want to get a bit more fancy and do the fade out, here is a class you can simply drag and drop onto any sprite you are using in unity to make it fadeout. In the inspector you can set the time until it will dissapear. 

As you can see, first we have to get a reference to the sprite component. We then use Unitys colour class to make a reference to it's alpha property.

Once the countdown you have set in the inspector reaches 0 it will fade out. You can change the speed of its fade next to colourfade however I felt .03 was a good speed, this means it will dissapear in about a second. 

public class DestroyFadeOut : MonoBehaviour {

    public float deathremovetimer;

    private Color mycolour; 
    private float colourfade = 1;

    void Start () {
        mycolour = GetComponent<spriterenderer> ().color;
        mycolour.a = 1f;
    }
    
    void Update () {

            deathremovetimer -= Time.deltaTime;

            if (deathremovetimer <= 0) {
                colourfade -= 0.03f;
                mycolour.a = colourfade;
                GetComponent<spriterenderer> ().color = mycolour;
                if (colourfade <= 0) {
                    Destroy (gameObject);
                }
            }

    }
}

Here we can see how the fadeOut will look

Hope you find this class useful.


Leave a comment Posted in Game Making Programming by Eggy on

Unity - Basic Collisions in C#

In Unity the first thing you're gonna want to be using is collisions. This is what happens when 2 objects collide with each other, or rather two rigidbodies with colliders. Below is all you will need to get started. By adding a rigidbody and a box collider our object can now hit other objects in the scene.

Below I've included the functions that Unity uses to test for collision between objects. Simple copy and paste and then write inside the code you want to execute when these objects collide.

You will see their is triggers and normal. The difference with trigger is that the object will not collide with it in physics space, however it will still execute a hit-test. You use triggers on objects in the background like levers, scenery, doors etc. Where as normal collisions are for enemies and the player. 

These are the two components you need on an object to run a hit-test

Use This code below when you want a hit test between two objects

 //Basic Collision 
 void OnCollisionEnter2D (Collision2D col) {
    if (col.gameObject.tag == "NameOfObject") {
         ///Run Code 
     }
 }

 //Collision Exits
 void OnCollisionExit2D (Collision2D col) {
    if (col.gameObject.tag == "NameOfObject") {
         ///Run Code 
     }
 }

 //Non-Physics Collision
 void OnTriggerEnter2D (Collider2D col) {
 }

 //Non-Physics Collision Exit
 void OnTriggerExit2D (Collider2D col) {
 }


Leave a comment Posted in Game Making Programming by Eggy on

Unity - Spawning Objects Class in C#

Here is a nice simple class you can use if you want to spawn objects from another object in Unity. It allows you to simply drag and drop the class on an object and then in the inspector set the frequency of spawn, how fast to spawn and what to spawn.

It's quite simple yet very versatile to use in many projects. The timer keeps adding, when it goes over a certain amount you set known as the frequency then it will instantiate the object at its transform position and set the timer back to 0. Simple yet effective.

public class Spawn : MonoBehaviour {


    public Rigidbody2D ObjectToSpawn; 
    public Transform WhereToSpawn; 
    public int frequency; 
    public Vector3 random;

    private int spawntime = 0;

    void Update () {


        random = new Vector3 (Random.Range(0,5), 0, 0);

        spawntime++; 

        if (spawntime > frequency) {
            Instantiate (ObjectToSpawn, WhereToSpawn.position+random, WhereToSpawn.rotation);
            spawntime = 0; 
        }
        
    }

Putting our spawn code to good use by spawning zombies out a door



Leave a comment Posted in Game Making Programming by Eggy on

Sound Variance

So a neat little trick when adding sound in games is randomising the pitch so one sound effect turns into 10. This means the user will always here a slightly different tone and it makes it feel like their is more sound effects added and gives it a more realistic effect. The volume is also using Unitys audio listeners to make the sound volume lower when you are further away, giving a proper ambience. The code below shows this, you'll also notice the sound doesn't activate unless the object is hit at a higher speed, this is good for only activating the sound effect when you push it over or it falls rather then simply touching it gently. 

We also use a Collision Function.

void OnCollisionEnter2D (Collision2D col) {

        if(col.gameObject.tag == "Ground") {

            if (col.relativeVelocity.magnitude > 4) {
                myrandom = Random.Range (0.5f, 1.5f);


                Soccersound.pitch = myrandom;
                Soccersound.Play ();
            }

        }
}
Leave a comment Posted in Game Making Programming by Eggy on