Blog

All articles

How to develop a minigame with the help of AI

It’s not secret that the field of IT is in a constant development, new technologies and tools are always appearing. The modern world is experiencing explosive growth in the field of artificial intelligence (AI), spanning many industries. The world of technology and gaming is changing faster than ever before and the integration of AI is becoming the norm. Therefore, specialists and companies in general need to be constantly on the lookout not to miss new approaches, mechanisms and opportunities.

That is why, our colleague Oleksandr tried to conduct an experiment – creating a scene of a mini game based on Unity, using Chat GPT. The purpose of this experiment was to gain experience and understand how Chat can help speed up the development of similar projects. Today he will analyze this case with you.


Let’s get started. I chose Unity with development environment 2022.3.12f1 as the graphics engine. Why Unity? Well, firstly I heard that it is a very popular engine with a developed IDE and good support. Secondly, it immediately allows you to deploy the game on more than one platform – for example, for iOS and Android. In comparison, using Apple’s native SpriteKit, it is possible to create a game only for the iOS platform. The free version of GPT Chat 3.5 was chosen as the AI. I want to clarify right away that until this moment I had no experience working with either the first or the second. I have previusly worked only on iOS development, but that’s why I found this experiment interesting.

I made a start, by creating the following two-point plan:

  • Find and complete a tutorial on creating a Unity game and get a basic understanding of the domain.
  • Try to create a similar/different game, but using Chat

Regarding the first point, I haven’t been able to find a good tutorial for making a 2D game. A manual for 3D caught my eye. After the first few hours of experimentation, I realized that continuing with this guide would be a waste of time. First, there was a significant difference between the versions of my IDE and the one described in the tutorial, many functions/controls and their locations did not match each other. Secondly, it became clear that the manual for creating a 3D game has a lot of extraneous things that I will not need in the experiment. Therefore, after getting the first idea about Unity, I decided to immediately go to the second point and start creating my game, but already with the help of Chat.

The idea for the game was very simple, to create a scene where the hero flies up and down from the left edge of the screen. With a long press, it goes up, and when you remove your finger, it goes down. The hero should also shoot automatically. From the right side (from random points along the Y axis), monsters should fly out and move towards the hero. When a bullet hits a monster, the monster must disappear. Also, the background for the scene had to be scrollable and infinite.

To my first question – “How to create such a minigame?” (Description gave him above) Chat responded by listing the steps with a brief description of each – “Defining game mechanics, choosing language and development environment, creating graphics, developing game engine, user interface, testing, publishing, marketing and promotion, support and updates”

This happened several times in different variations – I realized that I won’t get much from him with general requests. Remembering the material from the tutorial about the 3D game, I tried to break one large task into small ones:

  • Create a “hero” object with the necessary motion physics
  • Adding the “hero” ability to shoot “bullets” 
  • Create a “monster” object with the necessary motion physics 
  • Make the monster disappear after hitting the bullet
  • Create a background with an infinity scroll
  • Add image for all objects

After that, my requests took on a different form for Chat and it began to issue a specific code and actions with it.

This is how the work progressed step by step.

Stage 1 – Creating a “hero”

I started by creating an empty model/object and added it to my scene. After that, it was necessary to describe the Hero’s behavior in a special file with a script (C#). I asked Chat to make such a script:

Request – “Write a script to make the object go up while you hold down the tap”

The answer:

'using UnityEngine;

public class LiftObject : MonoBehaviour

{

    private bool isTapping = false;

    private Vector3 initialPosition;

    public float liftSpeed = 1.0f; // The speed of the moving object

    void Update()

    {

        if (Input.touchCount > 0)

        {

            Touch touch = Input.GetTouch(0); // Assume that the first touch is used

            if (touch.phase == TouchPhase.Began)

            {

                isTapping = true;

                initialPosition = transform.position;

            }

            else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)

            {

                isTapping = false;

            }

        }

        else

        {

            isTapping = false;

        }

        if (isTapping)

        {

            // Raise the object up

            Vector3 newPosition = transform.position + Vector3.up * liftSpeed * Time.deltaTime;

            transform.position = newPosition;

        }

    }

}

