Monday, September 16, 2013

UNITY: Digital Tutors: Introduction to Unity 4



CH11: Working with Physics

Mesh Collider:

  • Mass: no more than 100x other 
  • Is Kinematic: Not affected by forces or gravity, until acted upon by another rigidbody (best for doors, etc).
  • Interpolate: (change if there's glitches and twitches)
    • > None
    • > Interpolate
    • > Extrapolate 
  • Collision Detection:
    •  collision checks are done on certain frames, if object moves so fast that it misses collision check, it could pass through objects.
      • > Discreet (sparse checking)
      • > Continuous (medium checking)
      • > Continuous Dynamics (most/fast checking)
  • Constraints: Can be used to restrict rigidbody movement in specific axis
    • Freeze Position XYZ
    • Freeze Rotation XYZ
  • Rigidbodies should not intersect

CH 12: Getting Started with Scripting

A game is essentially a set of rules and systems set in place for the player to follow, which creates gameplay.
  • Before scripting, create SCRIPTS folder.
  • Three different available scripting languages:
    • JavaScript
    • C#
    • Boo
  • Use whichever language you're most comfortable with (this tutorial series uses C#)
  • Name the file using some kind of naming convention for your own sanity.
  • Unity uses MonoDevelop, but you can use Visual Studio etc. as well.

C#:

  • "using" statements allow for use of pre-made code already present in the UNITY game engine.
  • CLASS is a script in C#
    • public class HelloWorld : MonoBehavior
      • in this instance, HelloWorld is the class name
      • in this instance, MonoBehavior is the type
        • MonoBehavior inside of UNITY is a script object
  • two default functions:
    • void Start()
      • runs once for initialization
    • void Update()
      • runs once per frame
  • Debug.Log(); // basically a 'print to console' command
    • every "statement" should end with semi-colons.
  • Create a GameObject called "MessageMachine" and drop message related scripts onto this object. (or drop on to MainCamera)
    • All scripts need to be attached to an object in the hierarchy.
    • Once attached, it becomes a "component" of the GameObject
  • void Start();
    • good for initialization
  • void Update();
    • good for updating stats
      • game state win/lose conditions
      • player health, ammo, etc
      • boss health

CH 13: Creating the Player Spawn Controller

Goal: to have the player randomly spawn at one of the many designated spawn points.

Steps:

  • Create new Empty GameObject
  • Name it "PlayerSpawn_01"
  • Position "PlayerSpawn_01" into the scene
  • Duplicate and rename to "PlayerSpawn_02"
  • Position "PlayerSpawn_02" to your liking
  • Duplicate and rename to "PlayerSpawn_03" and position
  • Duplicate and rename to "PlayerSpawn_04" and position
  • Group all objects into a "TAG"
    • add TAG
    • name it "PlayerSpawn"
    • this allows us access to these objects via code
  • Make sure "First Person Controller" object is tagged as "Player"
  • Create C# Script "PlayerSpawnController"
    • Talk out what you want do first, which will give you a hint as to what you're going to need to script.
    • Will be needing arrays (lists)
    • Align the curly brackets first { }
    • Use void Awake()
      • gather all of player spawn objects:
Example:
void Awake()
{
    playerSpawnArray = GameObject.FindGameObjectsWithTag("PlayerSpawn");
}

Must declare a public variable before it can be used.


EX:
public GameObject[] playerSpawnArray;

CH14: Finishing the Player Spawn Controller

An array is basically a list to hold multiple pieces of data for a single variable.  What the above script is doing is looking for any GameObject in the scene with the tag "PlayerSpawn" until there are no more.

To get random player spawn, we need to create a new function to handle that.  We only want it to run whenever it's called, which is also another reason to embed into a function.

EX:
public GameObject GetRandomPlayerSpawn(int randNum)
{
    randNum = Random.Range( 0 ,  (playerSpawnArray.Length) );    
// set range to max playerSpawns found.

    if (playerSpawnArray.Length > 0 )
    {
        return playerSpawnArray[randNum];
    }
    // need to put something as a failsafe, in case the IF statement isn't met.
    else
    {
        return null;
    }
}

Need to initialize the "randNum" variable at the beginning, so under the main class, declare a private variable:

EX:
private int randNum;


Create an empty game object and call it "Controllers", then create another empty game object and call it "PlayerSpawnController".  Freeze both transforms, then parent "PlayerSpawnController" to "Controllers".

Add appropriate script to "PlayerSpawnController" object.

Exposing variables in Unity editor is crucial for gameplay creation.  Once you hit play, this script will populate the exposed variable with items that it has found.

CH15: Creating the GameState Script

In this tutorial, we're going to create the GameState script.  This script handles the core rules of the game.  All other scripts connect to this script, and communicates through.  Handles Player spawn, enemy spawn, placing objects, etc.

Create another C# script, and name it "GameState".  What do we want the GameState script to do?

  • find the player
  • find the playerSpawnController script
  • take the player and move the player object to one of the random spawn points
Note: Scripting is an iterative process.  Don't expect everything to work the first try.  It's a process of bug hunting + refinement.  

First step: create variables that we'll need.

Example script:

public class GameState : MonoBehavior
{
private GameObject player;
private PlayerSpawnController playerSpawnCTRL;  
// creates an object of this script class so we can access it through this script.
private GameObject randPlayerSpawn;
// create a variable to hold data created by the random player spawn script.

void Awake ()
{
     player = GameObject.FindWithTag ("Player");
     playerSpawnCTRL = GameObject.FindGameObjectWithTag("PlayerSpawnControl").GetComponent ();
     // make sure PlayerSpawnController is properly tagged
}

void Start ()
{  // we need to initialize our randNum value in this script
     int randNum = 0;
     randPlayerSpawn = playerSpawnCTRL.GetRandomPlayerSpawn(randNum);

    // actually spawn the player
     SpawnPlayer();
}

void SpawnPlayer()
{
     // take player object and move to random player spawn transform.position.
     player.transform.position = randPlayerSpawn.transform.position;
     Debug.Log("You have spawned at " + randPlayerSpawn.name );
}

}

Variable declaration tip:  " scope type name = initialValue "

Add another empty game object into the scene and rename it "GameState", then drop this script into it.



CH 16: Scripting the HUD

This chapter teaches you how to create a HUD to display feedback to the player.  I'll be doing this script in Unity, so only important notes will be put here.  Refer to project script ( available at request ) for details.

- you have to let Unity know what type of textures these are, so under "Texture Type" in the inspector, switch them over to "GUI".
- add "GUITexture" component to "HUD" object.
- you can manipulate the position of the "lantern" texture by changing the transform of the HUD object.
- Set SCALE to 0,0 and use PIXEL INSET (in pixels)
- Set Width and Height to half of what the icon size should be.

Creating a feedback C# script: " Inventory ", but it could be named anything (weapons, ammo, lantern fuel, etc)

- STATIC what is it?  Simply means "global"
- create "Texture2D" array variable to hold the textures to be used by lantern.
- Declaring Arrays requires the "[ ]" after the type of variable to indicate that it's that type, but an array.  Example:

public Texture2D[] hudFuelAmount;

- we only need updates when items are picked up, so don't use "Update()".
- create a "FuelPickup" function instead.

- Apply script to PlayerObject
- assign texture2D array elements in Unity UI (can probably use this technique for texture swapping too, for different zombie skins)

- clicking on the GEAR icon will bring up available options, rather than dragging and dropping

CH17: Creating Items in Unity

Create an item pickup that'll update our HUD.  Again, like the last chapter, this will only be notes and not a summary.  Refer to the scripts and the training videos for the actual lesson.

- import LanternFuel object
- create LanternFuel prefab
- create C# script "LanternFuel"

- use "OnTriggerEnter" event

"player.gameObject.SendMessage("FuelPickUp"); " is a form of running the function "FuelPickup". Great way of communication between scripts.  NAME HAS TO MATCH.

- " Destroy(this.gameObject); " refers to the object the script is currently on, and destroys the gameObject.

 - make sure to add a BoxCollider to your prefab, and select "Is Trigger".

Adding sound to Inventory.cs:
Add a public AudioClip variable "fuelCollectedSound"

then in your function add:
AudioSource.PlayClipAtPoint(fuelCollectedSound, transform.position);


CH18: Creating Lantern Functionality

CH19: Loading Levels

Application.LoadLevel("MainMenu"); //use this to go back to main menu
http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html

CH20: Publishing Our Game






No comments: