This tutorial series introduces Unity's Netcode for GameObjects, focusing on setting up a basic multiplayer environment. It guides intermediate Unity developers through project configuration, essential Netcode components, scene management, and establishing a host-client connection for two players.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
[Music]
before we start this video a large thank
you to Keith Max Aaron Scott tenacian a
name I unfortunately cannot pronounce
thank you for supporting my friend and
Cody for their support on patreon I hope
you guys enjoy the video hello everybody
I just want to say this before we start
if you are an absolute beginner this
series is probably not the series for
you you are more than welcome to follow
along but just know I'm not going to go
over basic things like variable types
what a void is what a co-routine is you
are expected to be an intermediate or at
least have a basic understanding of the
variable types and functionality within
Unity with that being said you can
follow along you just might struggle you
have to stop a lot and look up some
things as we're typing I would also like
to State my goal for the series which of
course is to help you become more
knowledgeable in unity and with netcode
like yourself I am learning things all
the time too so if you do things
differently than me that's awesome I
would love to hear how you do it with
that being said let's begin hello
2021.3.1.of1 if you want to use the same
bill that is fine I'm using a urp empty
template and I'm going to begin by
adjusting my project window because it
is brand new I'm going to add a tab for
the project I'm going to drag it here to
the right I'm going to lock both these
tabs so that if I click one it doesn't
influence the other
I'm going to drag my console just up so
I can always see it so errors are always
displayed and warning messages and we
are good to go so
I'm just going to adjust the sizings
here and this will be the window I'm
using for the remainder of the series
now let's go and begin by going to the
package manager and we're going to
search for netcode but if you typed in
right away You're Gonna notice you're
probably not going to get anything and
that is because we're not looking in the
right place so click on packages in
project and change that to Unity
registry now if you go to the search box
in the top right again and type in
netcode you will see netcode for game
objects at the time this video is
1.0.2 so I'm just going to click install
and I'm going to give that a moment to
install that may take a minute but it
shouldn't take too long honestly the
next thing we're going to grab is a tool
called Peril sync you don't need it but
I strongly recommend it it allows you to
clone your project so we're going to go
and add a package from a git URL I will
paste the URL in the description of this
video and just click add if you're
unfamiliar with using git there's plenty
of tutorials and resources online it's
pretty straightforward again you don't
need this but it makes your life
infinitely easier you can just
essentially open your project twice
instead of doing a build every single
time to test mode players so for a
series like this it's it's just a staple
honestly now if you have that installed
like me you're going to see Peril sync
at the top of your project here you can
basically open a window called the
Clones manager and we're not going to
worry about that right now but we have
it there and we are going to use it in
this video so for now let's create an
empty game object we're going to call
this game object the network manager and
we're going to use a script from netcode
for game objects called the network
manager just think of this like the
thing that takes in all the information
for the network between you and other
players okay that's all you need to know
right now about it so the only thing you
need to change on this is the transport
we're going to use the unity transport
in the future we're going to use a thing
called face punch for steam but that is
a while away and you're going to see
there's a few options here there's start
host start server and start clients will
a host acts as a server and a client
meaning a player and the server itself a
server will be something if you had
something like in the cloud that just
processes the information and a client
is just a game client that connects to a
server so it's let's make a new folder
and call this art and let's make another
new folder inside this and call this
models and I'm going to drag in I will
link this in the description uh my old
friends my low poly man that I use for
all of my prototyping and testing so
again this will be in the description if
you don't have a model but if you have
your own player model use that I'm going
to drag it into the scene here now and
I'm going to drag it back into a folder
but first let's make a folder for our
prefabs so I'm going to create a new
folder called data and then a folder
inside of that called prefabs so I want
to keep organization very important
right from the beginning so let's open
or rather drag in our player I'm going
to rename them first a player and drag
them into the prefabs holder and now we
have a prefab of our player and if we
edit the prefab in the project the
prefab gets edited anywhere it is in the
scene so in a network manager let's drag
in the player prefab into the player
prefab variable and this just basically
creates or uses this prefab for any
player that joins a game so let's add a
component to the player here and let's
add a component called Network object
you're going to be adding this to
basically every character in your
project that is going to have some
network data attract so all players and
probably all of your AI I'm then going
to right click and create an image it's
going to Auto create a canvas for me and
I'm going to use this as the title
screen so let's quickly make a simple
title screen menu I'm going to uncheck
raycast Target on your image that's
important because if you make images
that aren't being tracked by Mouse
clicks for UI buttons for example you
don't need to have that tick to just
wastes memory so untick that I'm then
going to choose uh scale with screen
size on my canvas scalar object I'm
going to say 1920 by 1080. so that's
going to change the size of my canvas
you see it's quite big go to your image
click on this button up here hold alt
and click the bottom right Little X here
or the Blue Cross rather and that will
expand it and stretch the image to the
size of your canvas which again is 1920
by 1080. all right that looks pretty
good I'm going to rename a couple things
here now so I'm going to rename image to
title screen background
and if we go into the Elden ring menu
here you can see we have this press
start button or press any button rather
so when we click this some things happen
so let's just press it and see you can
see here it's checking your saved data
and then it checks for our network
connection and it essentially starts our
Network so we're gonna do the exact same
thing here now in our Unity project
we're going to start our Network after
we press this button first we got to
make this button obviously so let's I'm
going to rename my canvas to uh title
screen canvas and I'm going to go over
here now under the title screen
background create a new UI image I'm
going to call this Banner
and you can just make this text if you
want it's just the banner of the game
like on Elden ring this is Elden ring
and giant letters I'm going to make this
whatever size I feel is necessary but
this isn't too important obviously this
is a stylistic preference again if
you're if you're using an image like me
you want to untick raycast Target
because you don't need to click this
image so it doesn't need to be tracked
as a raycast Target if you have a lot of
images in your UI that have that box
unticked it can add up for performance
long term so go to the image change it
to a Sprite 2D and UI change it to a
single and click Alpha is transparency
if you just drag in some text like me
from Photoshop again you can just use
text in unity this is just a style
choice and it's not important in the
slightest so now that I have that up
there tutorial series Elden ring in
unity I'm going to click this Anchor
Point on the top and that means if my
canvas scales at all this image should
stay towards the top of the screen then
I'm going to go into the UI or the
background rather and I'm going to
create a button okay so so this button
is going to be our start to gain button
or rather press start as I'm going to
call it
I'm just going to call that press start
button I'm going to drag that down and
if you want to you can anchor it to the
bottom of the screen because it's kind
of near the bottom so click this icon
and then hit this bottom anchor icon and
I'm going to change a few things about
it so the text I want this to say press
start or if you want you can say press
any button whatever you're going to set
it up like I'm going to change the text
to White and then I am going to change
the button a little bit so the normal
color is the color of the button defaulty
defaulty
and then we have the selected color
which is what we want that is the color
of the button that it turns to when it
is selected so the button is going to
Auto Select when we start the game we're
going to set that up so that's all
you're going to see basically in the
future we can animate it and have it
pulse but that's not important right now
let's not get lost in the Polish so I'm
going to go to the normal button color
and change it to something a bit darker
you'll never see this so it doesn't
really matter to be honest anyway but I
am going to set it up in case we do
somehow and I'm then going to after I'm
happy with that I'm going to basically
go down to the selected color and change
it to I'm just going to use a red to
indicate that it is indeed highlighted
okay now I have no text as you can see
it's not appearing for me so this is
because you're probably gonna get this
window called text mesh Pro importer
just click import text mesh Pro
Essentials this happens the first time
you set up a text in unity project if
it's using text mesh Pro and then your
text should appear like me and you
should be fine okay so now it just is
pressed start that's fine that's all I
need let's zoom out a little so we can
get our bearings and let's go to the
title screen canvas and make our first
script I'm going to call mine title
screen manager and this is going to
handle all functionality that is just
basically fired or used at the title
screen like starting a new game loading
your save files we'll keep it all on the
script to keep it nice and neat I'm
going to create my namespace SG you
don't need a namespace I like using one
mine is that she use whatever you want
I'm going to make a public void start
Network as host
okay so what are we gonna do well we
need to reference our network manager
but we can't do that without using the
netcode library so up here let's just
say using
unity.netcode make sure you don't say
Unity engine.network is not the same so
we're going to say network manager dot Singleton
Singleton
which allows us to reference our network
manager script in the scene because
there's only one unique in time and I'm
going to say dot start host and this
just starts the network as a host so
every time we hit the start menu we want
to start as a host that's our goal so we
actually call this function now from a
button which we're going to do I'm going
to go down to our press start button
that we've just made and we can actually
give this button some logic when we
press it so I'm going to untick raycast
Target because I'm only using a
controller but you should keep that
ticked if you want to be able to use a
mouse and keyboard so I'm going to come
down here now on the button and click on
click add a new event I'm going to drag
in the same game object that contains
our new script our title screen manager
I'm going to go down and click title
screen manager and then click our new
function that we just created so start
game as host alright perfect now we
click that it will fire this function
and we are referencing the network
manager which is the thing we just made
in our scene I'll go back and show you
so it's this thing right here
and then we are starting as a host so it
just basically presses this button for
us and runs the logic okay so just a
quick overview again so again the host
is kind of like the server and a client
at the same time it is both a player and
the server Authority a client is just a
game client connecting to another host
or a server I'm sure a lot of you know
plenty about that right now but if
you're just starting with the net code
don't worry about it you'll learn more
as we go so you should have an event
system game object now in your scene
that should have popped in after you
made your canvas let's drag in our press
start button in the first selected
variable this makes us when you start
your game and automatically selects the
press start button which is good if
you're using a controller next let's go
to our title screen canvas and duplicate
our press start button let's disable the
original and let's rename this duplicate
to something like new game or start new
game so we're going to use this button
basically to start a game and put our
player into the world
so let's go down now into the text and
change that to new game or again
whatever you want to call it as long as
it's clear this is going to start us a
new game and I am going to go over now
to the original that we disabled and add
a new on click event what is that event
well we want to First disable this
button so we can't click it again so
let's say gameobject.setactive and
untick the box now we could drag in this
new button and enable it but in the
future we're going to have a menu of
buttons like load game new game etc etc
so let's create an empty game object
under title screen background and I'm
just going to call this title screen
main menu
and then after I'm done that I'm going
to click on this icon and hold alt and
click this against Blue T So expand it
the size of my canvas I'm going to drag
in my new game button so it is a child
of this game object now instead of
enabling my new game button I'm going to
enable this game object which will show
everything that is also a child of it if
it is enabled tick the Box Fort is
enabled then add one more on click event
drag in your button go to button dot
select again that is a new game button
so when you press the start game button
it will automatically select the new
game button for you disable your main
menu and re-enable your press start
button and then press play now press the
start button you should see your network
does start and your player prefab that
you dragged in earlier spawns into the
scene and note if you have steam open
like me steam makes your controller
inputs act kind of weird in unity I just
press the A input and it pressed it
twice for me so close steam if you have
it open more on that later
so I'm going to save as as in save the
scene I'm going to call this scene scene
underscore main underscore menu
underscore 01.
after I have saved the scene I'm going
to delete the sample scene which is a
scene that you just start with with this
project and then I am going to create a
new scene for our world so basically I'm
going to separate this project into two
scenes the menu scene and the world
scene which will have our whole entire
world inside of it it's what we changed
to when we start our game so I'm going
to call the world scene scene underscore
World underscore 01. now if I open that
there's nothing here obviously because
we just made it we also have to add it
to our scene's build index so if I go to file
file
and then you go to build you'll see here
there is only the CM use tab which is
the sample scene which is now gone so
let's add our main menu first open that
up and then go to build settings and
click add open scenes you can delete the
old one make sure you add the main menu
first so it's your first scene then go
to your world scene go to build settings
again and add open scenes now you'll see
there is scene 0 and scene one which is
our main menu and our world respectively
okay so let's go to the network manager
now and I'm going to change that to
World network manager I'm just a
stickler for names and I'm going to drag
that to the bottom of the hierarchy here
I'm going to make a new game object I'm
going to call this world save game
manager and I'm going to add a script to
this I'm going to call it World save
game manager this is going to handle
loading into the world loading save
files starting a new game all that good
stuff okay so I'm going to delete the
start and update function I'm going to
make my namespace which is SG for
Sebastian Graves and then we're going to
begin to write some logic in here but
first let's say using Unity engine Dot
scene management this allows us to use
some functionality related scenes like
loading a new scene okay so let's make a
public static World save game manager
variable I'm going to call mine instance
this is going to be a Singleton so if
you want you can call it a Singleton
instead I just like using the keyword
instance I don't know why it's
preference that's all so basically this
allows the script to be accessed from
anywhere in the project at any time and
there's only one in the project at any
given time now you might want to use a
getter and Setter so you can't
accidentally override that but I'm not
going to do that I'm working alone so
I'm just going to keep it as a variable
on awake we want to check to see if this
variable has been filled so if instance
is equal to null then instance is equal
to this if instance is not null for some
reason it means there's two of these in
the scene at one time there should never
be so we're just going to say destroy
game object okay
and let's just make a comment here there
can only be one of these World save game
managers in the scene at one time so one
script we want to access this one and if
one happens to exist already this is a
mistake destroy the game object and thus
destroying the script so next let's make
a start function and let's say don't
destroy unload and we're going to use
this game object meaning this stays with
us through every scene that we load into
the world or the main menu
all right so next let's make a public
eye enumerator which is just a
co-routine basically and we're going to
call this load new game
and then we're going to load the scene
asynchronistically so in the future we
can use a loading screen and it can keep
track of the progress so I'm going to
make an async operation variable I'm
going to call it load operation I'm
going to equal it to scene manager dot
load scene asynchronistically you're
going to see that's going to require a
build index number that just means the
scene number okay so we can make a
variable for this so it changes we can
just change it in the inspector I'm
going to make a serializable field so it
is visible the inspector you can make a
variable of type int I'm going to call
this world scene index which means the
index number of our world scene we know
it's equal to one so I'm going to make
it one then I'm going to pass the world
scene index in this function and just to
show you I minimize the script go to
file go to build settings you can see
our world scene is number one so you
want that number to be equal to that of
Your World scene now since this is a
co-routine I'm going to say yields
return null so it stops giving us an
error and we need to call this function now
now
so let's go over to our world or rather
our title screen manager and right below
start Network as host let's make a new
void public void so we can access it
from the buttons let's say start new
game and like our network manager up
here we can access our world save game
manager because it is a Singleton so
let's say World save game manager and
since we called it instance and not
Singleton you might have called a
Singleton I'm going to say dot instance
and then we need to use the start Co
routine keyword here bracket and then
World save
gamemanager.instance dot load new game
and then save that okay perfect so let's
go to our new game button and you know
we've made that by duplicating our start
game button so let's get rid of the on
click event that it already has you can
see here our press start button is a few
if we go to our new game button it has
one we don't want that so let's just
clear it and make a new on click event
and like before we're going to drag in
the game object that contains our title
screen manager which for me is the title
screen canvas we're going to find our
title screen manager and I'm just going
to look for a start new game now if we
go and press play
we can go into the game here and you can
see pressing start actually spawns our
character and starts our network but if
we start the new game here our player
model does not survive the scene change
so we need to make it so when our player
model spawns when the network starts
he survives the scenes changing so go to
our player the prefab let's add a
component to him called the character
manager now across the series The
Character manager will just basically be
the brains of controlling all character
functionality and any scripts that have
to do with the character so let's make
our namespace as is per tradition minus
SG and then inside here we're not going
to do anything fancy right now we're
just going to make an awake method and
we're going to say don't destroy unload
and pass this game object this being the
character save that and we are good to
go he should now survive the scene
change so let's create a new empty game
object I'm just going to call this
player UI manager in the future this
will house all of our UI logic and will
be the hub for interacting between our
UI parts now I'm going to reset the
transform and add a new script or create
a new script rather I'm going to call
player UI manager I'm going to create
and open that up I'm then going to
delete the start and update
functionality I'm going to add my
namespace as is pro tradition minus SG
yours is whatever you you want it to be
and let's write some very simple logic
that allows us to test our Network so
we're going to delete this in the future
this is just for debugging but we're
going to keep it for a long time because
this allows us to quickly test I'm going
to make a header call it Network join
I'm going to make a serializable field
going to call this or make it up type
bull rather I'm going to call it start
game as client okay so when we start the
game uh when we hit the start button in
the menu it starts it as a host in order
to join as a client we need to disable
the network and run it again as a client
so I'm going to make an update function
here I'm going to say if start game as
client we're going to say start game as
client SQL false so we reset it
instantly I'm then going to say Network
and manager so for that I need to say
using unity.net Code make sure you don't
accidentally type in unity
engine.networking it is not the same so
using unity.netcode then I'm going to
say Network manager.singleton and we
want to do is shut down the network so
we're going to say Network
manager.singleton dot shutdown that's it
so I'm going to make a comment here so
it's very clear we're going to say we
must first shut down the network as a
host to start it as a client so because
we want to connect as a client okay so
right below that then we can reference
the network manager Singleton again but
I'm going to make a comment first be
super clear so after we shut it down I'm
going to say we then start the game or
start the network or restart the network
as a client so Network manager.singleton
dot start as client I believe start
client there we go and let's save that so
so
okay like our world save game manager
and our network manager we're going to
make this a Singleton because there
should only ever be one in the scene at
any given time so let's make a public
static player UI manager I'm going to
call mine instance again you should use
get and set if you're working with
teammates or anybody else so you can't
accidentally override this variable from
some other script but I'm just going to
keep mine as an open instance here on
public and awake I'm going to say if
instance is equal to null I'm going to
say instance is equal to this else
destroy this game object because there
should only ever be one in the scene at
any given time okay that looks good guys
we're almost ready to test this and
connect with another client so let's go
over to the prefabs folder I'm gonna
make a new folder I'm going to call this
world managers because we're going to
have a few of these as the series
progresses I'm going to make prefabs of
our player UI manager of our world save
game manager and of our network manager
in here and save them so if we change
the prefabs it will change in the scene
for us make some easier to edit then I'm
going to go to our clones manager I'm
going to open up a new clone if you
don't have this you can just build the
game and create a build and just run the
build I mean then say open a new editor
after I've cloned it successfully and
I'm going to drag it over the screen so
you can see it's on my second monitor
you can see it just is an identical
project you might have to change the
aspect ratio and stuff and let the
Windows settings but other than that it
is exactly the same now before you mess
around with it make sure you save your
scene in the main project so then it
will just basically carry over the
changes into this new project you never
want to edit the other project always
your main project never the Clone so
next I'll make a start function on the
player UI manager I'm going to say don't
destroy unload so it carries over into
the new scene and then if I press play
here and start the game on my main
monitor I'm going to start the game go
to new game you can see yep Iron Man
here survives the transition there he is
right there now if I drag the window
over from my second project and I start
it we're going to try to connect so I'm
going to hit play
and then when I get into the game view
here I'm just going to press start it's
going to load us into the new scene
after hit new game then let's go to our
player UI manager and click Start game
as client it's going to give you this
warning message saying you must stop the
server first just hit it again it's
because the server didn't stop when it
tried to start as a client when we hit
it you're going to see there are two
players in both of these scenes we have
just successfully connected to our host
from our client player we have
successfully established a network
connection between two players alright
guys that's gonna do it for the first
episode if you made it this far please
be sure to drop a like and leave a
comment it genuinely helps out the
series so so much as always a very
special thank you my patreons it's
because of you guys I get to keep making
this kind of content and I love making
this kind of content I'm very excited to
start the next episode where we're going
to cover some Locomotion we're going to
get into inputs and we're going to see
how much time we got and what we can
cover but we're going to start building
our basic controller very very excited
for that so I will see you guys in the
next one [Music]
[Music]
thank you [Music]
[Music]
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.