Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by nansy_g21 · Jan 03, 2016 at 07:21 PM · c#multiplayer

Know which player acts in multiplayer

Hello

I am developing a 2 player game using Unity Networking (players are on different pc)

Player 1 is both host and local player

Player 2 is just a client

What I am trying to achieve is

  • When P1 hits the button on screen the gameObject spawns on the left and moves to the right

  • When P2 hits the button on screen the gameObject spawns on the right and moves to the left

Is there a way to know the player who hits the button everytime? And spawn the object from the right place?

(It is not turn based a player can hit more than one times the button before the opponent)

Thank you

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by toddisarockstar · Jan 04, 2016 at 08:25 PM

networking projects are a bit more complex. when a button is pushed, that player's machine would need to send an "RPC call" to the other players. the RPC would contain info like a button number and a player number and maybe a location numbers.

you would setup a receiving section of code in your script to grab the incoming RPC info from the other machines and they would each spawn and do whatever based on the info they receive.

here is a simple RPC example:

 var myplayernumber:int;
 
 function Update () {
 if(Input.GetKeyDown("space")){
 //----------------------------send---------------
 networkView.RPC ("buttonpush", RPCMode.AllBuffered,1,myplayernumber,transform.position);}
 }
 
 //----------------------recieve---------------
 @RPC
 function buttonpush (info1:int,info2:int,info3:Vector3){
 
 print("All machines know that player"+info2+" pushed a button at this location "+info3);
 }



 


there is also Network.Instantiate and state synchronization options if you are looking to update locations on a frame by frame basis. but one way or another, anytime you are spawning stuff on a multiplayer game you should have a way of login stuff into arrays for future reference between machines.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nansy_g21 · Jan 05, 2016 at 03:53 PM 0
Share

Thank you so much It was exaclty this logic I needed.

One more question, when you say "future references between machines" you mean like if I want to destroy the game object I spawned? Will this array be useful then?

As far as the Network.Instantiate I found some tutorials that suggested to use Network Transform component on the game object I want to keep in sync. This works for me so far. I will keep your way in $$anonymous$$d if any problems emerge in synchronization.

avatar image toddisarockstar · Jan 07, 2016 at 05:19 AM 0
Share

Yeah, It totally depends on how complex your project is. The most important philosophy to understand getting into $$anonymous$$ultiplayer development is that even a smaller dual core CPU can think hundreds of times faster than data sent over a fast internet connection. So really most the information between computers is done mostly manually on a "need to know basis" for proficiency and any calculation is done on the local machine.

to answer your next question, thats just what i was talking about! if each spawned player is organized in a list or Array then yes, it would be very easy to Destroy something with a couple small ints with a RPC call. Better example might be using the arrays to do cool stuff like checking enemy distances, or updating rotations/animations based on position without the need to clog up precious bandwidth. actually, if you are at the step of moving characters now, you are going to find out in the next step that unity does not even offer a synchronization feature for animations! (but its for good reason).

you will eventually need arrays or similar databases to loop through for reference. when your character is spawned he might might also need a bazooka and a few grenades. which more importantly REQUIRE arrays to prevent need of synchronizing of those too!

Remember, anytime you sync an object, unity begins sending sets of 3 large float numbers approximately 60 times a second through our precious bandwidth for each object synced, which is a nice happy tool when necessary for our player characters.

for example... in most if games if a grenade is thrown, you don't use unity's sync features. A simple rpc is given only two times, once rpc signal for direction to give the others something to look at, and maybe a second RPC to update what the players machine said he blown up.

if you had a grenade cooking, each players machine would have the grenade position in the array changing, but only the player who owned it would give the RPC to start movment and blow it up. Assumption techics like this is why large commercial games like battlefield or call of duty can do so many players.

I noticed here, when you start asking multiplayer questions you dont get many answers. maybe because it is because there is a lot more opinions on different ways to do things depending on what you want to do.

anyways, what is your game setup? what are you trying to achieve in the end? I could provide code examples if i knew more. Its important to look at the "end picture" to set up any game from the beginning.

avatar image nansy_g21 · Jan 07, 2016 at 07:00 PM 0
Share

Ok I thought I had replied but can't see my reply (obviously I canceled). So let me start over again...

First of all thank you so much for your suggestions I will follow them and change the logic around communication in my project.

The project is a base deffence multiplayer game I am developing for my thesis in university. It is not too complex I think, but I am a begginer in networking so I am not familiar with this way of thinking.

It is 1 vs 1 game. One player creates a room and is host and local player at the same time and another can join the room.

There are two bases on the left and on the right one for every player. And there are buttons on screen for the players to hit and create their units (these buttons were my problem I had to detect which player hit the button to spaw the unit from his base and move it towards the opponents base).

Every unit starts from my base and moves until it collides with an enemy unit or my opponents base. If my unit collides with an enemy they fight until one is dead (every unit type has upgradable attack and defence stats). If a unit collides with the opponents base my opponent lose some of his life. When the player life is zero he loses the game.

I have my mechanics working (moment, animations, gold and mana changes etc) but as I said I have issues in communication. I tried to keep track of the player id via a static variable but this didn't work correctly. Now I am going to use your way with RPC calls. I will try this today (hopefully) and I will post my results and some of my code if needed.

Once again thank you for your time.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiplayer - Error when rejoining room 1 Answer

Online client code execution 0 Answers

Multiplayer pass int from lobby to game scene 1 Answer

Match making in Photon 0 Answers

Spawning objects uNet Client and Local 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges