Friday, April 25, 2014

Just a quick note

 Procedural platformer levels seem to be a thing in some circles.  I came across this decent rundown for creating the AI and I'll probably be putting together some code for a simple one soon. 

http://www.gamasutra.com/view/feature/170049/how_to_make_insane_procedural_.php?print=1

A game released last year that made use of it to a challenging level is Cloudberry Kingdom.

Wednesday, April 23, 2014

Interesting kickstarter

 I browse around kickstarter occasionally looking for potential useful tools.  Recently I discovered this typefacing program in development, Prototypo:

https://www.kickstarter.com/projects/599698621/prototypo-streamlining-font-creation?ref=discovery

I think this could be a pretty amazing tool for UI design work, and I'll be keeping an eye on it.  It's definitely got 12 pounds from me.

Tuesday, April 22, 2014

Parallax Scrolling class.




 In my pursuit for getting all my errant scripts in to proper classes I went to some of the basic operations.  In this one, it was the parallax scrolling standard for 2D platformers and fighting games.  The class takes two background objects and scrolls their texture material depending on the players movement.  It requires a velocity property from the character, and the class method should be called from the player class, or some other attached script that has a regularly updated reference to the players actual velocity.

For anyone new to parallax scrolling in unity, all you do is setup two game objects such as cubes or quads away from your main scene, separate them by one on the Z axis, set their shaders to unlit transparent, and then place a camera of the same aspect ratio as your main camera, and then set the camera the same distance from the cubes as the distance from you main camera to main scene.Then, set up public game object properties, drag the game objects to the reference in the UI, then in the main script take those game objects and pass them to the ParallaxCamera's ParallaxScroll method along with the player velocity.


using UnityEngine;
using System.Collections;

public class ParallaxCamera{
    private float moveParallax = 0;
    private float speed = 0;


    void setScrollSpeed(float setSpeed)
    {
        speed = setSpeed;
    }


    void ParallaxScroll(GameObject firstBackground, GameObject secondBackground, float velocitySpeed)
    {

        if(velocitySpeed != 0)
        {
            if(Input.GetAxis("Horizontal")>0){
                firstBackground.renderer.material.mainTextureOffset = new Vector2((moveParallax * speed)%1, 0f);

                secondBackground.renderer.material.mainTextureOffset = new Vector2((moveParallax * (speed/5.0f))%1, 0f);
                moveParallax+=.0005f;

            }else if(Input.GetAxis("Horizontal")<0 p="">            {
               firstBackground.renderer.material.mainTextureOffset = new Vector2(((speed*moveParallax)%1), 0f);

                secondBackground.renderer.material.mainTextureOffset = new Vector2((moveParallax * (speed/5.0f))%1, 0f);
                moveParallax-=.0005f;
               
            }else{
                return;
            }
            }
    }
    }
}

Again if anyone needs the working script opposed to this reference, there's one in my bitbucket repo, as usual.

Monday, April 21, 2014

Raycasting in Unity and the Pursuit for more OOP


 Something that I think most developers find when they start getting in to doing complex character animations in Unity using the 2D libraries is that the built in collision leaves something to be desired.  The go to solution for getting better collision is using raycasting and essentially building your own collision detection from scratch.

Recently I've been reworking all my scripts in to solid object oriented classes.  I should have done it from the start, but with all the code examples out there being condensed into single scripts it was bothersome to do it on the fly.  Anywho, here's the class I recently made to handle raycasting for my collision detection, I thought I'd share it today. I'd also like to go over my process for improving the script and making it more useful, saving loads of effort in the long run.


using UnityEngine;
using System.Collections;



public class RaycastDetect : MonoBehaviour {
   
    public bool rayDetected = false; //set detection false by default

    bool RaycastDetection(Transform rayStartingPoint, Transform rayEndPoint, string rayLayerToDetect, bool debug) {   
   if  (debug) {
        Debug.DrawLine(rayStartingPoint.position,rayEndPoint.position, Color.green);
        }
        rayDetected = Physics2D.Linecast(rayStartingPoint.position, rayEndPoint.position, 1<< LayerMask.NameToLayer(rayLayerToDetect));

        return rayDetected;
    }
    }

So as it stands, it takes two transforms and a layer to detect and draws the line, with an option for a debug mode so you can see the lines during development.  The issues here are that for proper collision detection you have to repeat this method a lot just for one direction, and that the debug often isn't needed or desired after the first test.    We can easily overload the RaycastDetection method so that the developer can pass in arrays of transforms, and then iterate through them.  This would be much faster on the development side, and as long as arrays don't get too large the execution shouldn't slow down.  Also, we can provide an option where you don't have to put anything about the debug in.


The first overloaded class would look like this:

    bool RaycastDetection(Transform[] rayStartingPoint, Transform[] rayEndPoint, string[] rayLayerToDetect)  { 
      
        int rayArraySize1 = rayStartingPoint.GetLength;  
        int rayArraySize2 = rayEndPoint.GetLength;   
        int rayArraySize3 = rayLayerToDetect.GetLength;
     
        for(int i =0; i         {
            int j = 0;

            for(int k = 0;k < rayArraySize3;k++)
            {
            rayDetected = Physics2D.Linecast(rayStartingPoint[i].position, rayEndPoint[j].position, 1<< LayerMask.NameToLayer(rayLayerToDetect[k]));
          
            if(rayDetected)return rayDetected;
            }
            if(j < rayArraySize2)j++;
        }      
        return rayDetected;
    }


Then it would be pretty simple to swap that around to allow each option of array and two single values and two arrays and one single value, with all single values already being done.  For the debugless class overload we just delete the debug bool option and the code for that.  If we wanted to make the class useable for 3D as well, we should be able to just delete the 2D after the physics and it should work fine.

I have the full class available on my public BitBucket account:  https://bitbucket.org/milam_dobbins/milams-repo/src/

Saturday, April 19, 2014

Recently I was offered an opportunity to review some software development books, in return for getting the book and another book free.  I'm busy at the moment, but if any developers are interested and have a blog to post a review to, here is the site you can contact:

http://www.packtpub.com

Friday, April 18, 2014

Unity

I have been doing some work in Unity.  For anyone interested in getting started, here is a list of the most useful videos and tutorials I have found:

A recorded live training for 2D character controllers.  Gives a full run down of using the relatively new built in Mecanim animation tool:
https://www.youtube.com/watch?feature=player_embedded&v=Xnyb2f6Qqzg


Here's Aron Grandbergs site that includes the A* pathfinding project, which is used in just about every Unity project I've seen, as well as other useful tools and insights:
http://www.arongranberg.com/

Ray Wenderlich has some solid tutorials.  I also recommend the site for their iOS tutorials as well.  I think the tutorials are particularly good(and I believe intended) for anyone wanting to do mobile games:
 http://www.raywenderlich.com/61532/unity-2d-tutorial-getting-started

Finally, Bergzerg.  He has a great step by step walkthrough of building a complete 3D rpg similar to the latest additions to the Elder Scrolls series.  It's useful either in parts, or for building the full project if you have several days to kill:
http://www.burgzergarcade.com

I hope this helps anyone that is looking for suggestions on where to start if they want to make a transition to Unity.  I particularly value it's cross platform ability.