Posts

Showing posts from 2017

Fade in and fade out Script for Unity 3d

Image
A Fader?:
Is your game looking for a bit of polish? Having a simple fade transition from scenes or transition from a point in a game can make all the difference.

The outcome:


The Method:
This uses a Coroutine to fade in/out over a specified time and using a specified amount of 'steps' for a smooth transition. This method takes a Unity UI image and adjusts the alpha until it is 0 or 100%.

/// <summary> /// This is used when a Unity UI image is needed to fade /// </summary> /// <param name="image"></param> /// <param name="fadeIn"></param> /// <param name="fadeTimeSec"></param> /// <returns></returns> IEnumerator Fader(Image image, bool fadeIn, float fadeTimeSec) { // the alpha of the image cannot be set on its own so a new color needs to be created Color tempColor = image.color; // The amount when additioned or subtractioned that oc…

How to set up a world map mouse over highlighting effect using C# in Uinty 3D

Image
The main idea:
The ability to mouse over an item in a game and have a highlighter placed at that position. (clicking or selecting the item will be covered at a different time)

The outcome: (Method 2 shown)

Art assets from the video can be found here: mapflaghighlighter

Method 1:
This uses a Coroutine on game start to create a ray from the mouse position and compares it to a Unity Tag name. Where the highlighter can then be placed. The highlighter only moves from its current position when the ray hits a different positions.

/// This is the good enough way: /// where you dont need an array, only to make sure you set the Tag of all the items in Unity. IEnumerator GetPointsM1() { while (true) { yield return new WaitForSeconds(waitTime); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { //Debug.DrawLine(ray.origin, hit.point); if( hit.transform.tag …

How to do chain damage in a Unity 3D game using C#

Chain damage is used in many video games as the second stage or upgrade to an existing damage from a weapon/spell. The concept is simple, one enemy is targeted and receives most of the damage and surrounding enemies receives some damage as well. This article will show you a few ways to create this in Unity 3d.
General idea: At the initial target a sphere of X radius is created and all the assets within that radius are added to a list and damaged accordingly.
The set up: A method is called when damage needs to be applied. That then creates a physics.overlapcircle with X radius at the target position. This gets an array of all the colliders in that radius and can apply damage accordingly. An example method s is below. public void ChainDamage(Vector3 center, float radius, int enemyCount, int damageAmount, int degradeAmount) { Collider[] allColliders = Physics.OverlapSphere(center, radius); // the first item in the array is your main target the next item in the array is the next c…

How different data structures can be used in game coding: List, Queue, and Stack

I'm sure most game creators are familiar with an array to store data, the problem with an array is once the size is set, it becomes much more difficult to manage. There are times when you want to sort, add, or remove data. The best way to do this is using different types of Data Structures. I'm going to focus on Lists, Queues, and Stacks though there are several others.
*This article assumes you have a basic understanding of Generic types.

Some sample code to learn and play around with. hopefully the comments are useful:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DataStructuresScript : MonoBehaviour { string[] arrayString = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" }; List<string> listString = new List<string>(); Queue<string> queueSt…

Adding randomness to your game through code using Unity 3d

There are several different ways to add randomness through code. I'm going to mostly talk about the built in random number generators and examples of where to use them. But first I will touch on why it is also important to understand the statistics of a random number generator.
*I'm going to reuse the word number when describing randomness, however what the number represents can be anything. Such as a random color(RGBA hex code) or choosing a random texture from an array.*

Randomness can be used in several ways. Two of the most common are below. 
Example 1. Where any number is used regardless of the previous.
Example: When using 1-10 at ten iterations any number is used. So there can be three 1's, four 2's, zero 3's,... This is achieved simply using Unity's Random.Range() or System.Random.Next().

The statistics of example 1:
Each number may or may not be used equally and every number within the constraints may or may not be used.
Example output at ten iterations: 1…

How to create slowly revealed text in a Unity 3d game

Image
I'm sure you have seen games that have text slowly appear on screen. This tutorial will show you how to achieve that look in your game. This will be done using the C# language and using a Coroutine.

Here is what you are going to learn to create:

Depending on how you want to display the text you can do it 2 ways.
1. The first way is to slowly display each letter of the text. This can be achieved using a Coroutine with a set time in-between each letter.
Ex:
IEnumerator TextSlower(float time) { foreach (char ch in theText) { textBox.text += ch; // wait between each letter yield return new WaitForSeconds(time); } StopCoroutine(TextSlower(0.0f)); }
2. The second way is to display each word in the text. This can be achieved using a Coroutine as described above but using String.Split to create an array of words.
Ex:
// create an array of words strArray = theText.Split(' '); IEnumerator TextSlower(float time) { for (int i = 0; i < strArray.L…