Tuesday, July 31, 2012

not an animator yet..

okay... here's the next exercise... i was gonna wait but this honestly took me 3 hours...

bleh.

learning animation.



Nobody believes me, but animation is my LEAST favorite thing to do in blender. I'drather rig a character honestly. but it's something I need to know how to do for my games. Here's my "course work" from cg cookie at the moment. I'll post these every few exercises I've completed and maybe comments if I have them.

Thursday, July 26, 2012

How to tell if the play has selected a 3d object

function Update () {


    if (Input.GetButtonDown ("Fire1"))
     {
     
         var hits : RaycastHit[]; // what we hit
        
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition); // creating a ray from the mouse pos
         hits = Physics.RaycastAll (ray.origin, ray.direction, 1000); // creating a ray that will check hits along ray
         Debug.DrawLine(ray.origin,ray.direction,Color.blue); // this is a debug option that will allow us to see the ray in debug mode.



// the second variable is how long it is...
 //I set it ridiculously high to make sure I hit everything
.  
        if (Physics.Raycast (ray, 1000)) {
        for (var i = 0;i < hits.Length; i++)  // for each hit in hits.
         {
        var hit : RaycastHit = hits[i]; // check this particular ray.
        var tag =  hit.collider.tag; // get the tag for the hit.
        if (tag == "Panda") { // if the hit object is "panda"
            print("got Panda"); // then we print panda.. .you can also add code inside this.
      
                                   }
          }
           
        }
     }

Create a login screen in unity

This is nice base using player prefs to create a login screen for your game... useful if you want to create a multiplayer game.

var password : String = "My Password";
var playerName:String = "player name";
function Start () {

}

function Update () {

}

function OnGUI()
{

// these strings set player name to the string you enter.
    playerName =  GUI.TextField (Rect (300, 250, 200, 20), playerName, 25);

// this is the password field that will create the password.
    password = GUI.PasswordField (Rect (300, 300, 200, 20), password, "*"[0], 25);
    

// pressing the create button creates a new user.
    if(GUI.Button(Rect(300, 350, 100,20), "Create"))
    {
    PlayerPrefs.SetString(playerName, password);
    }


// this lets the user log in. if the string == the entered password then you can load the next screen.
    if(GUI.Button(Rect(420, 350,100,20), "login"))
    {
        if (password == PlayerPrefs.GetString(playerName))
        {
            print(playerName);
            print("login successful");
        }
        else
        {
            print(playerName);
            print("login not successful");
        }
    }
}

How to save in unity.

Saving in unity is strange. the way that I have found to do it is by using player prefs.

so using player prefs when something happens that you want to save such as a player score you can use:


PlayerPrefs.SetInt("Player Score", 10);


then to access this saved information you can call

PlayerPrefs.GetInt("Player Score");


the string you use is irrelevant... you can use "myScore" or something else as long as you can remember and call the information again.

here's a test script you can play with. just attach it to the main camera or an empty object so that you can play around with it. the score should start out as "10" when you press set score it will set it to fifty. press get score and the scene will print out the new player score.


function Start () {
PlayerPrefs.SetInt("myScore", 10);
}


function Update () {


}


function OnGUI()
{
if(GUI.Button(Rect(10,10,200,200),"getScore"))
{
print (PlayerPrefs.GetInt("myScore"));
}

if(GUI.Button(Rect(300,10,200,200), "setScore"))
{
PlayerPrefs.SetInt("myScore", 50);
}
}


here are some other useful calls from the script reference


PlayerPrefs.SetFloat("Player Score", 10.0);
PlayerPrefs.GetFloat("Player Score");

PlayerPrefs.SetString("Player Name", "Foobar");
PlayerPrefs.GetString("Player Name");

Monday, July 23, 2012

My first penny.

Today I made my first penny using admob. I am so happy. Now I just have to work super hard to make better games so people will want to play them and it's feasible for me to make a living.

Friday, July 20, 2012

My pet panda

Currently I'm making a pet panda game. It's basically practice making somewhat more complex ai here are the screenshots I have now.



My resolution for the rest of the year is to produce one game a week... I'm not sure I can do it but so far I've been successful. my last game was "never ending zombies"... a simple shooter where you had never ending zombies.

How to make a health bar in Unity

Today I'm writing a short tutorial on how to create a simple health bar in unity... The code is actually pretty simple

Hopefully you already have a character script attached to a character... if not I may decide to do a simple one in a later tutorial.

in this character script we will add the OnGUI() function if we have not already done so. and a health float variable like so.

public var healthTex :Texture;
private var health:float = 1;  (note in this tutorial we will use 1 as full health 50 percent health would be .5)

OnGUI()
{
GUI.DrawTexture(Rect(Screen.width/4, 10,100*health,10), healthTex , ScaleMode.StretchToFill, true, 10.0f);
}


this will create a simple health bar that will scale based on how much health the character has.

if you want to create a back texture for an empty health bar for visual feedback you can also do this.

public var emptyHealth:Texture;

public var healthTex :Texture;
private var health:float = 1;  (note in this tutorial we will use 1 as full health 50 percent health would be .5)

OnGUI()
{
        GUI.DrawTexture(Rect(Screen.width/4, 10,100,10),  emptyHealth  , ScaleMode.StretchToFill, true, 10.0f);
GUI.DrawTexture(Rect(Screen.width/4, 10,100*health,10), healthTex , ScaleMode.StretchToFill, true, 10.0f);
}


the only difference is that we draw the empty health bar first and we do not times the width by health.

How to make a Main menu in Unity.

Today I thought I would write a short sweet tutorial on how to create a simple menu with loading features and multiple links.

The first thing you want to do is create a new scene. Then create a new javascript script(I usually call mine main menu) and attach it to the camera of the scene.

it will start out with just your basic functions. like so:

function Start () {
}


function Update () {


}

You will then want to add a new function called OnGUI()

On gui is where you draw all of your user interface. it is called repeatedly to track user button presses etc;

in OnGUI() we will put


if(GUI.Button(Rect(Screen.width * .25, Screen.height * .2, Screen.width * .5 , Screen.height *.2 ),"Play"))
 {  
 }


this should put a button that covers half the screen and has the text "Play" in it.

inside the buttons braces we will place this bit of code

Application.LoadLevel ("myLevel");

it should now read


if(GUI.Button(Rect(Screen.width * .25, Screen.height * .2, Screen.width * .5 , Screen.height *.2 ),"Play"))
 {  
Application.LoadLevel ("myLevel");
 }

if you play the game now the level will NOT load because you have to add it to the build settings for it to read. go to file>build settings and add the main menu scene as well as "my level" scene which should contain your first level or next menu.









this should work fine.. but if you want to also add a loading texture that will be drawn over the scene to tell the user to wait for the level to load you will need a loading texture like so:


we add the loading texture to the project and to the MainMenu.js we add a new public variable called loadTex;

public var loadTex: Texture;
 (This should go at the top just for form sake)

now in unity drag your loading texture to the public variable in the main menu script.

now we need to know when to draw the texture so in OnGUI() we write this line of code after our button


 if(Application.isLoadingLevel)
{
GUI.DrawTexture(Rect(Screen.width/4,Screen.height/4,Screen.width/2,Screen.height/2),loadTex, ScaleMode.StretchToFill, true, 10.0f);
}

this will draw the loading texture over your screen while the application is loading the next level... if you want to add a loading function on other scenes all you need to do is attach this same bit of code to something in each scene.

here is the completed code... have fun.

public var loadTex: Texture;


function Start () {
}


function Update () {


}


function OnGUI(){



if(GUI.Button(Rect(Screen.width * .25, Screen.height * .2, Screen.width * .5 , Screen.height *.2 ),"Play"))
     {  
    Application.LoadLevel ("myLevel");
    }

 if(Application.isLoadingLevel)
   {
GUI.DrawTexture(Rect(Screen.width/4,Screen.height/4,Screen.width/2,Screen.height/2),loadTex, ScaleMode.StretchToFill, true, 10.0f);
    }

}