How to effectively use chat GPT in unity?

I’ve been really addicted to using chat GPT both 3.5 and 4. And it was capable of coding in seconds, what would take me hours. However I often found myself not fully understanding the code. When the code got too big and chat gpt wouldn’t be able to find the bug, I was often stuck due to my lack of understanding of my code. Do you have advice how to use chat GPT effectively?

2 Likes

Have it explain the parts you don’t understand and maybe ask smaller questions, so it can write small chunks

2 Likes

How to effectively use chat GPT in unity?

If you’re addicted to something, that does not mean that thing has a lot of practical application.

In case of ChatGPT you can use it to explain things you don’t know or analyze code. Or produce small code examples.

. When the code got too big and chat gpt wouldn’t be able to find the bug, I was often stuck

Of course you will get stuck. When you ask ChatGPT to do your job you will learn nothing in the process. Learning nothing means you don’t understand how it works, and if you don’t understand how it works, you won’t be able to fix it when it inevitably breaks.

To stop being stuck, you’ll need to learn programming yourself.

7 Likes

Yeah, learn how to code. Don’t understand what the code does or how it works? Learn how to code.

5 Likes

Yea, you need to understand most if not all of it to work with it effectively. There are several things you could try.

  1. Copy paste the code it generates into a new prompt and ask it to refactor with human readability in mind.
  2. Ask it to explain parts you don’t understand.
  3. Ask it to comment on each method, or even each line of code, but you’ll run into token limits quite fast with this. Better to ask about things you don’t actually understand yet.
  4. Use 4 as much as possible, 3.5 produces results of significantly lower quality including more bugs.

There’s no shortcut here besides using ChatGPT to speed up the learning process. At the end of the day you have to understand the code it generates to use it effectively.

1 Like

Ask it to explain the code to you. Ordinarily it will give you a very basic explanation of what the code is doing but it can be asked to give a more in-depth explanation especially if you ask it about a particular section of the code.

Initial request to generate a character controller that uses rigidbodies

Sure! Here is an example of a character controller in Unity that uses rigidbodies for physics simulation:

using UnityEngine;

public class CharacterController : MonoBehaviour
{
    [SerializeField] private float speed = 5f;
    [SerializeField] private float jumpForce = 5f;

    private Rigidbody rb;
    private bool isGrounded = false;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
        movement = Vector3.ClampMagnitude(movement, 1f);

        rb.AddForce(movement * speed, ForceMode.VelocityChange);

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isGrounded = false;
        }
    }

    void OnCollisionStay(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }
}

Explanation of FixedUpdate() and it’s contents

FixedUpdate() is a method in Unity that is called at a fixed interval, typically 50 times per second by default. This makes it a good place to handle physics calculations, as physics simulations require consistency to be accurate.

In the context of a character controller, FixedUpdate() is a good place to handle movement calculations. In the example character controller I provided earlier, the code in FixedUpdate() is responsible for moving the character based on user input and applying a force to simulate jumping.

Here’s a breakdown of the contents of FixedUpdate() in that example:

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
    movement = Vector3.ClampMagnitude(movement, 1f);

    rb.AddForce(movement * speed, ForceMode.VelocityChange);

    if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    {
        rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        isGrounded = false;
    }
}

The first two lines of the method use Input.GetAxis to get the user’s input on the horizontal and vertical axes. This input is then used to create a Vector3 called movement.

The ClampMagnitude method is then used to ensure that the magnitude of movement is no greater than 1. This is done to prevent the player from moving too quickly.

rb.AddForce is then used to apply a force to the character’s Rigidbody based on movement and a speed value. The ForceMode.VelocityChange argument is passed to ensure that the force is applied immediately, rather than being accumulated over time.

Finally, the code checks if the user has pressed the spacebar (Input.GetKeyDown(KeyCode.Space)) and the character is grounded (isGrounded). If both of these conditions are true, a force is applied to the Rigidbody to simulate jumping. The ForceMode.Impulse argument is passed to apply the force as an instantaneous impulse.

Note that the isGrounded variable is set in the OnCollisionStay method, which is called when the character is in contact with a collider that has the tag “Ground”.

In summary, FixedUpdate() is a method in Unity that is called at a fixed interval and is useful for handling physics calculations. In a character controller, FixedUpdate() is a good place to handle movement and jumping calculations based on user input.

Beyond that like everyone else said you need to learn programming, the language, and the frameworks involved.

5 Likes

Refine the question so Chat GPT explains the part(s) you don’t understand. I’ve been losing my mind about how great AI is.

4 Likes

It sounds like you’re using it at least somewhat effectively already, but have some unrealistic expectations of what it can do for you. It might speed things up, but it can’t do your job for you.

