XR Step-by-Step 2023! Unity + C# + ChatGPT = Mastering AI-Powered XR Coding

We keep hearing “ChatGPT is going to eliminate software developers!” — NOT true any time soon.

What ChatGPT and other A.I. tools WILL DO is to empower software developers who DO know how to use these tools over the developers that DO NOT!

Think of a developer that doesn’t know how to use Google or Stack Overflow to find coding answers… errrr… you can’t? Me neither. Those that couldn’t went extinct like the dinosaurs. 🙂 Don’t be a Dev Dinosaur, master AI-Powered XR coding!

Want to see the other blog articles in this (and other) series?
Click here to see all of my XR Step-by-Step blogs separated by device and version!

What is ChatGPT? ChatGPT is a FREE Artificial Intelligence language model released to the public in November 2022 by OpenAI. Within just 5 days of it’s release, it had 5 million users!!

With ChatGPT you can type any question you can think of… The same way you’d chat with any human online, and ChatGPT will almost always give you an amazing answer that exceeds your expectations.

Luckily for programmers like us — that also includes being able to write actual code!

Let’s just jump on in…

  • Setup your Unity tools and project for Quest or Hololens – see instructions for both here – XR Step-by-Step 2023.
  • We’ll keep going with the Hololens example, but you could easily do this for the Quest example as well. We left off with having the cube floating in front of us…
  • Questions you ask ChatGPT are called “prompts” – and as you’ll see if future articles, writing good prompts is a critical skill you’ll develop
  • Let’s have ChatGPT create a Unity script to make the cube rotate around the z-axis once every 2-seconds
  • Let’s try the prompt “create a Unity C# script for a VR application that can be added to a GameObject that makes it rotate around the z-axis once every 2-seconds” and press Enter
  • Wow! That’s actual code – it’s simple, and we’ve written it before in prior articles – but seeing it auto-generated from our prompt is pretty amazing…
  • Note it also gave us a speed variable that matches our “rotate around the z-axis once every 2-seconds”. Everything is even commented very nicely.
  • I want it to be named Rotate not RotateAroundZ — easy to do — just ask ChatGPT to do so… “rename the script to Rotate”
  • Click Copy code
using UnityEngine;

public class Rotate : MonoBehaviour
{
    // Speed of rotation in degrees per second    
    public float rotationSpeed = 45f; 
    
    void Update()
    {
        // Rotate around z-axis
        transform.Rotate(0f, 0f, rotationSpeed * Time.deltaTime); 
    }
}
  1. (Unity -> Project) Click Scripts folder
  2. Right-Click in the details section for Assets/Scripts
  3. Click Create
  4. Click C# Script
  5. Name the script Rotate and double-click on the Rotate script to launch Visual Studio
  • (Unity) Back in Unity, you’ll see your updated script!
  1. (Hierarchy) Click Cube
  2. (Project -> Scripts) Drag the Rotate script to the bottom of the Inspector for the Cube
  3. (Inspector) You’ll see your script added! Note, you can change your Rotation Speed value here if you’d like to see it go faster or slower
  4. (Unity) Press Play
  • Look at that rotating cube!
  • Nice! Does what we wanted – but that was pretty easy… most of us could have googled that and found it pretty quickly. Now let’s turn up the difficulty for ChatGPT…
  • Let’s ask ChatGPT to “change the script so that the object rotates one rotation around the z-axis, then one rotation around the y-axis, then one rotation around the x-axis and continues this pattern of rotations and each time it finishes any of these rotations I want it to randomly change color”
  • Wow — now that’s not code that I could have googled easily…
  • Let’s try it. Just update the code in your Rotate script, save it – and press Play.
  • Here’s the final code that ChatGPT and I (note, I had to tweak it) — came up with…
    • IMPORTANT NOTE – at first the colors weren’t working – but with a few tweaks, and adding the colors to an array on start(), fixed that!
    • Again this is where YOUR critical thinking skills and experience comes in!!
    • This is why ChatGPT is NOT replacing programmers any time soon… It gets you close to a solution – that you can quickly learn from – but it isn’t always correct.
    • You may have to go through a couple iterations with ChatGPT telling it what errors you’re getting, or what’s not working. It’ll make corrections / suggestions.
    • But in the end – use these tools – don’t be a Dev Dinosaur, master AI-Powered XR coding!
using UnityEngine;