In those scripts we verify that the correspondent presses the tap to the screen. This way, the object will go up at the given liftSpeed. When the user lets go of the screen, it starts to fall.

To revise this script, simply attach it to a Unity object. You can also adjust the liftSpeed to regulate how quickly the object moves.”

Previously, I had never worked with scripts for objects – I asked: “How can I attach a script to an object/sprite in a unit?”

Answer – “To attach a script to a sprite in Unity, you need to follow these steps:

  • Create a new script: open your project in Unity. In the “Project” panel, right-click on the folder in which you want to create the script. Choose “Create” -> “C# Script” (if you’re writing in C#) or “Create” -> “JavaScript” (if you’re using UnityScript).
  • Customize the script: Rename the script according to its purpose by right-clicking on it and selecting “Rename”. Double-click the script to open it in the integrated development environment of your choice (such as Visual Studio or Visual Studio Code).
  • Attach the script to the sprite: Go to the scene where the sprite is located. In the “Hierarchy” window, select the sprite to which you want to attach the script. In the “Inspector” component in the panel on the right, find the “Add Component” section. Enter the name of your script in the search field and select it from the list. Your script is now attached to the selected sprite. You can start editing the script, adding logic and interaction with this sprite. When you run the scene, the script will execute when interacting with the sprite or according to your logic.ʼʼ

As you can see, the chat offered a sequence of actions, and also described the work with the IDE, as for a beginner, it was very useful in the first stages.

After starting the program, I found that the hero can only go up when long-tap, but does not go down when removing the finger from the screen. But, the Chat had nothing to do with it, he did what was asked.

Question – “What should be added to the script, so that the object goes down if there is no tap?”

Answer – Chat issued a script that allowed the object to drop.

Then I discovered that my Hero can go beyond the screen at extreme points.

Request – “Change the script so that the object cannot go beyond the screen”.

Answer – Chat rewrote the script with new restrictions, but for some reason set minY and maxY constants so that I could limit the Hero’s progress.

After I asked him to “add constraints not by constants, but by real screen sizes” – I got a script that already met all the requirements for the first stage.

Stage 2 – Adding the option to fire “bullets”

At first, I tried to solve the problem with one “magical” query

The request is “the object should shoot”.

Answer – Chat understood this in his own way and updated the Hero’s script – gave him the ability to move around the screen in 4 directions and shoot when the button is pressed.

Request – “No, you should use the previous script – where the object can move up/down. Just add to it automatic firing of white rectangles from left to right with an interval of 0.5 seconds”

Answer – Chat corrected the script, but as a result it turned out that the rectangles did not fly out of the Hero, but appeared in front of him and flew to the right.

Request – “Make the starting point of the rectangle’s flight calculated from the middle of the height of the current object.”

Answer – Chat wrote a new script and advised me to add a new object for the bullet, the Hero will use it when shooting.

After adding a new empty object to the scene, I asked Chat to write a script for it. Then it turned out that the ball flies from left to right, as it should, but at the same time moves down the Y axis. To eliminate this, I had to disable the gravity option in the Ball object.

The last test at this stage was the correct dislocation of the released balls when they leave the screen. According to the code proposed by Chat, Bullets should destroy the object’s update functions (called constantly for each object):

void Update()

    {

        // Destroy the ball when it goes off the screen.

        if (!GetComponent<Renderer>().isVisible)

        {

            Destroy(gameObject);

        }

    }

Unfortunately, when starting the program, an error – MissingReferenceException – immediately started popping up. And if you don’t delete the object, it remains in memory.

All Chat’s suggestions regarding NULL checks and its combination with isVisible type checks were unsuccessful. After spending some time on all kinds of variations of Chat requests that led me in circles, I turned to Google, where I managed to find a solution.

The orb needed to use the object’s lifecycle function and cause a short in it that would cause the owner of the orb, the object (Hero) to delete it.

void OnBecameInvisible()

    {

        // We check whether there are subscribers to the event and call it

        OnBulletInvisible?.Invoke();

    }

Stage 3 – Adding “Monsters”

To create and animate the Monsters, I basically followed the same steps as for the Bullet. The only difference was that the Bullet was generated by a Hero, and monsters had to be dealt with in this way – to create their own object (MonsterSpawner) that could “shoot” them.

Request – “It is necessary that objects (monsters) fly out on the screen from the right side and fly along the X axis to the left side. The interval of the appearance of monsters is 2 seconds.

Answer – Chat offered me two scripts – Monster Script (MonsterScript.cs) and Monster Spawner Script (MonsterSpawner.cs)

Question – Where to use the MonsterSpawner script? What object to attach with it?

Answer – “Yes, you can attach the MonsterSpawner script to an object in the scene. Create an empty GameObject, select it in the hierarchy, and then attach the MonsterSpawner script to that object. You will then be able to adjust monster spawner options (eg monster prefab, spawn interval, etc.) through the Unity inspector”

Next, it was necessary to kill the monster.

Stage 4 – Make the monster disappear after hitting the bullet

Request – “Monsters should disappear when hit by a bullet.”

Answer – “You need to add Box Collider 2D to your monsters and orbs to detect collisions.” 

Chat also released two updated scripts for Bullet and Monster that took collision into account and a new script for MonsterSpawner. He was removing the monster when it collided and the cloger was triggered. 

I did similar manipulations with the bullet, now I had a scene that looked like something.

Step 5 – Create a background with endless motion.

For the background, I decided to use a print screen of a picture with a starry sky. The idea was to teach this picture to scroll endlessly and most importantly silently along the X-axis, in the form of a background for the scene.

Question – “How to make an infinite background that moves from right to left?”

Answer – “To create an infinite background that moves from right to left, you can use two instances of the background next to each other and move them as you move to create an infinity effect.”

The option with two pictures did not suit me.
Request – “You need to create an infinite background from one picture”

Answer – “To create an infinite background from a single image in Unity, you can use a script that will move the background texture along the horizontal axis, creating an infinity effect.”

The background did not move. The chat tried to give various tips on how to set up the components, but it felt like it was just offering them to me in circles.
Next, I tried to switch from my mother language to English, because I knew that Chat understands it better and gives more accurate answers. The result did not change, they continued walking in circles.
After spending a lot of time, I turned to Google and found a solution there. For the infinity background, it was necessary to create an object of a completely different type – Quad. I also had to create a Material for it, based on my image, and set up a texture shader for it in some way.

After all this, there is probably one last moment left:

Question – “How to make sure that the spheres do not overlap with the background?”

Answer – “So that the spheres do not overlap with the background, you can use the layer system in Unity. Make sure your background and balls are on different layers and adjust their order so that the background is below the balls.”

So I changed the Z hierarchy for the layers and it worked. All that was left, to add the picture.

Step 6 – Add images for all objects

I no longer needed Chat to add pictures to each object. I already knew this from the tutorial. Got the desired result by creating sprites with each pattern and attaching them to the desired objects.

Conclusions

  • Chat really speeds up the development process, offering ready-made scripts upon request and usually with accompanying instructions. 
  • The quality of the answers directly depends on the quality of the requests, the more precise the request with details and nuances, the more likely it is that in response the Chat will start the first time.
  • Some problems Chat could not solve, so you had to search for yourself. For example: memory leaks, the correct operation of the infinity background and the selection of the correct shaders.
  • Unity skills are required to create mini games.
  • According to feelings, Chat can save 20-30% of development time. However, each type of game requires an understanding of how everything should interact and what nuances exist for different cases. 
  • The higher your Unity skill, the more effectively you can work with Chat.
  • The answer from Chat will be more complete if you make the request more clearly and completely. Therefore, I want to emphasize that the free version 3.5 was used during the experiment. It is obvious that the results of the paid (extreme) version will be better.

Author Alexander Bondar