Creating a Da○saba○bar (First Issue)

"Weekly Making a D*saver*ber," inaugural issue!
I started abruptly in a DeAgostini style.
It's been a while.
This is Matsuyama from the Systems Development Department.

When I'm browsing Facebook or manga apps on my smartphone,
I often see ads for "Da○sabur○ber."
I don't dislike those kinds of chaotic games
, so I decided to try making one myself to see what it's like.
By the way, I've only played the demo version, so
please forgive me if there are significant differences from the original game.

Rough requirements

As usual, we'll start by listing the requirements:
① There is a 2D field (a grid-based type)
② The player character can be controlled by touching the screen up, down, left, and right
③ There are multiple weapons and levels
④ Enemies appear spontaneously and move towards the player
⑤ There are collision detections for the player, enemies, and weapons
⑥ When you defeat an enemy, experience points are displayed, and by collecting them, you can obtain new weapons or level up existing ones

That's about it for now.
We'll figure out the finer details as we go along.
Time measurement, sound effects, and other detailed production elements will be future tasks (while subtly hinting that we might not even do them).

Project Creation

I haven't used Unity in a long time, so let's install the latest version. It
seems that 2022.3.18f1 is the latest LTS version, so I'll install that.
Unity Hub is really convenient.
I'll name the project "BeBeSurvivor" and create it as a 2D application.

resource

I'll find the necessary resources (sprites) for creating it on the Asset Store.
While browsing for free 2D assets, I found exactly what I was looking for.

◾️Undead Survivor Asset Pack:
This pack includes all the necessary assets such as players, enemies, and weapons.
It even works, so you could almost finish the project with
just this, but I'll gratefully use the sprites.

◾️Lowpoly Textures Pack:
This is an image pack for covering fields.
It's also included in the Undead Survivor Asset Pack, but let's try using this one too.

Import these two using Package Manager

Paving the field

The Lowpoly Texture Pack has a ton of assets, so I'll just pick out a few that seem like they'll be useful.
They were a bit too big, so I'll scale them down.
Like this.

We'll convert this into a prefab and then use a script to place it in a suitable area.
Something like this.

And the script looks like this:

// Generate a field ranging from (-50, -50, 0.5) to (50, 50, 0.5) (5 x 5 grid) Vector2 pos = new Vector2(-50, 50); float size = 5f; int cnt = 20; for(int x = 0; x <= cnt; x++) { for(int y = 0; y <= cnt; y++) { // If x and y are 0 and cnt, generate field[1], otherwise generate field[0] int idx = (x == 0 || x == cnt || y == 0 || y == cnt) ? 1 : 0; // Generate field[2] for the center grid if(x == cnt / 2 && y == cnt / 2) { idx = 2; } // Generate GameObject obj = Instantiate(field[idx], new Vector3(pos.x + (x * size), pos.y - (y * size), 0.5f), Quaternion.identity); obj.transform.parent = parent.transform; } }

I'll try placing different images only on the outer edge and in the center.
For now, I'm making do with a finite field, but I think infinite scrolling would be better in reality.
(Though, even I have to admit, this is pretty haphazard code...)

Player

The survivor asset pack contains some good materials, so I will use them

Since there are animation patterns available, create animations for idle, movement, and death, and
set them up in the Animator.

This is what it looks like when moved (standby motion)

Movement (Input System)

This moves the player character.
The image is that the character moves in the direction you tap, with the center of the screen as the origin.
while I've been away from Unity Input System has been released
I'll import that from the Package Manager as well.

I tried it out briefly, but I couldn't get the information to work properly from the Input Action.
For now, I'll skip the Action and proceed with the implementation using traditional script control.
(I'll try tinkering with the Input Action again when I have more time.)

var mouse = Mouse.current; if(mouse.press.ReadValue() == 1) // press state { if(holdTime > 0.0f) // wait { holdTime -= Time.deltaTime; if(holdTime < 0.0f) holdTime = 0.0f; } else // move { Vector2 pos = mouse.position.ReadValue(); } } else // release state { holdTime = HoldTime; }

Operation information can be obtained using Mouse.current.
The tap state can be obtained with mouse.press.ReadValue(), which returns 1 if touching and 0 if released.
After a certain period of time has passed since the touch, the touch coordinates are obtained with mouse.position.ReadValue() and
the direction of movement (vector) is calculated. It looks something like this.

Since the coordinates of the two points are known, the vector can be calculated simply by subtraction.
Length is irrelevant, so we'll normalize it using `normalized`.

Vector2 vec = (pos - center).normalized;

If you set this to the player's coordinates (localPosition), the character will move.
Like this. (Standby ↔ Move)

While you're there, extract only the X component of the direction of movement and change the character's orientation (rotate 180° on the y axis)

Camera Tracking

In this state, the character will move off-screen, so we need to make the camera follow the character so that the character is always in the center of the screen. All you need to
do is set the localPosition of the MainCamera's Transform to match the character's coordinates.

The code looks like this. Note that the z coordinate is fixed to the camera

// Follow the camera Vector3 pos = playerController.GetPosition(); pos.z = -1.0f; mainCamera.gameObject.transform.localPosition = pos;

After implementation it looks like this:

enemy

For now, scarecrows will do, so I'll just place some enemies randomly.
The Survivor Asset Pack has some good assets for this, so I'll use those. (Thanks!)

Since there are animation patterns available, create animations for movement and death and
set them up in the Animator.

For now, I'll leave it like this

Collision Detection

We will now perform collision detection.
We will use the Collider2D provided by Unity as standard.

First, let's prepare.
Add a Circle Collider 2D to both the player and the enemy.
Make sure IsTrigger is checked.

Also, add a Rigidbody 2D to the player. (Not needed for the enemy)
Since physics calculations are not needed this time, set the Body Type to Kinematic.

We will obtain collision detection using a script.
Add the following functions to the player and enemy code. (Trigger related)

private void OnTriggerEnter2D(Collider2D collision) { // Called the moment the collider hits // Damage occurs if it hits an Enemy if (collision.tag == "Enemy") { Debug.Log("Player Hit : " + collision.name); } } private void OnTriggerStay2D(Collider2D collision) { // Called continuously while the collider is in contact } private void OnTriggerExit2D(Collider2D collision) { // Called when the collider leaves }

We'll determine what the hit target is using its tag.
The Player tag is already registered by default, so we'll add the Enemy and Weapon tags.
We'll set the Tag for each object.

If you run it so that it outputs a log when it comes into contact, it will look like this

That's all for this month

That's all for today.
I think we've pretty much implemented the basic mechanism.
Unity has all the necessary features, so it's relatively easy to implement.

In the next issue
weapons (3 types x 3 levels)
I hope to implement

I plan to upload the Unity project to GitHub later.
Please be patient.
I will update the article once it's uploaded.

I uploaded the Unity project to GitHub
I hope this is helpful in some way
BeBe Survivor

That concludes today's story.
*Although I wrote "weekly" at the beginning, the next issue is scheduled to be uploaded in March.

If you found this article helpful,please give it a "Like"!
14
Loading...
14 votes, average: 1.00 / 114
717
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Matsuyama Kensho

I worked for a long time at a game development company, handling tasks such as programming and project management.
I joined Beyond Inc. in 2019 and work at the Yokohama office.
I mainly handle project management for server-side development (and occasionally do programming).
My hobbies are cycling (road racing) and watching horse racing.