Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to effectively use chat GPT in unity?

Discussion in 'General Discussion' started by marqumax, May 4, 2023.

  1. marqumax

    marqumax

    Joined:
    Jun 17, 2021
    Posts:
    2
    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?
     
    the_unity_saga likes this.
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,836
    Have it explain the parts you don't understand and maybe ask smaller questions, so it can write small chunks
     
    the_unity_saga and chriseborn like this.
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,480
    > 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.
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,010
    Yeah, learn how to code. Don't understand what the code does or how it works? Learn how to code.
     
  5. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
    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.
     
    the_unity_saga likes this.
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,692
    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.

    Sure! Here is an example of a character controller in Unity that uses rigidbodies for physics simulation:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class CharacterController : MonoBehaviour
    4. {
    5.     [SerializeField] private float speed = 5f;
    6.     [SerializeField] private float jumpForce = 5f;
    7.  
    8.     private Rigidbody rb;
    9.     private bool isGrounded = false;
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
    22.         movement = Vector3.ClampMagnitude(movement, 1f);
    23.  
    24.         rb.AddForce(movement * speed, ForceMode.VelocityChange);
    25.  
    26.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    27.         {
    28.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    29.             isGrounded = false;
    30.         }
    31.     }
    32.  
    33.     void OnCollisionStay(Collision collision)
    34.     {
    35.         if (collision.gameObject.CompareTag("Ground"))
    36.         {
    37.             isGrounded = true;
    38.         }
    39.     }
    40. }

    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:

    Code (csharp):
    1. void FixedUpdate()
    2. {
    3.     float moveHorizontal = Input.GetAxis("Horizontal");
    4.     float moveVertical = Input.GetAxis("Vertical");
    5.  
    6.     Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
    7.     movement = Vector3.ClampMagnitude(movement, 1f);
    8.  
    9.     rb.AddForce(movement * speed, ForceMode.VelocityChange);
    10.  
    11.     if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    12.     {
    13.         rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    14.         isGrounded = false;
    15.     }
    16. }
    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.
     
  7. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,963
    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.
     
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,599
    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.
     
    the_unity_saga likes this.
  9. aabduljawad

    aabduljawad

    Joined:
    Apr 1, 2023
    Posts:
    16
    Not yet ;)
     
  10. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    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.
     
    neginfinity likes this.
  11. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    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.
     
    Voronoi likes this.
  12. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
    That is not my experience. Are you testing the free version or the paid version of ChatGPT?
     
    the_unity_saga likes this.
  13. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    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.
     
  14. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,692
    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.
     
    Last edited: May 9, 2023
    the_unity_saga likes this.
  15. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
    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.
     
    DragonCoder and Ryiah like this.
  16. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,480
    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.
     
  17. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,482
    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.
     
  18. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,628
    What do you base this claim on?
     
  19. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,692
    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.
     
    Last edited: May 10, 2023
  20. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,480
    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:
    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.
     
  21. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,692
    I was using Vicuna following these instructions which uses oobabooga.

    https://hub.tcno.co/ai/text-ai/vicuna-cpu/

    It's just that I'm on an 8GB GPU and none of the models fit it.
     
  22. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,480
    Well, yeah, with 8GB VRAM you likely won't have enough. However 12GB GPU is not a particularly powerful videocard these days.

    Also, honestly, those models are the only reason to buy 12GB GPU....
     
  23. Steviebops

    Steviebops

    Joined:
    Apr 9, 2013
    Posts:
    132
    Fairly new to the whole thing myself, seems to be some disagreement on what's the greatest value for Unity devs.
    So is it just not worth paying for GPT Plus at this point?
     
  24. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,692
    I've very much found it to be worth it.
     
    stain2319 and PanthenEye like this.
  25. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,480
    It depends on where you are, what your situation is, and whether chatgpt is accessible to you in the first place.

    I'm in a region which is very thoroughly locked out of the service, and while accessing FreeGPT is possible with some hassle, paying for GptPlus is absolutely not worth it, due to amount of hoops you have to jump through. That's why I'm interested in local models instead. The same deal is with MidJourney, by the way.

    Also, it is a good idea to understand what you want from it. The service is good for talking about things you do not know, exploring new concepts or discovering new things. As a code generator, though, I don't know. Probably wouldn't use it.
     
  26. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    605
    From my limited testing (supplementing normal workflow for the past 3 weeks) I found that Github Copilot increased productivity more. ChatGPT (both gpt3.5/4) gave some "interesting" code at times.

    I think if you are less experienced then ChatGPT is more useful though as you can ask it things.. where as copilot can improve your productivity as an experienced programmer that has a good idea of what code they want.
     
    stain2319 likes this.
  27. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,599
    Was this post generated by ChatGPT? :p
     
  28. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    577
    Is there a difference between Chat GPT+ and using Bing Chat? My understanding was that Bing is using Chat GPT 4 and I've found it quite good at generating usable code. Also, the results seem to be referencing web data that is newer than 2021, it's fairly easy to check using the footnotes provided.
     
  29. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
    In my experience, Bing Chat produces a lot of pseudo code instead of actual usable code when you try to get it to generate systems on wider scale that involve multiple separate scripts. It's also a lot quicker in generation speed than the paid ChatGPT4 so I doubt it's the full model.

    I tried to use it again just now in my standard test of generating a functional Options menu logic and it crapped out in the middle, said it can't do that right now and hid the partly generated code. Albeit, the code it did generate looked alright before it poofed out into nothing.
     
  30. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,570
    Yeah. I recently made a video where I tried Bing Chat GPT to write code for me. It ended-up creating some unusable nonsense.
     
  31. Pacman8030

    Pacman8030

    Joined:
    Nov 3, 2022
    Posts:
    13
    I ask ChatGPT how to do something and it produces a small code example and I take a minute to understand how it works. I don't ask it to code my entire game.
     
    stain2319 and DragonCoder like this.
  32. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
    Looks like they nerfed the paid ChatGPT4. It's much faster now but also produces pretty S*** results as far as code generation for Unity is concerned.
     
    neginfinity likes this.
  33. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,480
    Looks like it is a trend. They've been doing this stuff to FreeGPT since January, where initially you were able to do amazing things, but then OpenAI kept reducing its abilities more and more. Current version has 10% power of the original at most, if not less.
     
  34. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,203
    It is practically useless if can code even a minimum

    Is same as trying to adapt someone else's code than write your own, soon will find out that is 1000x faster to just write your after a point when learned enough.

    It is great if have never seen code before though, maybe give a hint of what to look out for ?

    Will probably be a few decades until can do any number of really complex stuff, it is juts very very basic right now to bother at all.
     
  35. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
    It was/sometimes still is great at refactoring my code into something more readable when I'm under time constraints. And it's definitely better at naming things. It used to be able to work with scripts of around 400 loc, now 200-300 loc scripts seems to be a challenge for it. They might've reduced ChatGPT4 memory. It's still useful for code snippets for API you don't immediately recall from memory. Faster than googling or searching docs since you can ask within context of what you're working.

    I haven't tried API in Playground yet, and I've received GPT4 access. Something to explore in the future.

    Have you tried the paid or the free version? The free version is indeed pretty bad for codegen, the paid one is a mixed bag right now but definitely has its uses.
     
    Ryiah likes this.
  36. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,203
    The free only, i can see the uses in general, just think for me was more lost time than just google things

    Perhaps paid is better though, cant tell on that
     
  37. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,692
    You still need to know how to write code and it still hallucinates, but it's improved enough that when someone sees little value it's a safe bet they've only used the free version.
     
    Last edited: May 25, 2023
    nasos_333 and PanthenEye like this.
  38. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    417
    The paid version is hit or miss for me in terms of writing code and seems to depend on how easy/common the code is. Certain simple things like moving a rigidbody with AddForce that it has probably seen in the training material a thousand times, it does very well for the most part.

    It's really bad about using GetComponent in Update and even if you tell it not to, it will forget and start doing it again in short order...

    I've also been playing with GitHub Copilot and it seems like it does a better job. But I haven't used it for much beyond simple boilerplate so far.
     
    Ryiah likes this.
  39. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    605
    Have you tried Github Copilot and the preview Copilot X?

    I find it quite a lot better than ChatGPT4 even before this "nerf"
     
  40. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
    I briefly tried Copilot last year. It didn't seem that useful because it seemingly wasn't able to analyse further than the script/method I'm in and it suggested a bunch of non-relevant nonsense frequently. But I'm hearing a lot of positive feedback lately. I'll give it a go again.
     
  41. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    605
    https://github.com/features/preview/copilot-x

    Give this a shot (waitlist is just a few days now)
    It gives you an interface similar to ChatGPT but inside your IDE and can answer questions/prompts unlike the old Copilot
     
  42. jiraphatK

    jiraphatK

    Joined:
    Sep 29, 2018
    Posts:
    276
    Things I have used GPT4 to do
    -Editor script to create static class for Tag, Layer and Input Action reference, etc.
    -Script to parse .srt (subtitle file) into a more performance format(scriptable object) during the authoring time.
    -Create Method names, skill names, passive names, talent names
    -Linq
    -Regex
    -File IO stuff.

    There are other things I have tried but it either get it wrong or simply refuse to adhere to the requirement.
    Gameplay code are one such example that you cannot rely on it.
     
  43. Deleted User

    Deleted User

    Guest

    To really explore, take a look at this: A OpenAI package for the Unity Game Engine to use chat-gpt, GPT-4, GPT-3.5-Turbo and Dall-E though their RESTful API (currently in beta). Independently developed, this is not an official library.

    An OpenAI API account is required.

    GitHub: RageAgainstThePixel/com.openai.unity







    "How to keep employed in a world taken over by robots? Train to repair them."

    p-
     
    Last edited by a moderator: May 28, 2023
    Stephen-Hodgson likes this.
  44. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
  45. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    just wanted to add something I learned recently about the free version of chat gpt: it used to have a text length limit but that seems to be gone now, or at least much larger.

    From youtube you can get the transcript of a video, paste into chat gpt and say "summarize" and you get condensed version.

    Very great for deciding whether or not its worth time to watch a longer video or not (like a lot of GDC talks can really be boiled down to a few key points)
     
  46. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    257
    Could it be that it became much worse lately? 2-3 months ago i used it all day and hit the message limit all the time because it was so helpful. Now it just generates garbage and became lazy. I canceled my subscription after it put out this code

    public void YourFunction(){
    //your code goes here
    }

    what happened?

    are there any good alternatives? Is copilot-x (is it „copilot chat“ now? I am confused by the naming) or using the chatGTP api/playground a better alternative?
     
  47. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    417
    Yes, it has been reduced in power multiple times since release. I miss the version from 6 months ago, it was amazing.
     
  48. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,005
    Stanford researchers have confirmed that current ChatGPT4 is far worse in certain cases than it was back when it launched. People theorize they've split the initial large model into multiple smaller, more specialized models so when your prompt is handed to the wrong model or falls between specialization of two separate models, it S***s the bed. But no one knows for sure what's going on behind the scenes. OpenAI is ironically very closed off to the rest of the world.
     
  49. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,365

    funny you should say that, i threw it a class of mine and asked it for performance ways to improve it.. it threw me the usual avoid this, dont that (such as allocations).. but they werent relevant it then took my code, and basically templated it to regions and took all the code out and did as you say "//your code here" comments such as "//Death logic" .. now i appreciate truncing the code to give more answer around it, but you didnt change any code nor make actual helpful relevant comments, you just spouted a generic how to write performant code and turned my work into a handful of comments.. cheers.. for just wasting my time for reading it
     
  50. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,480
    There's a sort of a race to develop a better alternative that can be run locally on your GPU or CPU. There are tons of models being spawned, and it is hard to keep track of all of them.

    Someone mentioned WizardCoder, models like vicuna can be used with code to limited extent. Also, apparently Llama 2 has been recently released and someone already quantized it, then we have open assistant silently being developed.

    So, basically, it is hard to name something immediately, but if you poke around, you may be able to find something "good enough".