Brief overview of ChatGPT and its potential uses in game development
ChatGPT (Generative Pre-trained Transformer) is an artificial intelligence language model that has been pre-trained on a massive amount of text data. It is capable of generating human-like language and can be used for a variety of natural language processing tasks, including text completion, summarization, and translation.
In game development, ChatGPT can be a valuable tool for generating code and providing suggestions or prompts for developers. By inputting a description of a desired game feature or behavior, ChatGPT can generate code that can help developers save time and improve the quality of their work.
For example, ChatGPT could be used to generate code for complex AI behaviors, physics simulations, or other game mechanics. It can also be used to suggest improvements or optimizations to existing code.
While ChatGPT is not a replacement for skilled game developers, it can be a valuable tool to help streamline the development process and allow developers to focus on the creative aspects of game development. As AI and machine learning continue to advance, it’s likely that ChatGPT and other similar tools will become increasingly important in game development and other fields.
Introduction to the specific task of creating floating stones that change speed based on the player’s distance
In an existing game, I was tasked with implementing a group of floating stones that would change behavior as the player moved closer to them. In their idle state, the stones should float smoothly and slowly, but as the player approaches, they should start to jitter more and more.
This required creating a class, implementing dependencies, and other code that couldn’t be achieved through animator controllers or libraries. While this wasn’t a “nightmare” level task, it was still time-consuming. ChatGPT proved to be a useful tool for generating code snippets and saving time in the development process.
Explanation of how ChatGPT can be used to generate code for game development tasks
When working with ChatGPT, it’s important to start with the right context. In my case, I began with a promo message about the technology I planned to use. It’s important to keep in mind that ChatGPT is just a tool and won’t generate ideas or code on its own. You need to provide clear and detailed input for the best results.
That said, ChatGPT can be a real-time-saver. When using the tool, it’s essential to provide a detailed description of what you’re trying to achieve. The more information you provide, the better the output will be. It’s important to note that using ChatGPT should take less time than achieving the same result without it. So, don’t be afraid to put in the effort to provide a detailed description. With ChatGPT, you can streamline your development process and focus on other aspects of your project.
Example prompts and code snippets used to generate the necessary code for this task
Let’s dive into the practical use of ChatGPT. As I mentioned earlier, I started by providing context about my game engine and coding language.
I want to ask you to help us create some code for my game based on Unity SDK, C# code language.
ChatGPT responded kindly, and I moved on to the next step — providing a detailed description of the task and its conditions.
A few words about the context of the task. In the game, players can find floating stones. These stones have random directions of jittering, but this jittering is very smooth. If the player appears within around 10 meters of the stone, the jittering speed becomes faster and the speed depends on the player’s distance to the stone (more if closer).
As a result, ChatGPT provided me with a basic realization of the class I needed to implement. While the code wasn’t perfect, it covered around 80% of what I needed to do.
using UnityEngine;
public class FloatingStone : MonoBehaviour
{
public float maxSpeed = 1f;
public float minDistance = 10f;
private Vector3 initialPosition;
private float initialSpeed;
private bool isPlayerNearby;
private void Start()
{
initialPosition = transform.position;
initialSpeed = Random.Range(0.2f, 0.5f);
}
private void Update()
{
float speed = isPlayerNearby ? CalculateJitteringSpeed() : initialSpeed;
transform.position = initialPosition + new Vector3(
Mathf.PerlinNoise(Time.time * speed, 0) - 0.5f,
Mathf.PerlinNoise(0, Time.time * speed) - 0.5f,
Mathf.PerlinNoise(Time.time * speed, Time.time * speed) - 0.5f
);
}
private float CalculateJitteringSpeed()
{
float distanceToPlayer = Vector3.Distance(transform.position,
PlayerController.instance.transform.position);
float speed = Mathf.Lerp(initialSpeed, maxSpeed, (minDistance - distanceToPlayer) / minDistance);
return Mathf.Clamp(speed, initialSpeed, maxSpeed);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
isPlayerNearby = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
isPlayerNearby = false;
}
}
}
Next, I made some corrections — I added context about using Zenject for player injection and refactored the class to align with project conventions. I then asked ChatGPT to use my updated code version and add jittering rotation to the object based on the same conditions.
After this step, I received the final version of the code that I could use in my project. I made some minor changes on my end, and in the last step, I asked ChatGPT to add XML comments to the final class and received the desired result.
The final result of the code
public class FloatingStone : MonoBehaviour
{
[SerializeField] private SpeedSettings speedSettings;
[SerializeField] private float minDistance = 10f;
[SerializeField] private float amplitude = 0.5f;
[SerializeField] private float rotationAmplitude = 1f;
[SerializeField] private new SphereCollider collider;
private Vector3 initialPosition;
private Quaternion initialRotation;
private float initialSpeed;
private bool isPlayerNearby;
private Transform player;
[Inject]
private void Constructor(IPlayer player)
{
this.player = player.Transform;
}
private void Awake()
{
initialPosition = transform.position;
initialRotation = transform.rotation;
initialSpeed = speedSettings.GetRandomSpeed();
collider.radius = minDistance;
}
private void Update()
{
float speed = isPlayerNearby ? CalculateJitteringSpeed() : initialSpeed;
Vector3 newPosition = initialPosition + new Vector3(
Mathf.PerlinNoise(Time.time * speed, 0) - amplitude,
Mathf.PerlinNoise(0, Time.time * speed) - amplitude,
Mathf.PerlinNoise(Time.time * speed, Time.time * speed) - amplitude
);
Quaternion newRotation = initialRotation * Quaternion.Euler(
Mathf.PerlinNoise(Time.time * speed, 0) * rotationAmplitude - rotationAmplitude / 2f,
Mathf.PerlinNoise(0, Time.time * speed) * rotationAmplitude - rotationAmplitude / 2f,
Mathf.PerlinNoise(Time.time * speed, Time.time * speed) * rotationAmplitude - rotationAmplitude / 2f
);
transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, 0.1f);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 5f);
}
private Vector3 velocity = Vector3.zero;
private float CalculateJitteringSpeed()
{
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
distanceToPlayer = Random.Range(distanceToPlayer - 0.1f, distanceToPlayer + 0.1f);
float speed = Mathf.Lerp(initialSpeed, speedSettings.GetMaxSpeed(), (minDistance - distanceToPlayer) / minDistance);
return Mathf.Clamp(speed, initialSpeed, speedSettings.GetMaxSpeed());
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
isPlayerNearby = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
isPlayerNearby = false;
}
}
[Serializable]
private struct SpeedSettings
{
[SerializeField] private float max;
[SerializeField] private float from;
[SerializeField] private float to;
public float GetRandomSpeed() => Random.Range(from, to);
public float GetMaxSpeed() => max;
}
}
How ChatGPT can save game developers time and improve code quality
ChatGPT is a powerful tool that can help game developers save a significant amount of time and improve the quality of their code. In fact, it can save up to 60–70% of the time needed for task implementation. As a professional developer, I knew how to implement a task from scratch, but still needed to write each line of code, optimize it, and debug it. However, with ChatGPT, I was able to save a lot of time on the first point and focus on the last two points. This is the main idea of my article about using ChatGPT for coding in real life.
In game development, time is a crucial factor. Developers have to work under tight schedules and deadlines. This is where ChatGPT can come in handy. It can help game developers save time by providing them with basic code implementations that they can use as a starting point. With ChatGPT, developers can get a basic realization of the class they need to implement and then modify it according to their needs. Another advantage of using ChatGPT is that it can improve the quality of code. ChatGPT provides developers with different code options that they can use as a reference. This can help developers write cleaner, more efficient code that is easier to maintain and debug.