# 2D game implementation question

I am a college student who started a game development class last week. This is my first experience with the Unity game engine.

In our first class we, (I am in a 4 person team), put together a game concept for our first assignment. We have two weeks to complete the work.

I have been working through the Unity3D tutorials and I have a few questions about our game and possible implementations. I will start with a conceptual overview of the game to give you an idea of what we are creating.

The playing surface looks like a target i.e. a bunch of concentric rings. Each ring spins at a different rate and in either direction. The rings have attached obstacles (they spin with the circle). The rings have intermittent walls that impede moving along the ring or onto an adjacent ring. Some of the obstacles would block the player and some would kill the player. The player enters from the side and attempts to cross to the middle avoiding the obstacles. Think of the game as frogger but with circles instead of horizontal lanes.

After looking at the docs for Unity3d I cannot figure out how to fit our game into the conceptualized world view/engine/class hierarchy. The biggest problems is creating and managing the rings. The only way I can figure out to make a target/ring is using a circle with the donut hole cut out by transparency or a bunch of circles all centered at the origin with the drawing layer order from largest to smallest. I don’t see how to use the callbacks for colliders, entering, touching etc with all the objects overlapping. If we used this approach we would have to calculate the players position on the canvas for each frame, calculate which ring it was on, calculate where it is on the ring using the current ring rotational position, figure out what is on the ring at that position (look up table or something?), and then figure out if it is a valid move position, blocked, or killed. Each ring would have to be drawn manually with objects in the PNG. Then we would have to come up with a map of positions on each ring that were “valid”, “blocked”, or “death”. This is not good. None of this is supported by the engine or even leverages the classes. No physics engine, Rigidbody, Collider, Joints, Force, or anything else.

I am new to Unity3D and definitely don’t have a complete understanding of how games are crafted. I feel sure this is the wrong way to implement this concept. Could anyone offer general implementation ideas on a better approach?

Jared

You’re definitely going to want to use colliders to handle all the hit-detection and responses to your player traversing the level.

Lots of work has already been done for you, so all you have to do is learn how to use it.

Here’s a rundown of hit-detection using colliders:

Colliders have a checkbox to make them Triggers. Triggers can detect collision, but allow things to pass through them. Without this checked, the collider will perform a blocking collision, not allowing other colliders to pass through.

Colliders also have a checkbox to make them Kinematic. Kinematic means that they won’t use the physics engine to move and react to stimuli.

There are 2D counterparts to colliders. (Box, Circle, Polygon) colliders use a separate physics engine that ignores the Z axis for overlap detection.

Colliders have these callbacks - Enter, Stay, Exit.

Enter - called once when something enters the collider for the first time.
Stay - called every frame while something has entered but hasn’t exited yet.
Exit - called once when the object that entered before has now exited the collider.

Depending on whether it is a Trigger or not, they have different implementations.

You can define these callback functions in a script, and if the gameObject the script is added to has a collider, they will be called automatically by Unity when something else with a collider touches it.

These callbacks also have 2D counterparts for 2D colliders.

You can find these under the “Messages” portion of the documentation here: Unity - Scripting API: Collider2D

For example, here is a class which implements both types of callbacks:
(You will typically only need one or the other, not both)

``````public class ExampleScript : MonoBehaviour {

// if this game object has a 2D collider NOT set to trigger
// these will get called and print to the console

private void OnCollisionEnter2D(Collision2D other) {
Debug.Log(other.gameObject.name + " has touched " + name);
}

private void OnCollisionStay2D(Collision2D other) {
Debug.Log(other.gameObject.name + " is still touching " + name);
}

private void OnCollisionExit2D(Collision2D other) {
Debug.Log(other.gameObject.name + " stopped touching " + name);
}

// if this game object has a 2D collider set to trigger
// these will get called and print to the console

private void OnTriggerEnter2D(Collider2D other) {
Debug.Log(other.gameObject.name + " has overlapped " + name);
}

private void OnTriggerStay2D(Collider2D other) {
Debug.Log(other.gameObject.name + " is still overlapping " + name);
}

private void OnTriggerExit2D(Collider2D other) {
Debug.Log(other.gameObject.name + " stopped overlapping " + name);
}
}
``````

Your rings will be gameObjects with SpriteRenderer components. That component will take your image. Either rings with transparency, or stacked circles as you said.

The obstacles could exist as child gameObjects, with a ring as its parent. As the ring rotates, the children will rotate with it.

If the obstacles need to rotate at a separate rate, you can make an empty gameObject as their parent, and rotate that instead.

Every gameobject can be assigned to a collision layer. You have full control over which layers can collide or not. So you can make all your rings and obstacles not collide with each other, but still collide with the player.