Sunday, July 12, 2015


Ill be gone for a few weeks. When i come back ill try and cover Mechanim and RAIN AI, weapons script, a .pdf that involves a unification of all the scripts i have and will publish into a simple one level game. I dont think i will have time to publish another post short of this.

P.S. I've noticed an increase in traffic. Comments are open to anyone, but they are moderated. Feel free to add complaints if the posts are not clear or if you have a question on the code samples.

Sunday, July 5, 2015

Tutorial: Weapon Change

One of the most key components of modern day FPS and 3rd person shooters as well as hack and slash like Diablo has been the ability to change weapons. Today's tutorial will be about changing weapons. It will be simple, we will have slots for three weapons. Press the numbers 1-3 to change.

Before we begin we need to first set up our character. If you haven't already place the FPS controller in the scene. Add an empty GameObject and position it approximately where you would hold a weapon, parent the empty GameObject to the MainCamera. Rename this empty object to: weaponsObject. Now add an AudioSource to the weaponsObject. Check off Play on Wake.

For this script we only need two variables. An array that will hold the GameObjects that correspond to our weapons: GameObject[] weapon An AudioClip to play a sound when we switch weapons: AudioClip switchWeapon

Now in the update we will set our structure to receive user input, based on the number they choose we will call a function to change our weapon and play a sound.
if(Input.GetKeyDown(KeyCode.Alpha1))//Alpha1 means the numbers that are above the letters. No the function keys.
GetComponent<AudioSource>().PlayOneShot(switchWeapon, 0.4f);

So we are checking if they player pressed a key. Alpha keys are the numbers above you letters, using the keypad will not be recognized. Now based on weathers its 1,2,3 we pass the integer value to the changeWeapon(); function. We will play it once(PlayOneShot) and at a voulme of 0.4f.
Now copy past the above code twice change the ones to twos and threes respectively.

The changeWeapon() function operates on a loop using a makeshift break; statement. We pass the integer value based on the number the user presses, We compare it in the for loop. If the for loop variable (i) matches the passed value we set that weapon active.

public void changeWeapon(int num)
     for(int i = 1; i < weapon.Length; i++)
if(i == num)

Drive Link:

Change Weapon.cs

Thursday, July 2, 2015

Tutorial: Basic, Basic, AI

This is a very simple AI. It will just follow the player around.

This script will rely on intrinsic classes to the Unity engine, namely: TransformVector3 and CharacterControler. The float value is self explanatory, it will be the speed of the chase bot. There are two Vector3 variables: distanceFromPlayerTarget and my_Velocity. The first one my_Velocity is used by the chase bot to give it direction as it moves. distanceFromPlayerTarget is used to judge the distance from the player. Finally, the Character Controller chase_AI is what allows NPC to move, access animations and so on.

I'll explain the code in the Update() so you get an idea of how it works. We need our bot to know how far away our player is, so we take the players position by calling the .position method and we subtract from that value the position in world space with: transform.position; Now we need to normalize this Vector3. Why? Well, currently the Vector is giving us the length of the distance, we want direction. So we call the normalize to change the value to Vector3(0,01) rather than what ever its current value was.
Next we have to give velocity to out bot, we do this by taking the direction of the player we just normalized and multiply by the chase speed. Now with this we can make the bot move. So we call the character controller and call the .Move(), we have to pass the my_Velocity and multiply it by Time.deltaTime.

Make a new scene. Add in the first person controller(its a capsule)that unity provides. Add three or so cylinders. Attach your Chase script to the cylinders, also add character controllers to each cylinder.
Next drag the First Person Controller from the hierarchy to the Target_Player field, next drag the Cylinder from the hierarchy to the Chase_AI field. Now change the Chase Field value from zero to what ever you want. Start small though, these bots can be very fast.

You can play around with this. Add in conditionals to the distance from player and have the bots chase if the player is a particular distance from the chase bot. You can also add collider boxes around the scene have them correspond to particular chase bots, if the player passes through them have the bots chase the player.

Drive link: