-
Notifications
You must be signed in to change notification settings - Fork 1
Level Two
If you are:
- a beginning Unity developer who is interested in coding, AND
- experienced in non game development-related coding tasks in Java and C#, then Level One tasks are perfect for YOU! Get ready to feel competent 😄 👍 💯
- All of the Level One Prerequisites.
- Be proficient in Git management (know these commands: push, pull, fetch, merge, status, add, commit).
- Have a good working knowledge of Object-Oriented Programming and Java/C# syntax. (This level will already be harder than the intro Java class at school, and possibly even harder than APCS. Be prepared!)
- Have Unity installed and updated to version 5.5 or above. (Excluding beta releases)
1. Implement towers, minions, and other "Tile Objects". Tile Objects are any assets that are directly visible on the Grid. Some of these objects may already have base code that you simply need to build off of, but many times you will need to code your own features and mechanics, especially for more specialized Tile Objects such as paths and spawners.
2. Get to know the Level One Developers. These are the people who can help you with the design aspect of Tile Objects, and will supply you with the necessary constants and ideas to get you started.
TASK: Implement the FireTower that was defined in the Level One Example.
STEP BY STEP INSTRUCTIONS:
- ALWAYS ALWAYS PULL FIRST! This is great practice for any collaborative coding project you may work on.
- Locate the directory that Nemesis is cloned into. Go to Assets/Stats.
- Read the file named FireTower.stats. (Created in Level One.) This file is also copied below for reference:
# Created by Ben C.
Name: Fire Tower
Description: Shoots fireballs and cool stuff.
Health: 200
Cost: 100
Speed: 10
Ability: 1 : Fireball : Shoots a fireball. : 50 : 100 : 2000
# Fireball just shoots a fireball. Deals 50 health per level increase, fires every 5 seconds
Ability : 2 : Explosion : Creates a damaging explosion around the tower. : 200 : 500 : 1250
# Explosion recharges every 10 - # of levels seconds. Deals 200 damage to all minions affected
- Open Project Nemesis using Unity. Click on the Scripts/ folder, and create a new C# Script using the right-click menu. This script should look something like this:
using System.Collections; //using is equivalent to import in Java
using System.Collections.Generic;
using UnityEngine;
public class FireTower : MonoBehaviour { //Colon reads as "of type". It is equivalent to "extends" in Java.
// Use this for initialization (DO NOT USE CONSTRUCTORS! They will NOT be called. Also NEVER use "new" to instantiate GameObjects. Unity handles this using the method GameObject.Instantiate(). Read up on this and more using the Unity Scripting Reference.
void Start () { //called once at the beginning
}
// Update is called once per frame
void Update () {
}
}
If this syntax is not at least somewhat familiar, we recommend you get more experience with plain C# first. If you have taken a Java class before, you should at least understand what is going on here. (Not your fault if you don't. Our Java class curriculum kind of sucks.)
Next, you'll want to change the parent class to GridObject, as there are useful functions and variables that you will need from there, which are:
- ownerTile, which is the GridTile the object is located in;
- lClickCanvas, which is the Canvas that opens when you left click the object;
- rClickCanvas, which is the Canvas that opens when you right click the object.
You can do this by changing the class definition from
public class FireTower : MonoBehaviour
to
public class FireTower : GridObject
More methods from GridObject to come soon. I'm still working on it.
The next thing you will want to do is create some new Canvases for lClickCanvas and rClickCanvas. You don't need code for this. Save your script and go back to Unity, where you can see the Hierarchy on the left. This displays all of the objects currently in the scene. You want to create your Canvas under Player -> UI -> The Map. Now, right click The Map, and go to UI -> Canvas. Rename your Canvas to "FireTowerL", or whatever the name of your GridObject is. Do the same for the Right Click Canvas (e.g. "FireTowerR"). Now, you are free to include whatever information you see fit into the canvas. Have fun designing your own buttons, displays, sliders, and more! Some things you will definitely want to include are the health (if Minion), name of object, and a short description. Have fun!
BEGINNER UNITY TIP: Public variables will show up automatically in the Inspector. Private, protected, and const variables will NOT unless you do a bit of magic.
Below are some very useful methods and functions you will need to use very frequently. Get to know them!
//Variables
public int num;
public float num2 = 4f; //need to add 'f' at the end of a number
protected string str1 = ""; //String is NOT capitalized, as they are of similar type to ints, floats, etc. (But don't be confused, as NONE of these are truly primitive!
//Unity Objects
Vector3 move = new Vector3(x, y, z); //Creating new 3D vector
Vector2 move = new Vector2(x, y); //Creating new 2D vector
public Canvas someCanvas; //No need to instantiate! You can assign public variables in the inspector
public Transform someObject; //Transforms control position, rotation, etc.
public GameObject someObj2; //Base class for all objects in Unity.
//Grid Objects (Custom-created for Nemesis :D)
public Grid theGrid; //probably won't need this when creating plain GridObjects, as you can reference the Grid from your GridTile
public GridTile ownerTile; //If you extend GridObject, you won't need to create a new GridTile as it will be transferred over
public GridObject someOtherObject; //Most useful for you.
//GameObject functions: You do NOT need to put GameObject in front of these methods because all Unity scripts extend GameObject by default.
Destroy(gameObject, int afterSomeSeconds); // this function is used to get rid of an object
GameObject someObj3 = Instantiate(somePrefab, Vector3 position, Quaternion.identity) as GameObject; //Quaternion.identity = default rotation
SomeCustomObject someObj4 = someObj3.GetComponent<SomeCustomObj>(); //returns a certain script attached to a GameObject
Destroy(col.gameObject, time);
//Transform functions
transform.Translate(Vector3); // or Vector2 can be passed or Translate(x, y, z);
transform.Rotate(Vector3);
transform.position.x; // can get x, y, or z
//when using position, you need to set it to a Vector object before you can change it:
Vector3 movement = transform.position;
movement.x += 3; // increments the x-value
Vector3 foo = bar + baz; //where bar and baz are Vector3. If you have taken Precalc and/or know how to add vectors, it does just that.
float magnitude = foo.magnitude; //Returns the magnitude of a given Vector2 or Vector3
void OnCollisionEnter2D(Collision2D col){ // the col object refers to the object that someone is going to collide into
Destroy(col.gameObject, time); // time refers to how long someone should wait until after
}
void OnTriggerEnter2D(Collision2D){
Destroy(col.gameObject, time);
}
// difference between OnTriggerEnter2D and OnCollisionEnter2D is that the trigger allows for the object that script is //attached to travel through object, while OnCollideEnter2D bumps into an object and is blocked until the object is gone, or //this can be used as a barrier prompting the object to do something else
// THIS IS FOR THE GAMEOBJECT THAT IT IS ATTACHED TO, NOT THE OBJECT COLLIDED INTO, FOR THAT USE col
//You can change between Trigger and regular Collider in the Inspector panel for that object.