public class Rotate : MonoBehaviour
{
    public float rotationSpeed = 45f; // Speed of rotation in degrees per second
    private Color[] colors; // Array of colors to choose from
    private bool isRotating = false; // Flag to check if the object is currently rotating
    private int rotationIndex = 0; // Index of the current rotation
    private Quaternion targetRotation; // Target rotation of the current rotation

    private void Start()
    {
        colors = new Color[] { Color.red, Color.green, Color.blue, Color.cyan, Color.white, Color.magenta, Color.yellow };
    }

    void Update()
    {
        // Check if the object is currently rotating
        if (isRotating)
        {
            // Rotate the object around the current axis
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
            if (Quaternion.Angle(transform.rotation, targetRotation) <= 0.1f)
            {
                StopRotation();
                ChangeColor();
            }
        }
        else
        {
            // Choose a new rotation axis and start rotating
            rotationIndex = (rotationIndex + 1) % 3;
            Vector3 rotationAngles = Vector3.zero;
            if (rotationIndex == 0)
            {
                rotationAngles = new Vector3(0f, 0f, 90f);
            }
            else if (rotationIndex == 1)
            {
                rotationAngles = new Vector3(0f, 90f, 0f);
            }
            else if (rotationIndex == 2)
            {
                rotationAngles = new Vector3(90f, 0f, 0f);
            }
            targetRotation = Quaternion.Euler(rotationAngles) * transform.rotation;
            isRotating = true;
        }
    }

    // Stop the current rotation
    void StopRotation()
    {
        isRotating = false;
    }

    // Randomly change the color of the object
    void ChangeColor()
    {
        int randomIndex = Random.Range(0, colors.Length);
        GetComponent<Renderer>().material.color = colors[randomIndex];
    }
}

In summary, as amazing as ChatGPT is for helping programmers write code, there are some limitations to be aware of:

  • FREE versus Premium Subscription: With great popularity comes the limitation that ChatGPT often sees a huge amount of traffic, and users were being blocked as the service was at capacity. In response to this OpenAI launched ChatGPT Plus – a premium subscription for $20/month that prioritized Plus members over free members for access to the service. In my opinion, this is very reasonable and I for one am a Plus member. 🙂
  • Text-Based: For now, ChatGPT is strictly a text-only language model and does not generate any AI images – this however is in their near term pipeline to add AI images and video features.
  • Incomplete Understanding: While ChatGPT has a vast knowledge of programming languages and concepts, it’s understanding is based on the training data available up to September 2021. Therefore, any new languages, frameworks, or updates released after that date will not be part of the generated solution.
  • Ambiguity: If a question or request is not specific enough, ChatGPT may not provide the most accurate or helpful response – it will take it’s best guess at what you want. Writing “prompts” for ChatGPT is a skill, but one you’ll quickly pick up on by using it on a regular basis.
  • Code Quality and Efficiency: Although ChatGPT can generate code snippets, the quality, efficiency, and adherence to best practices may not always be guaranteed. Your critical thinking skills are crucial to taking advantage of ChatGPT as a code generation tool. It’s always recommend to double-check the generated code and make adjustments as needed.
  • Code Errors: Sometime ChatGPT generates code with errors. When this occurs, tell ChatGPT what the error was and it will often come up with a solution. Why it doesn’t come up with the correct solution the first time, I’m not sure – but this will likely improve as it learns and as you provide feedback.
  • Security and Reliability: ChatGPT can’t guarantee the generated code to be free from security vulnerabilities or potential bugs. ChatGPT learned how to code from examples online, so be sure to review and test the code thoroughly before using it in a production environment.
  • Overall Answer Length: ChatGPT operates with a token limit (a token can be as short as one character or as long as one word). The maximum token limit for GPT-4 is around 4096 tokens. It is better to break down any request into smaller, manageable parts and ask for guidance on each part individually. This will help you receive more accurate and useful code examples that you can then piece together to form a complete solution. ChatGPT should be treated as a starting point and not as a complete or infallible solution.

In conclusion, ChatGPT is a extremely powerful tool that can give you a competitive edge in your XR development process. By incorporating AI tools into your development process, you can streamline your workflow, write code faster and more efficiently, and ultimately you’ll begin to master AI-Powered XR Coding!

Build something amazing in XR!!

Want to see the other blog articles in this (and other) series?
Click here to see all of my XR Step-by-Step blogs separated by device and version!

One thought on “XR Step-by-Step 2023! Unity + C# + ChatGPT = Mastering AI-Powered XR Coding”

Leave a Reply