Dec 21 • Ashray Pai

Making a VR Game using ChatGPT

With the rise in the usage of AI, I wanted to see if it’s capable of making a VR game from scratch. So to find that out, I used ChatGPT as my AI tool and before I get started with this research, I had to set some rules and those rules were:
  • I ask the AI questions like a beginner with an intermediate knowledge of programming.
  • AI writes the code, I have to just copy and paste it.
  • I will not make any changes to the code directly but I will ask the AI to do it as per my requirement.
  • Errors will be debugged by AI.

So with the rules being set, I could now start developing the game.
YouTube

Want to watch the video instead?

Setting up Unity for VR

Considering I am a beginner not knowing how to use Unity, my first question to the AI was straightforward.
Hey, can you help me create a VR game using Unity?
Although the AI provided detailed instructions, they did not align with my needs. Specifically, I wanted to use the XR Interaction Toolkit package and asked the AI on how to use it.
I want to use XR Interaction Toolkit
The listed steps were insufficient to set up Unity for VR. It asked me to refer to the documentation for more details, but this was not satisfactory.
So, the AI did not entirely help me set up my Unity Project. Since I already have a base project setup for VR using the XR Interaction toolkit, I went ahead and used it.
💡 You can get the Base Project as well, check out this video.

Game Development

My goal is to create a basic shooting game in which:
  • Enemies spawn randomly, pursue the player, and shoot at them when they are nearby.
  • The player can shoot enemies as well.
  • The game ends when the player's character is killed.
  • The number of enemies increases as they are defeated.

This outline provides the core features of the game, but there may be additional details to consider as I continue to develop and refine it.

Getting the code

Based on the game features, here are the following questions I asked the AI:
  1. For shooting bullets on the trigger press
    a)
    Write a C# script that will shoot bullets on the VR controller trigger press
    b)
    Update the script to use Input Action Reference to trigger the bullet
  2. For enemies to follow the player and stop when it is within a given proximity
    a)
    Write a C# script to make the enemy follow the player
    b)
    Update the script to make the enemy stop moving when it comes close to the player
    c)
    Update the script to move the enemy only in x and z axis, keeping the y axis same
    d)
    Update the script to make sure the enemy looks at the player
    e)
    Update the script to find the player automatically
  3. For enemies to shoot bullets when it’s close to the player
    a)
    Write a C# script where the enemy shoots bullets once it stops moving
    b)
    Write a separate C# script that will shoot bullets once the enemy is close to the player
    c)
    Update the script to find the player automatically
    d)
    Update the script to shoot bullets once every few seconds
  4. For managing health
    a)
    Write a C# script to manage the health of the player
    b)
    Write a C# script to manage the health of the enemy
  5. For applying bullet damage
    a)
    Write a C# script where the bullet applies different damages to the player and enemy based on what it collides
  6. For spawning enemies with increasing frequency
    a)
    Write a C# script to spawn enemies with increased frequency within a given area
  7. For keeping a count of enemies that were killed
    a)
    Write a C# script to keep a count of the enemies that have been killed
  8. For handling the death of the player and enemy
    a)
    Update the enemy health script to increase the count of enemies killed count
    b)
    Update the script to find the enemy counter automatically
    c)
    Update the player health script to invoke a Unity event when the player dies
  9. For displaying the count of killed enemies
    a)
    Update the enemy count script to display the count as test on a canvas
    b)
    Update the script to use TextMeshPro
  10. For displaying player health
    a)
    Update the player health script to display the current health as text on a canvas
    b)
    Update the script to use TextMeshPro
    c)
    Namespace UnityEvent could not be found an error
After every question I asked, I copy pasted the script into Unity and tested it. If it did not work as expected, I would ask the AI to update the script.
Now that I had most of the script ready and done testing it, it was time to import assets and make the game.

Scene Setup

I thought it would be fun to use chickens as enemies and eggs as their ammo, so I imported them from the Unity asset store.
Then, I imported a gun and bullet assets for the player.
Created Canvases to display the Kill Count and the health of the Player. Just to keep it simple, placed the Kill Count Canvas at one of the sides and the player health at the top of the left gun.
I added all the scripts and tested the game and while doing so, I found that:
  • I needed a script to reload or exit the game once the player dies
  • The player was getting disabled when the player dies which I did not want.
  • The enemies would continue to spawn even after the player has died
  • I needed a script to destroy the existing enemies when the player dies
  • The bullets were not getting destroyed after it has hit something
  • Nothing happens when the player or the enemy falls from the play area.

Which meant it was time to go back and ask the AI to fix it.

Back to Coding

Here is what I asked the AI to do:
  1. For reloading the scene
    a)
     Write a C# script to reload the scene on button press and also exit the game on another button press
    b)
     Error button namespace could not be found
  2. For making sure the player does not get disabled
    a)
     Update the player health script to not disable it when the player dies
  3. For destroying existing enemies
    a)
     Write a C# script to destroy all the existing enemies when the game ends
    b)
     The script does not work as the enemies are spawned at run time
  4. For keeping track of the spawned enemies
    a)
     Update the enemy spawner script to add enemies to the list in the enemy destroyer script
    b)
     Update the script to find the Enemy Destroyer automatically
    c)
    Update the script to spawn enemies with an increase in frequency over time
  5. For destroying the bullet
    a)
     Update the bullet script to destroy the bullet if it does not collide with anything after a few seconds
    b)
    Update the script to use OnCollisionEnter
  6. For handling the event when a player or enemy crosses the play area
    a)
     Write a C# script that will check if the enemy or player has entered. If the enemy has entered it should destroy the enemy and increase the enemy counter value. If the player has entered it should restart the scene
So with that, I had everything that I needed and it was time for the final polishing.

Conclusion

The AI was able to generate code for various aspects of the game, including shooting bullets, enemy behaviour, health management, spawning enemies, keeping track of enemy kills, handling player and enemy death, and displaying information to the player. However, the AI was not able to fully assist with setting up a Unity project for VR, and the researcher had to rely on a pre-existing base project instead.
Overall, I can say that AI did make this VR game for me but the game was not optimized and the codes were not of production level. For example, it uses public property instead of private in most of places. While spawning enemies it does not make use of object pooling. It sometimes even changed the code entirely when asked to update by just making a small change.
It’s okay to take the help of AI for prototyping and getting answers if you are stuck but you will have to make quite a few changes to it. You will not be able to use AI to create a production-level game.
💡 Fun fact! The title image of this blog post was generated by AI as well.

Thanks for reading this blog post 🧡

If you've enjoyed the insights shared here, why not spread the word? Share the post with your friends and colleagues who might also find it valuable.
Your support means the world to us and helps us create more content you'll love.