A Publication of the Public Library Association Public Libraries Online

How I Used ChatGPT to Make a Video Game

by on May 15, 2023

Artificial Intelligence or AI for short is on fire, ranging from conversations about it being used ethically, whether it’s the topic of artists having their work stolen, or students using it to do their homework. You can make a picture book with AI, opening several questions in library land. If we’re going to have to start cataloging works based on if an AI did it, or a human did it. In the future, will patrons make these requests, “I want to read something a human wrote, no AI at all?”

The debate on the ethics of AI is lengthy, especially in terms of art, but one aspect of the debate is using AI as a tool. Whether you use it as a tool to brainstorm ideas for projects or use it to help your writing- it’s a very powerful tool- so powerful, I asked it to help me make a basic 2D
Platformer.

Recently as a graduate student, I took Team Studio, a class where you work with other students in creating games for your portfolio. I wanted to use Unity’s visual scripting for the project, but that ended up not working out. I now, as a librarian, had to learn C# from scratch in order to create a player controller for my character. The player controller would have the ability to move, flip, jump, and be able to receive damage. Since I had previously used visual scripting to make my previous platformer and have fun in Dreams, doing the same via C# was a challenge,
especially since we had an eight-week time limit. Coding is not something you are taught in library school. If anything you learn some HTML/CSS to help design webpages. I already knew some back from my Neopets, and MySpace days in the mid-2000s. Therefore, the transition to learning C# was more doable compared to if I didn’t know any coding at all, but here’s what makes programming so difficult- there are more than 200 ways to program movement, and there’s no right way of making a frog jump for example, you just have to make programming work for you.

There are benefits to visual scripting and traditional scripting in terms of making video games. My argument for visual scripting is that for us librarians since books are our main specialty, it’s easy for beginners to feel empowered to want to try to make video games, so it would be easy with some time for it to be offered as a class to the patrons. All you are doing is connecting nodes with each other. I highly recommend Dreams and Game Builder Garage for video game collections, because those games teach you the skills needed to do visual scripting. However, there are more tutorials and guides out there for traditional coding, which made using C# to do the programming for our student project more feasible. Look at the below picture, one is visual scripting while the other is C#. Both are scripts that make a game object rotate and spin. Which is easier? It’s up to you to decide.

There are plenty of Youtube tutorials out there that I followed in teaching myself C#. However, finding tutorials that actually help, and are not outdated was difficult to find. I got my player controller to move, but it could not jump. Finally, I found one where you had to add an empty game object of “ground check” to the player and tag the layers. Hurray, my player jumps! But then, after my teammate made a script for health that I tried to incorporate, now my player refused to jump. I was stressed out, for why was the player not jumping now?

Game development is “figuring out why this is not working, you finally fix it, but in fixing that you break something else.” My teammate ended up saving the day by having to create an entirely new player controller script. It was extremely stressful having to learn enough C# in a short time frame to get a working player, so the professor gave the suggestion of using ChatGPT to write scripts. While the game we created for class features no ChatGPT scripting, his advice did inspire me to do this pet project. I was able to make a player controller script that both moves around and jumps using tags and layers, I did not even have to do the method of adding a ground check game object to my player like other tutorials I followed did. This is what I created: a very simple, 2D platformer where you can walk and jump around. The artwork is all from Canva, as I cannot draw/animate well.

You cannot hurt enemies or take damage as of right now, but the goal is eventually, using ChatGPT alone to make the scripts capable of doing so. Eventually creating a more fleshed-out 2D Platformer is the goal, but the fact that ChatGPT was able to create a decent player controller script, as well as enemy patrol scripts that work was impressive. I did not have to do the ground check game-object trick that so many of the player controller tutorials I saw utilized. Here is the catch- you have to know some C# to utilize it properly. You have to know how the
syntax works and what certain terms mean, otherwise just copying and pasting it directly onto Visual Scripting may cause compiler issues for your game, and make your game unplayable.

Here is an example of how I told ChatGPT to write the scripting.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 10f;
public AudioClip jumpSound;
private Rigidbody2D rb;
private AudioSource audioSource;
private bool isGrounded;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
audioSource = GetComponent<AudioSource>();
}
private void FixedUpdate()
{
float moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
Jump();
PlayJumpSound();
}
}
private void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
isGrounded = false;
}
private void PlayJumpSound()
{
if (jumpSound != null)
{
audioSource.PlayOneShot(jumpSound);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;

Then, in Unity, all I had to do was edit the PlayerController script, or in the case of starting it from scratch, create a new script by clicking “add component” at the bottom.

Then just copy and paste it into Microsoft Visual Studio, save, add the jump audio sound, and then you are done.


Tags: