Photon Fusion 2 Unity Multiplayer Basic Tutorial for Beginners | EP.06 Collecting Input & Ticks Use

Nested Mango
24 Feb 202412:39

Summary

TLDRIn this Unity game development tutorial by Ram Singh, viewers learn how to implement input handling in Fusion 2 with host mode. The video explains the input system's operation, the importance of host authority in updating network movements, and the creation of a 'NetworkInputData' script for local player movement. It also delves into the concept of 'tick' in multiplayer games, which is crucial for smooth gameplay, and demonstrates how to apply these concepts to move a player character across a network. The tutorial is designed to help developers understand the basics before exploring advanced features.

Takeaways

  • 😀 Ram Singh is a Unity game developer, YouTuber, and blogger.
  • 🔄 The tutorial covers how to work with input in Fusion 2 using host mode.
  • 🎮 It's important to understand the basics before exploring advanced features in Unity.
  • 📱 The input system involves a local player creating an input structure that is sent to the host for network updates.
  • 👤 The host has the authority to update the network, while the local host handles the actual movement.
  • 🔄 Host migration is possible in Fusion 2, allowing any player to become the host if the current host disconnects.
  • 📝 A script named 'NetworkInputData' is created to structure the input data for the local player's movement.
  • 🏗️ The use of 'struct' is preferred for lightweight data over 'class' due to its simplicity and lack of inheritance capabilities.
  • 🔑 Key codes are used to capture input from the local player, such as movement with the WASD keys.
  • ⏱ The concept of 'tick' is introduced as a way to update clients or servers in a multiplayer game environment.
  • 🎲 A higher tick rate results in a smoother game experience, as it involves more frequent data exchange and state synchronization.
  • 👾 A 'NetworkBehavior' script is used for player movement instead of 'MonoBehaviour' to receive movement data from the host.
  • 🔄 The 'UpdateNetwork' method is overridden to get input from the host and perform movement on every tick.
  • 🎉 The tutorial concludes with a successful demonstration of moving a player in a multiplayer environment using Fusion 2.

Q & A

  • What is the main focus of this tutorial by Ram Singh?

    -The main focus of this tutorial is to explain how the input system works with Fusion 2 in host mode for multiplayer game development in Unity.

  • Why is it important to understand the basics before exploring advanced topics in Unity game development?

    -Understanding the basics is important because it provides a foundation that allows developers to comprehend and effectively utilize advanced features without confusion.

  • What does Ram Singh suggest for new subscribers who haven't watched previous tutorials?

    -Ram Singh suggests that new subscribers should watch the previous tutorials to understand the basic terms and concepts before exploring advanced topics.

  • How does the input system work in Fusion 2's host mode according to the tutorial?

    -In host mode, every player has the authority to get input, but only the host can update the network. The local host creates an input structure that is passed to the host, which then updates the movement on all other devices.

  • What is the role of the 'Local Host' in the context of the input system explained in the tutorial?

    -The 'Local Host' is responsible for capturing the input from the local player's controller and creating an input structure that reflects the player's movement, which is then sent to the host.

  • What is meant by 'host migration' in the context of multiplayer games?

    -Host migration refers to the ability for a different player to become the host in a multiplayer game session, allowing for the game to continue smoothly if the original host disconnects.

  • Why is it necessary to create a 'NetworkInputData' script in Unity for handling player inputs?

    -The 'NetworkInputData' script is necessary to structure the player's input data, such as movement, which is then sent to the host to be updated across the network for all players.

  • What is the difference between a 'class' and a 'struct' in Unity, as mentioned in the tutorial?

    -A 'class' can inherit interfaces and is used for more complex data types, while a 'struct' is a value type used for lightweight data and cannot inherit interfaces. It is stored on the stack and does not contain references.

  • How does the 'tick' concept relate to the smoothness of a multiplayer game?

    -The 'tick' represents a single iteration of the game loop, and the more ticks that occur in a second, the smoother the game will be, as it allows for more frequent state synchronization and updates.

  • What is the role of 'NetworkBehavior' in controlling player movement in a Unity multiplayer game?

    -The 'NetworkBehavior' is used to override the 'UpdateNetwork' method, which allows the host's input to control the player's movement on every tick, ensuring synchronized movement across the network.

  • How does the tutorial demonstrate the application of the input system to a player character in Unity?

    -The tutorial demonstrates by creating a 'NetworkInputData' script for input collection, applying 'NetworkBehavior' to the player character for movement synchronization, and explaining the importance of the 'tick' for smooth gameplay.

Outlines

00:00

🕹️ Understanding Input Handling in Fusion 2

In this paragraph, Ram Singh, a Unity game developer, introduces the topic of input handling in Unity's Fusion 2 with host mode. He explains the basic concept of how input is captured from the local player and then sent to the host to be updated across the network. The importance of understanding the basic input system before exploring advanced features is emphasized. The tutorial is aimed at those who are new to the channel and have not seen previous tutorials, as it builds upon foundational knowledge. Ram encourages viewers to subscribe and like the video to help the channel reach a wider audience. The explanation includes a diagram to illustrate the process of input from the local client to the host and then across the network, highlighting the role of the host in managing input updates.

05:01

🔄 Exploring Tick Rates and Player Movement in Multiplayer

This paragraph delves into the concept of 'tick' in multiplayer games, which is a crucial element for smooth gameplay. Ram explains that a tick represents an iteration in the game loop, during which data is exchanged between the host and clients, facilitating state synchronization. He clarifies that it is possible for multiple ticks to occur within a single frame, which can significantly impact the game's performance. The paragraph also covers the creation of a 'Network Input Data' script to handle player input and movement locally before sending it to the host for network-wide updates. Ram discusses the use of structs for lightweight data and the importance of proper coding practices, such as using curly braces to avoid errors. The tutorial progresses with an explanation of how to apply these concepts to player movement in Unity using Network Behavior and the Network Character Controller.

10:03

🎮 Implementing Player Movement with Fusion 2 Multiplayer

The final paragraph focuses on implementing player movement in a Fusion 2 multiplayer setup. Ram provides a step-by-step guide on creating a script for the player that overrides the 'UpdateNetwork' method to receive input from the host during each tick. He details the process of obtaining direction from the network input data, normalizing it, and then using it to move the character controller within Unity. The movement is synchronized with the 'Runner.doDeltaTime' to ensure it is consistent across the network. Ram demonstrates how to assign the script to the player character in Unity and encourages viewers to test the setup in a multiplayer environment. The paragraph concludes with a brief test of the player movement, showcasing the successful implementation of the input and movement system discussed in the tutorial.

Mindmap

Keywords

💡Unity Game Developer

A Unity Game Developer is a professional who uses the Unity engine to create video games. In the script, Ram Singh identifies himself as a Unity game developer, indicating his expertise in developing games using the Unity platform. This is central to the video's theme, as he is teaching viewers about Unity's multiplayer capabilities.

💡Fusion 2

Fusion 2 is a multiplayer networking library for Unity, designed to simplify the creation of networked games. The script focuses on how to use Fusion 2 to handle player input in a multiplayer environment, demonstrating its importance in the video's tutorial on setting up networked input.

💡Host Mode

In the context of multiplayer gaming, Host Mode refers to the configuration where one player's game instance acts as the authoritative server for the session. The script explains how input works in Fusion 2 with Host Mode, emphasizing the role of the host in managing and updating player movements across the network.

💡Input System

The Input System in Unity allows developers to capture and respond to user inputs such as keyboard, mouse, or controller actions. The video script delves into how the input system works with Fusion 2, showing how player inputs are captured and synchronized across a networked game environment.

💡Local Player

A Local Player refers to the player controlling the game on the device that is running the game locally. The script describes the process where the local player's inputs are used to create an input structure that is then sent to the host for network synchronization.

💡Remote Device

A Remote Device is any device other than the local player's device that is connected to the game network. The script uses the term to illustrate the concept of input being sent from the local player to be updated on the remote device via the host.

💡Network Input Data

Network Input Data is the structured information representing player inputs that are sent over the network. The script describes creating a 'NetworkInputData' script to handle player input, which is a crucial step in the process of synchronizing player movements in a multiplayer game.

💡Struct

In programming, a Struct (short for structure) is a way to organize data in a lightweight, value-type container. The script explains the use of a struct for 'NetworkInputData' to keep the data lightweight and simple, which is important for efficient network communication.

💡Tick

In the context of game development, a Tick refers to a single iteration of the game's update loop. The script explains the concept of a Tick in relation to multiplayer synchronization, indicating that each Tick involves data exchange and state updates between the host and clients.

💡State Synchronization

State Synchronization is the process of ensuring that all clients in a multiplayer game have a consistent view of the game state. The script mentions state synchronization as part of what occurs during each Tick, highlighting its importance for maintaining a smooth multiplayer experience.

💡Network Behavior

Network Behavior is a component in Unity's networking systems that allows for custom networked behavior on game objects. The script describes using Network Behavior for player movement, emphasizing its role in receiving and applying movement data from the host in a networked game.

💡Host Migration

Host Migration is the ability to transfer the host role to another player in a multiplayer game session. The script briefly touches on host migration in Vision 2 (presumably a version of Fusion), indicating that if the current host disconnects, another player can take over as the host.

Highlights

Introduction to the tutorial on how input works in Fusion 2 with host mode.

Explanation of the importance of understanding basic concepts before exploring advanced features.

Description of the input system in Fusion 2 involving two devices: a local device and a remote device.

Clarification on the role of the host in updating movements on the network for all players.

Illustration of how the local client creates an input structure that is then updated by the host.

Introduction to the concept of host migration in Vision 2, allowing any player to become the host.

The necessity of creating a 'Network Input Data' script for capturing and moving the local player.

Discussion on the difference between classes and structs in Unity, with a focus on using structs for lightweight data.

Instruction on collecting input data from the local client using Unity's Input.GetKey method.

Emphasis on the correct use of curly braces in code to avoid common programming errors.

Explanation of how to send collected input data to the host for network synchronization.

Introduction to the concept of 'tick' in multiplayer games and its role in smooth gameplay.

Description of how a higher tick rate can lead to a smoother gaming experience.

Discussion on the possibility of multiple ticks occurring within a single frame.

Explanation of the tasks performed during a tick, such as state synchronization and physics simulation.

Guidance on creating a 'Player' script using Network Behavior for movement in a multiplayer environment.

Demonstration of assigning the 'Player' script to a character in Unity for multiplayer testing.

Conclusion of the tutorial with a successful demonstration of player movement in a multiplayer setting.

Transcripts

play00:00

hello everyone welcome back on channel

play00:02

guys I'm Ram Singh unity game developer

play00:04

YouTuber and blogger in last tutorial we

play00:05

learn that how we can spawn our player

play00:08

inside the room in this tutorial we are

play00:09

going to learn how input thing work in

play00:11

the Fusion 2 with the host mode so let's

play00:13

start the today video if you're new on

play00:15

the channel make sure subscribe the

play00:16

channel if you have not watched previous

play00:17

tutorial so make sure that you have

play00:19

watched because it is a very important

play00:20

thing that you understand the basic

play00:22

after that you can just explore any

play00:24

advanced things so that uh you know the

play00:26

basic term you know the basic B to

play00:29

create the room to get the input that

play00:31

you can explore the advanced thing so

play00:34

let's start the today video supporting

play00:36

me you can just join or give me thanks

play00:38

or just if you you can't do anything so

play00:41

please like the video so that I can

play00:43

reach much more people like you in this

play00:46

tutorial first we have to understand

play00:48

that how input system work with the

play00:51

Fusion 2 so basically what happens is

play00:55

that let's suppose we have two devices

play00:58

that is my device that that is the

play01:00

Remote device and this is the me local

play01:02

player on my local device B1 is the I'm

play01:05

getting input means whatever I'm using

play01:08

my any controller and then what I'm

play01:10

doing is just I'm just moving in my

play01:13

local device okay and creating one input

play01:16

structure that thing I have to turn to

play01:18

host and then host will just update my

play01:22

movement on another another device like

play01:25

in this P1 so here you can see I'm on

play01:27

the P1 and you can you can see same

play01:30

structure uh getting on the P1 okay so

play01:33

basically in the host mode what happens

play01:34

that host means every player have the

play01:37

authority to get the input host have

play01:40

authority to update on the network but

play01:42

Local Host have authority to get to do

play01:45

the movement but it is not on the

play01:47

network basically it just provide the

play01:49

structure to to the host and host update

play01:51

on the all Network so hope you

play01:53

understand so here is my device I'm

play01:55

getting the input so my player is

play01:56

getting mve locally and I'm creating one

play01:59

structure what is my movement and that

play02:01

structure pass to the host and host

play02:03

update on the network means on on all

play02:05

other devices so this is the way to get

play02:08

the input so this was the just diagram

play02:11

hope you understand what I'm trying to

play02:12

say host and you can call this is the

play02:14

local client this is the client so these

play02:16

are the client and you also know that

play02:18

host migration also work in the Vision 2

play02:20

basically if this host out so this

play02:23

player can be the host okay so this is

play02:26

the way of host migration this is the

play02:28

way to get the okay great so let's jump

play02:31

into the unity and here we have to

play02:33

create one structure where we can take

play02:35

the input so basically we have to create

play02:37

one script that is the network input

play02:40

data so basically this will be the

play02:42

structure that will take the input and

play02:44

move the our local player and then this

play02:47

will be provided to the host that will

play02:50

update on the network you can remove

play02:52

this thing and here is the class we want

play02:55

data that is the data structure what we

play02:57

want we want the movement in movement

play02:59

what we need need we need Vector 3 where

play03:01

we want to notice XY Z Direction

play03:03

movement so for the Simplicity for the

play03:06

lightweight data we use the stru struct

play03:09

struct we cannot inherit we cannot

play03:11

implement but I Network input I am just

play03:15

inheriting so in the comment box you

play03:17

have to let me know that what is the

play03:19

difference in class and struct basically

play03:21

struck is structure so basic difference

play03:23

is that we cannot inherit any uh

play03:26

interface but we are inheriting so you

play03:28

have to also let me know that why we are

play03:30

doing this and this is the homework for

play03:32

you and the basic difference is that we

play03:33

cannot inherit and basically struct we

play03:36

use for light weight data means we want

play03:38

to few similar kind of data types so at

play03:41

that time we use the struct we have not

play03:43

to create the object and it does not

play03:46

contain the reference so that's why

play03:48

store in the stack so there are so many

play03:50

differences and in interview people ask

play03:52

so basically what we want we want our

play03:55

Vector 3 so just do the vector 3 this

play03:57

will help us get the direction this is

play03:59

is our structure of of structure of our

play04:02

input whatever we are getting and in

play04:04

locally we will move but on the network

play04:07

host will uh do the update so basically

play04:10

this will be provided to the host okay

play04:12

great you have to not worry about the

play04:14

compression basically us to compress the

play04:16

data before sending and it only send if

play04:19

it is getting any differences in the

play04:21

input okay so next step is to collect

play04:24

the input from the local input from the

play04:26

local client we have to go in the basic

play04:28

spawner because we have done so many

play04:31

call backs here so we have to search on

play04:33

input so on input what we have to do we

play04:36

have to just create where data and

play04:38

basically what we have to do new network

play04:40

input data that what user is pring so

play04:43

basically we have to use input get key

play04:45

then we have to use key code basically

play04:47

key code provide the button so for the W

play04:51

and what we want on the W basically data

play04:54

and then we have we need direction and

play04:57

plus we have to use Vector 3 do forward

play05:00

so similarly what we have to access we

play05:02

have to access w a DS so make sure you

play05:06

have just Ed the code s code A and D

play05:09

never write this kind of code because in

play05:11

most of cases what happens that you have

play05:13

not provided the curly braces so it WR

play05:16

single time uh single line line of code

play05:19

make sure someone have done this or make

play05:21

sure that someone have write any line of

play05:23

code this code will not work at that

play05:25

time you can face a problem so make sure

play05:27

that you have used every code the curly

play05:30

braces Above So alt and arrow so make

play05:32

sure that you have pass all the key WS

play05:35

for back for left D for right okay so we

play05:38

have collected all the data what we need

play05:40

so we need to send to the host so what

play05:43

we need we need Network input so here is

play05:46

the input here we have input input we

play05:48

have to use to set data so basically we

play05:50

use set and then we have to pass this

play05:54

data basically what doing is just

play05:56

passing to host so this code is getting

play05:59

collected locally and then passing to

play06:01

the host okay great now it's time to

play06:04

explore one more very important Concept

play06:06

in the fusion so it is a very important

play06:08

you have to understand and after that we

play06:10

can go further and then use one code so

play06:14

that you can understand so this is my

play06:15

device so you can call me client or you

play06:18

can call me host and here is one other

play06:20

that is the host server or you can call

play06:23

client so host and client can be anyone

play06:26

so don't worry about it and there is

play06:28

another device that is also client okay

play06:30

great so what is the tick so you have

play06:33

most probably heard in the multiplayer

play06:36

what is the tick tick player very

play06:37

important role that you have heard that

play06:39

tick value is more your game will be

play06:42

more smoother so what is the tick so

play06:44

tick is like on is a in simple way is a

play06:47

way to update the client or server or

play06:51

host so let's suppose this is my device

play06:53

and one Loop is running so let's suppose

play06:56

from here we are starting then we are

play06:58

coming here here basically Fusion what

play07:01

does is just run one Fusion just run a

play07:05

continuous loop in your application in

play07:07

one itation means whenever it complete

play07:10

one iteration that is called tick what

play07:12

calls tick means every device every

play07:15

client every host every server run

play07:17

continuous loop and in continuous loop

play07:20

one completion or one itation is called

play07:23

take and in one tick one tick what

play07:26

happens just it exchange the data

play07:28

between the server or host to client and

play07:31

then every like server also run their

play07:33

tick so in one tick means basically one

play07:36

iteration they just uh send data to

play07:40

client uh whoever is connected to the

play07:43

device it just send data and get receive

play07:45

the data data basically they perform

play07:48

State synchron basically they perform

play07:51

State synchronization physic

play07:53

synchronization input synchronization as

play07:56

well as

play07:57

computational logic synchron means in

play08:00

one in one tick they exchange the data

play08:03

and what is the one tick one execution

play08:05

or one itation of loop basically uh it

play08:08

performs always then people ask me that

play08:11

that it is possible that in one frames

play08:14

what is one frame like in 1 second how

play08:17

our our rendering is happen like our UI

play08:20

our 3D message render so basically

play08:23

people ask me that in one frame it is

play08:26

possible that multiple tick play so yeah

play08:28

it is a simple it is possible means one

play08:30

frames multiple dick can be performed

play08:33

okay and in most probably it happens

play08:35

because if it will not happen so your

play08:38

game will be very laggy the number of

play08:40

tick is higher so your game will be much

play08:42

more smoother great so hope you

play08:44

understand and in the comment box you

play08:45

have to let me know that what is the

play08:48

tick and what is the frames and in one

play08:51

frames how many tick can be perform in

play08:53

one tick what are the things happen

play08:56

basically State synchronization happen

play08:57

physics simulation or input transition

play09:00

or computational logic anything happen

play09:02

between the client server or host client

play09:05

okay great okay so now we have

play09:06

understand the tick part now it's come

play09:09

to the how we can apply on the player so

play09:11

basically we have to create one more

play09:13

script that is the player so just open

play09:14

this script remove this part and make

play09:17

sure that you have the fusion so using

play09:20

Fusion we are not going to use mono

play09:21

behavor because it is getting movement

play09:24

from the host in every tick so what we

play09:27

have to use Network Behavior so we are

play09:29

going to use Network behavior for

play09:32

movement what we need we need Network

play09:35

character controller and in awake

play09:37

basically we have to access this one so

play09:41

just do the tab and basically what we

play09:43

want so we want every tick on every TI

play09:45

we want to access the host input host

play09:49

input what we need we have to override

play09:51

update Network so basically fix update

play09:54

Network call on every tick okay means

play09:57

when game Loop complete

play09:59

one that is the tick and in every tick

play10:02

so basically we get the input and then

play10:05

we do the movement locally and then that

play10:08

structure input we send to the host and

play10:10

that host just sync my state on another

play10:13

device on network like write the logic

play10:16

and basically this is the tick so I have

play10:18

already explained to you that in one

play10:20

frame tick can be called okay so in

play10:22

every we are getting call means in one

play10:25

frame this method will be called

play10:26

multiple time basically we have to get

play10:29

we have to get the input from the host

play10:31

so basically we have to get get input so

play10:35

basically we have to out network data so

play10:38

that thing network network input data we

play10:41

have to get input from the host so out

play10:44

Network input you can call data and that

play10:47

data we have to do in the if we are

play10:50

getting get input then basically we are

play10:52

getting this structure from the network

play10:54

the data and that data we have to get

play10:58

direction so we we have to get the

play11:00

direction and then we have to normalize

play11:02

okay and after the normalize what we

play11:04

want we want movement so basically we

play11:07

have to access the character controller

play11:09

Dot and then we have to perform the

play11:11

movement so basically we use move and

play11:14

let's see we want to move 10 unit and

play11:17

then in the direction so that is the

play11:19

data Direction multiply by time do Delta

play11:22

so basically in unity we multiply by

play11:24

time. Delta but it is getting from the

play11:27

network so we have to use Runner do

play11:30

Delta time that's it so basically it

play11:32

will be called in every and I will

play11:34

explain about tick what is the tick host

play11:37

you can call host or you can call server

play11:40

here script we have to assign on the

play11:42

player so just go on the player and here

play11:44

we have to search that is the player

play11:46

okay so now you can see that we have

play11:47

assigned the player and it's showing

play11:49

like this and let's run let's see how it

play11:52

working to play and basically we have

play11:54

tested with the multiplayer So currently

play11:56

I'm doing in my local don't worry about

play11:58

it test with the the multiplayer I'm

play11:59

just joining to the host so click on the

play12:02

host now we have generated our player

play12:04

let's see it's getting movement or not

play12:06

so this is my player so here you can see

play12:08

we are able to move our player right so

play12:11

this is the beauty of fusion and we are

play12:13

able to move our player so you can just

play12:15

t on the network so it will work

play12:18

perfectly so we are able to move our

play12:20

player system is getting hacked so I'm

play12:23

running so many software right now

play12:25

that's it for today tutorial and hope

play12:26

you enjoy the video hope you learn

play12:28

something you and just run on the

play12:29

multiplayer you can check and thanks for

play12:31

watching have a good day see you in next

play12:32

video bye-bye take

play12:37

care

Rate This

5.0 / 5 (0 votes)

相关标签
Unity DevelopmentMultiplayer GamesInput SystemFusion 2Game TutorialNetwork SyncHost ModePlayer MovementYouTuberBlogger
您是否需要英文摘要?