In terms of game development I am
a total beginner. I know some of the basics of Unity. Rider holds no
secrets for me. So I'm wondering... Can I script A unity game with
AI assistant? If you think I can, like this video. And subscribe to our channel
for more of these videos. Now let's hit play! I already prepared an empty Unity 3D project
with one modification: I downloaded two different assets that I can use in the game to
visualize things later on. I have brick figures and I have bricks that I can use on the scene.
And with that, let's see if we can code a game. So what I'm going to do first is create a
new empty game object and I'm going to call that one "Main", and this empty main game object
will be the one that controls the entire game. To control the game I'm going to
add a new component, a new script, and I will call that one "GameController". That
GameController is now being added to my assets, it's linked up to the empty game object, so
let's see if we can open this one up in Rider. In Rider I already configured AI assistant.
You will need a subscription for that or use the free trial. But with that, we can
just start coding using the AI assistant. The start method here is the method that I will
use to start generating the game scene that I have in my game. So what I want to do is create a grid
that is aligned on the x and z axis and that will use one of the prefabs that I downloaded earlier
from the asset store. So what I'm going to do in this start method I'm going to Alt+Enter, use
the AI, AI actions, and then Generate code... The prompt I have here is very
specific. I will read it to you, I just pasted it because it's very long and
I don't want you to see me typing but it's: "generate a 5x5 grid on the X and
z-axis containing the grid element preap with a 1.1 distance between each
elements scale the grid element prefab to 5 by 5 and color the grid element prefab
red, blue, green, yellow or gray randomly". Let's execute that one and see what
the AI Assistant makes of it. So it's already creating a field to hold my grid
element prefab, it's setting the spacing in a private variable so that's also quite
useful, and then adding everything to my grid. This looks pretty much okay, so let's accept
all of the changes that we have here, save, and go back into the Unity editor.
Now my main game object now has a link to link the grid element prefab, and
what I'm going to do is search the assets that I have in my project for a two by two
brick to use there. Let's switch into Play mode and see if we have a grid and... Yes
we have! We have a grid that is generated by code and I didn't even have to type
a line of code so that's pretty cool. I think the next thing I want to do
is add a player prefab and an enemy prefab so what I want to do is on that
grid locate a player that I can move around using the arrow keys and then have
an enemy that tries to catch that player. So what I'm going to do is simply copy
this one, make that player prefab and make this one enemy prefab, save that,
go back into Unity and just hook things up so I can visualize everything that I have
there. So for my player prefab I'm going to use a green block figure that I downloaded
as an asset - so let's go with that one, and for my enemy I will search for a red one
that I know is in there and use that one. Back into Rider... So what I want to do is add
that player at the center of the grid, so let's see if we can prompt that using AI assistant.
Now, previously I used the Alt+Enter shortcut to do that, but you can also write comments and
then Rider will try its best to infer the prompt from that comment and do something with it. What
we're going to do is say that we want to create a player from player prefab at the 3x3 position of
the grid. That should be the center. Let's see if AI Assistant can do something with that. Yes it
can and it's adding something to my grid, nice! Let's do the same thing with the Enemy. Create
an enemy from the enemy prefab at the edge of the grid. That looks like a cool prompt so let's
use that... and that also looks nice. It's placing it at position 0, 0, 0, which should be the edge -
at least one edge of the grid that we have there. Now just to keep track of the player that I want
to use later on to move it around and everything, I am going to create a new field for that. So I'm
typing some code myself and I'm going to use the same thing or going to do the same thing with the
Enemy. Now player is not yet a field but I can Alt+Enter and then create a field from that using
Rider functionality and be done with it. And I can do the same thing with the Enemy here, create
a field and keep track of the enemy in there. Allright! Just to see if this works, I'm going
going to switch back into Play mode and see if I now have a player and an enemy on the grid. And
apparently I have, so thank you AI for doing this! Cool, now back into the editor I also want
to move the camera a little bit. So now the camera was somewhere in the back of the
grid, and ideally I want to have, like, a top down view of the entire grid so
I can see what is going on in the game. So what I'm going to do is again instruct
AI Assistant to do that and say "move the camera so that it shows the entire grid from the
top". Let's go with that, we get some code there, I have no idea if this works. We'll find out
later. But let's go with this one for now. Now one thing we want to do is move the player
around, and that can be done in the Update function. So for every frame the Update function
is called in Unity, so what we're going to do here is see if the AI Assistant can help us to make
sure that the player can actually move around on the grid when we are using keyboard shortcuts.
So again, I'm going to use prompting in line here, using comments, and going to say "move player to
the grid element in front of it when the arrow up key is pressed". Now very important I also want
to make sure that we do not move out of the last grid element just to make sure that we stay
in the boundary of the grid that we created. AI assistant is thinking, it apparently
generated something already so I can just Tab and look at the code there ,and to be fair
this looks reasonably okay. We are getting the current player position if it's smaller than
four, which is the grid size that we have, zero based, then we are going to move
it around based on the key that we have. Now an interesting thing is that when
prompting from comments if you go to the next line AI Assistant may give you some hints
about other functionality that you want to add, and it detects that we're using arrow up
key down key we are in a Unity project. So apparently it inferred that we are trying to
build a game and you will see that there's now a prompt that says "move the player to the
grid element behind it when the down key is pressed and do not move out of the first grid
element" which is quite interesting. So let's Tab and let's go with that. Now hopefully AI
Assistant will also generate a left and right function... and apparently we do get a left
function and we get a right function in one go. So that's pretty interesting, let's switch
back into Unity and see if this actually works. So the first thing you will see is that the camera
is now to down so that worked, and if I press the keys we are essentially seeing the player move
around which is also quite interesting. Nice, again thank you AI Assistant. The last thing I
want to add in the game, or at least one of the last things that I want to add in the game, is
the ability for the enemy to catch up with the player that we have. So we're going to try
to catch the player so that the player will have to escape the enemy that we have there. So
what we can do there is use coroutines in Unity, and I'm going to prompt saying "start a
coroutine to move the enemy to the player". That's going to be generated using AI
Assistant which is nice except that that MoveEnemy method is nowhere to be seen.
So what I'm going to do is use Alt+Enter, create that method in there,
and just go with IEnumerator. Now an interesting thing happens: this
throw new SystemNotImplementedException line has the AI Assistant icon right next to
it, which means that I can Alt+Enter and the first thing that you see in the menu there
is that I can implement this method using AI Assistant. So let's see if that works.
You will see that AI assist opens up a chat and weill try to come up with some code
and an explanation of what that code will be doing. It gets the context of the fact that we
are in a Unity project, it gets the context of the player and the enemy being there, and
based on that it's going to do something. This looks cool, so I'm going to go with
that. I'm going to select the method that we have there and I'm just going to
click replace this one and move it in. That's pretty nice. Going to close this
window, go back into the Unity editor and see if the enemy is actually trying to
catch up with the player that we have. And yes, it's very fast apparently which
is not what we want so maybe we want to change the speed there a little bit. But
we can do that using the speed that we have here and let's uh, let's divide it
by 100s and see if that works better. We'll do that one later on. The final thing that
I want to add in the code here is to also make it so that when the enemy comes very close to the
player, essentially intersects with the player, I want to make sure that the player position
resets to the center of the grid or at least the initial starting position, so that later on
we can even add some scoring functionality etc. So I can do that in the update method, and
what I'm going to do is add another prompt and say "if the enemy is within 0.5 from
the player reset the player position". Now I know this is not very specific, but ideally AI
Assistant is going to infer that the original position was position 3x3 and it is actually
applying that one. So um, this should work. Let's go back into Unity, go back into play
mode and see if this all works or not. So the enemy is now moving, it is indeed slower,
I can still move my player, the enemy is changing direction and when the enemy catches
up with the player, which is going to happen, yes, the player position resets to where uh
it originally started. So I would say our game is right now it is complete and we can
ship it and probably expand on it and so on. Now if you go back into Rider you
will see that there's other AI functionality that you can invoke if you
go with the Alt+Enter menu, AI actions, you will also see that there's the explain code
functionality. This one is super interesting because maybe you don't remember what you
did or you want to have a quick summary of what you have been doing while you
were generating the code for this game. So what you can do is explain code. This
will open the AI Assistant chat again and you can just see what has been going on
and the AIAssistant is going to try and explain what you have been doing. It's quite
good actually, so you will see that entire explanation. What's also really interesting
is that I think for not knowing Unity, not knowing scripting, etc. that in the
past couple of minutes we managed to build a small game in Unity with Rider and
the AI Assistant, and that's pretty cool. I would say I don't think the code that we wrote
here will win any awards and I think experienced Unity developers will probably be angry and
frown at me for setting up the entire scene in code instead of in the editor, but I do think
being being able to explore and learn with the help of the AI Assistant is quite interesting and
quite helpful. So I would say give this all a try! Links to the relevant resources are in the video
description below and thank you for watching!