In the case of programming something your job isn’t necessarily to write every single character of code yourself, but it is your job to provide code which correctly reaches the intended result. To do that you need to understand the code yourself. If you can’t understand what the text generator is spitting out then that’s the issue you need to fix, not how you’re telling it what to spit out.

Even if you ask it to explain things for you, a part of your job would be to verify it’s explanation.

1 Like

Not yet :wink:

You are overestimating what it can do for you. I have tried gpt for several weeks and the quality of the code is terrible. I would recommend learning coding first otherwise you won’t get the result you have in mind.

1 Like

i consider it mainly as a translation device.

translate code to english, or english to code. I even copy paste forum post into it if the original text is unclear.For translate english to code, i have to be very precise if i want results that could actually be used directly. So I am still doing 90% of the work, but the annoying stuff like having to lookup the correct syntax is handled by the AI. So it just saves time is all.

I also use it convert english to math, and vice versa. This is helpful for somebody who doesn’t have background in math, but often times the basic notion of what you want to do is there, you just don’t know how to describe it with math. chatgpt is excellent at understanding plain language and then giving back some great examples broken down into math formulas.

1 Like

That is not my experience. Are you testing the free version or the paid version of ChatGPT?

1 Like

It didn’t matter. The code was buggy, slow and often wrong and GPT doesn’t know what it is doing even for simple algorithms. As soon as I started complaining that it did something wrong, it changed the code and made it worse even though my complaint was actually wrong.

I have mainly tried to generate some DOTs code to get into it again after a long pause. It’s okay to get an idea of how things work, but in the end, I rewrote the generated code again, as it was simply not working. It might be ok to generate some pseudo-code for a small algorithm other than that it’s quite useless in my opinion.

BTW: You should also be aware that it wastes so much energy and water for cooling, it’s an environmental catastrophe if we start using that crap intensively on top of our already incredible energy consumption.

GPT-4 consistently produces better code than GPT-3.5.

My experience has been mostly the opposite. Performance isn’t always ideal but the code rarely fails to work out of the gate. Yesterday I gave it a task to write an entire class with multiple methods. It ended up using LINQ which wasn’t ideal but the class worked flawlessly.

GPT’s training data has a cutoff of September 2021 which means at best it’s only aware of 0.17, and since there wasn’t a ton of information on the web for it it has far less to work off of. Anything that has had a ton of material released online is going to fare very well. Anything esoteric far less so. DOTS is just too new.

1 Like

Definitely matters, ChatGPT4 is a whole different beast, it’s more advanced in every single metric with double the memory of free ChatGPT 3.5. It’s not just an incremental improvement, it’s a night and day comparison. FreeGPT is a nice toy and occasionally useful, but ChatGPT4 can actually be used for work in my experience.

DOTs as in ECS or Jobs? Its training data is cut off on September 2021, so for anything that has come out in the past two years, such as ECS 1.0, you won’t get anything usable out of it. For the good old Monobehaviour, however, it generates usable code in my experience since it has such a long history with very few changes.

Most new tech is inefficient, there are open source models in the works that already work on solving this issue. At the current pace, it will be solved soon. The benefit this brings to things like medical research and other life-changing industries far outweighs energy concerns.

2 Likes

The issue here is that GPT-4 is not as readily available as “FreeGPT”. Personally I’m going to wait till local LLMs properly takeoff. There was some interesting progress going on there.

But in terms of results it’s incremental at best, because the tech seems to already be close to a ceiling and increasing the amount of data used to train and the ram it uses can only help so much.

What do you base this claim on?

Local LLMs aren’t as readily available either. Most people can run them but the performance is insanely low if you don’t have an expensive graphics card. I briefly tried out Vicuna and on my 5950X I only achieved 0.33 tokens per second with the first prompt of a thread with each subsequent prompt taking progressively longer.

If I wanted to have acceptable performance I’d have had to purchase a graphics card with 24GB VRAM which is around $1,000 for a renewed model with brand new ones starting around $1,200.

Researchers have suggested the way forward to more powerful models is likely going to be simpler models rather than more complex ones but with more advanced training tools. OpenAI has mentioned heading down that path.

It sounds like you’re running it on CPU, probably using koboldcpp. You need, ahem, oobabooga software (good luck spelling this right on the first try) to run it on GPU. I get 11 tokens per second. Aitrepreneur on youtube explains how to set it up. Here’s an example:

9005164--1240954--upload_2023-5-10_20-18-11.png

^^^ This is local run. I’m on 12 GB GPU. This is “vicuna-13b-GPTQ-4bit-128g”.

For me it works faster than FreeGPT. There are also 7GB models.