Coding Discussions with Mr. Risker: collisions (AS 2.0 vs C#)

As mentioned before, I am a total n00b when writing code. My experience comes from visual scripting, using programs like UDK, as well as editor extensions like Playmaker and Ork Framework. However, learning the terms on the extensions, as well as the experience I had writing in Actionscript 2.0 (AS2.0 henceforth) allows me to at least understand coding enough to manipulate things to my liking. I am using this information to learn to code and using this series as a way to teach others, as well as myself.

Behind the scenes, I do have a programmer read this, because i am a n00b that makes typos at times, and If there is a mistake afterwards I will fix it. So leave information in the comments to let me know how I am doing.

My Game: Biped’s Journey was created purely from if then statements. it works, but it isn’t too clean. Unfortunately, the school that I went to focused less on how to make the game worked, and focused more on how to make the game look and feel good.

I learned more, but i didn’t add it since ActionScript was slowly Dying.

The example that I will use is based on my collision detection code that was built in AS2.0 and using the same example modified for C#

ActionScript 2.0

Two things that you will notice when reading the AS coding.

ActionScript is Proprietary, so you will not notice any “using” statements because Adobe Flash (RIP) would have known the coding words that would be used.

ActionScript 2.0 was the last version that allowed coding on the items themselves. AS 3.0 was more conventional and did not allow coding on objects. (FUN FACT: i thought that coding on the object itself was “object-based coding.” let’s just say that It was one of the things that prevented me from getting a specific job.)

I will name the characters “player” and “enemy” this would need to be the clip names, since the code is going on the clip itself.

of course, you will need to give the player parameters, or the collision will not do anything. so i gave the player a heath value which was on the top of the code.

 

health = 100

 

in AS 2.0, that is all that was needed, because the code was on the player (this will also work on the enemy).

then i gave it an if / then statement

 

If (player(enemy)){

x -= 5;
health -= 10;

}

in other works if the player clip collides with the enemy clip, the player’s X co-ordinate will move backward 5 pixels and the game will take a value of 10 away from the stored health value. I added the X value in order to push the character back and remove the enemy’s ability to constantly collide and cause a quick game over.

on my game each I added a value to the if/then statement because i didn’t know about functions at the time. functions in AS 2.0 are similar to the void command in other languages. so instead of the above code i could have done this:

 

function smallDamage (){

x -= 5;
health -= 10;

}

and in full

 

health = 100

function smallDamage (){

x -= 5;
health -= 10;

}

If (player(enemy)){

smallDamage;

}

now AS knows that the top area is a variable, and that the function is public unless stated otherwise. a cleaner version would look like this

var health = 100

public function smallDamage

{

x -= 5;
health -= 10;

}

If (player(enemy))

{

smallDamage;

}

This worked, but not to my expectation. because the characters were created using 3DS Max the animation clips were PNG files and the clip used the picture edge as the collision, this caused the collisions to happen extremely early and hard to guess because the barrier is transparent.

A while back, I mentioned a good article describing hitboxes and hurtboxes in fighting games. and I used it in this game to fix the PNG issue. in each animation I created a red box named “hitbox” and a green one named “hurtbox in the respective movie clip. this changed the coding in a small way.

If (player.hitbox (enemy.hurtbox))

{

smallDamage;

}

In other works, if the player’s hitbox collides with the enemy’s hurtbox, cause a small amount of damage, or knock the character back 5 pixels and subtract 10 health.

Of course there is much more that can be done, but this is the basics of how the battle system worked in AS 2.0

now that you were hit with that much information, you may need a break. here’s the picture of the puppy again:

C# time!

for this we will be using unity3D, my current engine of choice. Also we will be making multiple pieces of code, or scripts. finally. we wll be sticking to 2D

I mentioned multiple scripts, because of how the scripts will be added. You add the scripts as a “component” in Unity. in this case, the Health-related information will go onto the player model and the Damage related information will go onto the weapon model. Keep in mind, that the models are called “gameObjects” in the system’s code.

with C #, and other conventional languages you will need to let VisualStudio (or Monodevelop, Notepad++, etc.) know that you are using Unity’s library:

using UnityEngine;
using System.Collections;

after that you would need to create a script for the Player’s health

public class Health : MonoBehavior {

public const int maxHealth = 100;
public int currentHealth = maxHealth;

}

Per the official Unity 3D documentation: “MonoBehaviour is the base class from which every Unity script derives. When you use C#, you must explicitly derive from MonoBehaviour.”

also according to the code, your maximum amount of health will be 100 percent, and when starting the game, your current health will be equal to your maximum health

when you add health to a game you may have to create an if/then statement that will monitor push the variable back down to 100 if it goes over, but that can be something for another blog.

what is good about C# is that you can reference and borrow code from other scripts. in this case, we will cause damage on one script and take the damage from the other script. back to the “Health” code

public void TakeDamage (int amount)
{
currentHealth -= amount;

Debug.Log (“Player is hit”);

}

after any void name you have parenthesis symbols. within the parenthesis you can add values, which can be replaced later. TakeDamage has the value of (int amount) which will be placed by whatever integer that is called upon when the code is activated. if nothing is there, it isn’t used.

Debug.Log is a command that only affects the “console” in the editor. this will help you with knowing if the code is working. I normally add a string of what should be happening. with SALSA, i created a log thats says, “the character should be smiling” when the “Happy” code is implemented. much more can be done, but as I mentioned before, I’m still a n00b.

for the Damage Script:

public class Damage : MonoBehaviour

{
void OnCollisionEnter2D(Collision2D collision)
{

var hit = collision.gameObject;
var health = hit.getComponent<Health>();

if (health != null && collision.gameObject.tag == “Enemy”)
{
health.TakeDamage (10);
}

}

 

I used pieces from the 2D collision tutorial, as well as a PlayerHealth and Bullet tutorial on the Unity Documentation in order to create this code. The 2D tutorial didn’t show a way to reference the player health. I am not at the Gamedev computer right now so i will test it later and check the results. however. this is what should happen if i am reading this correctly

in Unity, 2D characters use a component called Collision2D in order to read the collision properly.

OnCollisionEnter2D looks activates when you enter a Collision2D component, which you gave the name collision

to shorten the coding you add the variable of hit. hit will refer to collision.gameObject from now on. you also change add the variable health (case matters here) to refer to hit.getComponent<Health>();

without naming collision2D and adding variables beforehand, “health” would equal  Collision2D.gameObject.getComponent<Health>(); instead.

also getComponent is looking for the component “Health” on the gameObject that it collided with. when it finds health it activates TakeDamage. TakeDamage looks for the current health and subtracts buy the integer provided. which, in this case, is 10.

public void TakeDamage (10)
{
currentHealth -= 10;

Debug.Log (“Player is hit”);

}

 

As mentioned before, I am not an expert and I can be wrong. However I believe that in teaching others, I also teach myself. Hopefully you learn something, and if you know more than I do, leave examples in the comments. i would be happy to discuss more on this.