Forcing OpenGL on Unity5.x on windows Builds

November 3 | 15

So in the lab we needed to run our builds in windows in OpenGL so based on the docs you just add a “-force-opengl” flag when starting the app and you should be good to go.

But we where getting a pink screen all over the place (symptoms of unrecognized shaders). After fighting with it for a while I asked Aras and turns out that since Unity 5.1 OpenGL shaders are not included into windows builds by default.

So to make this work, just go to Editor->Project Settings -> Player and uncheck “Automatic graphics API” and add GL in the player settings and you should be good to go.



Making particles follow a path

September 7 | 15

So I was tasked to make a particle system follow a set of points (P1, P2, P3, … Pn) for a visualization in the CAVE. Basically given a particle system, make the particles in it follow a set of points:












If we linearly interpolate their positions through a group of points we will get a set of particles that will follow a set of points, something like this:












Instead what we want is to make each spawned particle follow a set of directions, something like this:












So in order to achieve this, we create an array of directions (given the points), then we check the total distance traveled for each particle and then depending on where the particle is located (across the points) we change the direction accordingly towards the next point (when it reaches each point)

The end result:

The Code:

The code is pretty straight forward; one editor script and one simple script that needs to be attached to a game object that contains the particle system.

Editor Script: (remember to place it under “Editor” folder).
  This class draws the handles for the diferent nodes the particles are going to follow.
  created by:
  Juan Sebastian Munoz Arango
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class FollowerCustomEditor : Editor {

    private Follower myTarget = null;
    private const int offset = 50;//offset when creating a new node.

    void OnEnable() {
        myTarget = (Follower) target;
        if(myTarget.nodes == null) {
            myTarget.nodes = new List<Vector3>();
            myTarget.nodes.Add(myTarget.transform.position + Random.insideUnitSphere*offset);

    void OnSceneGUI() {
        if(myTarget != null) {
            for(int i = 0; i < myTarget.nodes.Count; i++) {
                myTarget.nodes[i] = Handles.PositionHandle(myTarget.nodes[i],

    public override void OnInspectorGUI() {
        for(int i = 0; i < myTarget.nodes.Count; i++) {
            myTarget.nodes[i] = EditorGUILayout.Vector3Field("", myTarget.nodes[i]);
            if(GUILayout.Button("X")) {
                if(myTarget.nodes.Count > 1)
        if(GUILayout.Button("Add")) {
            Vector3 newPos = myTarget.nodes[myTarget.nodes.Count-1] +
 The script:
  Created by: Juan Sebastian Munoz arango
  Simple particle system that follows certain o
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Follower : MonoBehaviour {

    public List<Vector3> nodes;
    private Vector3[] directions;

    void Start() {
        GetComponent<ParticleSystem>().startLifetime = nodes.Count;
        if(nodes.Count == 0)
            Debug.LogError("Nodes needs to have at least 1 item");
        directions = new Vector3[nodes.Count];
        for(int i = 0; i < nodes.Count; i++) {
            directions[i] = (nodes[i] - ((i-1 >= 0) ? nodes[i-1] : transform.position));

    void Update() {
        ParticleSystem p = (ParticleSystem) GetComponent<ParticleSystem>();
        ParticleSystem.Particle[] particleList = new ParticleSystem.Particle[p.particleCount];
        int partCount = GetComponent<ParticleSystem>().GetParticles(particleList);

        for(int i = 0; i < partCount; i++) {
            float timeAlive = particleList[i].startLifetime - particleList[i].lifetime;
            float dist = GetAddedMagnitude((int)(timeAlive));
            int count = 0;

            while(dist > GetAddedMagnitude(count))

            particleList[i].velocity = directions[count];
        p.SetParticles(particleList, partCount);

    private float GetAddedMagnitude(int count) {
        float addedMagnitude = 0;
        for(int i = 0; i < count; i++) {
            addedMagnitude += directions[i].magnitude;
        return addedMagnitude;

    private void OnDrawGizmosSelected() {
        Gizmos.color =;
        Gizmos.DrawLine(transform.position, nodes[0]);
        for(int i = 1; i < nodes.Count; i++) {
            Gizmos.DrawLine(nodes[i], nodes[i-1]);

Still there is some stuff missing like adding speed to the particles or making the generated pivots move relative to the game object but it pretty much does the job.



How to know if a point is inside a polygon

April 16 | 15

Here’s a quick code snippet to know if given a point, the point is inside an array of polygon vertices. Based on the crossing number algorithm.


// In Unity Vector2 is implemented with a pair of _floats_ x and y!
// if you are not working with unity make sure to implement Vector2 with _double_ to avoid
// precision errors.
private bool PointInsidePolygon(Vector2 point, Vector2[] polygonVertices) {
    if(polygonVertices.Length < 3) //not a valid polygon
        return false;
    int nCounter= 0;
    int nPoints = polygonVertices.Length;
    Vector2 p1, p2;
    p1 = polygonVertices[0];
    for(int i = 1; i < nPoints; i++) {
        p2 = polygonVertices[i%nPoints];
        if(point.y > Mathf.Min(p1.y, p2.y))	{
            if(point.y <= Mathf.Max(p1.y, p2.y)) {
                if(point.x <= Mathf.Max(p1.x, p2.x)) {
                    if(p1.y != p2.y) {
                        double xInters = (point.y - p1.y) * (p2.x - p1.x) /
                            (p2.y - p1.y) + p1.x;
                        if((p1.x == p2.x) || (point.x <= xInters)) {
                            nCounter ++;
        p1 = p2;
    if((nCounter%2) == 0)
        return false;
        return true;

This can also be extended to 3D space if necessary, but that’s for the reader.

Open Match-3. Developed in Unity

April 14 | 15

So I had this project for long time stored and didn’t know what to do with it so what a better idea than releasing it!.

Hopefully the community will learn from it, and if you find any better way of doing certain things I’ll be more than happy to receive some feedback!

This project is released under the MIT license, you can do whatever you want with it :).

As the title says is a match 3 that works in Unity. 4.x and 5.x. it contains:

  • Generates a move if the board doesn’t have any more moves left.
  • Supports any amount of gems (for the time being I just added 5 types of gems).
  • Works with any board size (width x height)
  • Generates an “explosion” effect when 3 or more adjacent gems are clicked.
  • Generates a hint (depending on a time in secs set in the inspector) of the largest adjacent gems.
  • Support for score multiplier when you click on several correctly adjacent gems before the hint kicks in.

Here are some screenshots of the game:

Screen Shot 2015-04-14 at 15.03.57















Also supporting any width x height (17×18) in this case.

Screen Shot 2015-04-14 at 15.05.21












Finally in order to modify certain parameters of the game, just click over the main camera object and set the desired values inside GameManager component.

Screen Shot 2015-04-14 at 15.07.54












Liked the project?, Here!, fork it!


Like this post?, feel free to subscribe to my posts, I’ll keep you posted on anything that I write :).


Any comments/suggestions/feedback let me know :).


Adding Transform.LookAt2D like it belongs to the Unity API

March 31 | 15

Unfortunately there is no Transform.LookAt2D in Unity as for now in Unity5. BUT with an extension method this can be added into the API quick and easy!.

Its pretty simple. We just need to have a namespace included wherever we want to use Transform.LookAt2D and thats pretty much!, here’s how:

Lets create the extension method for LookAt2D:

using UnityEngine;

namespace UnityTransformExtensions {
    public static class TransformExtension {
        //first arg is the class we want to add the extension method to.
        public static void LookAt2D(this Transform tr, Transform target) {
            Vector3 relative = tr.InverseTransformPoint(target.position);
            float angle = Mathf.Atan2(relative.x, relative.y) * Mathf.Rad2Deg;
            tr.Rotate(0,0, -angle, Space.Self);
        public static void LookAt2D(this Transform tr, Vector3 pos) {
            Vector3 relative = tr.InverseTransformPoint(pos);
            float angle = Mathf.Atan2(relative.x, relative.y) * Mathf.Rad2Deg;
            tr.Rotate(0,0, -angle, Space.Self);

And then we just need to call it from any of our scripts as part of the Transform methods! here’s a quick example:

using UnityEngine;
using System.Collections;
using UnityTransformExtensions;//Remember to include the extension namespace

public class LookAt2DTest : MonoBehaviour {
    void Update () {
        Vector3 lookPoint = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
        transform.LookAt2D(lookPoint);//called like its part of the API!

Quick, easy and clean!