Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by UFO_505 · Jun 27, 2015 at 10:49 AM · c#photonreadstream

Photon Unity - Only receiving one value.

 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
 {
     if (stream.isWriting)
     {
         if(playerNumber != 1)
         {
             stream.SendNext(Input.GetAxisRaw("Vertical"));
         }
         if(playerNumber == 1)
         {
             GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");

             foreach(GameObject player in listOfPlayers)
             {
                 stream.SendNext((Vector2)player.transform.position);
             }
         }
     }
     else
     {
         vertAxis = (float)stream.ReceiveNext();


         if(playerNumber != 1)
         {
             GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");

             if(listOfPlayers.Length != 0)
             {
                 foreach(GameObject player in listOfPlayers)
                 {
                     player.transform.position = (Vector2)stream.ReceiveNext();
                 }
             }
         }

     }
 }

If the code is like this then the values for player.transform.position won't be received. But if I remove:

             vertAxis = (float)stream.ReceiveNext();

Then the values for player.transform.position will be received.

Can anyone explain what's going on or come up with a fix? Thanks.

Comment
Add comment · Show 3
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 fuego_see_money · Jul 01, 2015 at 05:55 PM 0
Share

Do you have a PhotonView component attached to every player?

avatar image UFO_505 · Jul 01, 2015 at 10:02 PM 0
Share

Thanks for the comment!

Yes I definitely do have a PhotonViem on every player. If I didn't I don't think I would be able to send even one value.

avatar image fuego_see_money · Jul 02, 2015 at 02:30 AM 0
Share

In general, using for loops in OnPhotonSerializeView is not a good idea, because you can create functionality via networking so that you don't have to send data in a n^2 format. This will optimize your performance results greatly.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fuego_see_money · Jul 02, 2015 at 02:28 AM

Okay awesome. I don't know what script that you posted is attached to, but what I would do is, attach this script to every single player:

 public class NetworkCharacter : Photon.MonoBehaviour 
 {
     Vector3 realPosition;
 
     void Update () 
     {
         if (photonView.isMine) 
         {
             //do nothing
         } 
         else 
         {
             //lerping position
             transform.position = Vector3.Lerp (transform.position, realPosition, .15f); 
         }
     }
         
     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         if (stream.isWriting) 
         {
             stream.SendNext(transform.position);
                 } 
                 else 
                 {
             realPosition = (Vector3)stream.ReceiveNext ();
         }
     }
 }

If you attach this to every player, than what this will do is have each player keep track of each other's positions. Dont use your other script anymore.

Let me know if you have questions.

-Will

Comment
Add comment · 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
0

Answer by UFO_505 · Jul 08, 2015 at 09:01 AM

Thanks for your help, Will.

I don't quite see the problem with for statements because from my testing I haven't found them causing any issues.

Anyway, thank you for the code you provided. However it doesn't quite do what I want because I want to send all players input to one 'host' player to create a kind of authoritative server.

I have finally managed to find a solution though:

     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         if (stream.isWriting)
         {
             if(photonView.isMine && playerNumber != 1)
             {
                 stream.SendNext(Input.GetAxisRaw("Vertical"));
             }

             if(photonView.isMine && playerNumber == 1)
             {
                 GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");
 
                 foreach(GameObject player in listOfPlayers)
                 {
                     stream.SendNext(player.transform.position);
                 }
             }
         }
         else
         {
 
             GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");
 
             for(int i=0; i < listOfPlayers.Length; i++)
             {
                 if(listOfPlayers[i].GetPhotonView().isMine && listOfPlayers[i].GetComponent<Player>().playerNumber == 1)
                 {
                     vertAxis = (float)stream.ReceiveNext();
                 }
             }
 
             for(int i=0; i < listOfPlayers.Length; i++)
             {
                 if(listOfPlayers[i].GetPhotonView().isMine && listOfPlayers[i].GetComponent<Player>().playerNumber != 1)//listOfPlayers.Length != 0)
                 {
                     foreach(GameObject player in listOfPlayers)
                     {
                         player.transform.position = (Vector3)stream.ReceiveNext();
                     }
                 }
             }
         }
     }
Comment
Add comment · 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
0

Answer by tobiass · Jul 10, 2015 at 09:31 AM

I think your code clashes with the idea that each individual PhotonView is controlled by it's owner. This makes sure each player can affect it's own objects and then send the new position/state to the others.

Your code looks like you run the script on all clients but only have one client send updates for every player object. That can't work. How do you get the other client's input?

Take a look at the Marco Polo Tutorial. It shows how to send and receive values like you try to.

If that doesn't apply to your case, you need to post what you want to achieve.

Comment
Add comment · Show 4 · 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 UFO_505 · Jul 17, 2015 at 09:40 PM 0
Share

basically what im trying to do is create a kind of authoritative server without actually having the server execute any code (photon servers can't).

one player acts as a kind of server

every player sends their input to that one player and then that one player runs the game and tells all the other player's what is happening

i have managed to get this to work to an extent - the players can all move and rotate their characters and they're all in sync because one player is telling everyone else what to do

im about to start work on the shooting mechanic which will probably work in a similar way.

do you see any serious problem with what im trying to do? i realise that there will be about double the lag of what i would otherwise have. however the game is quite simple so i am hoping that each client will be able to send updates more often than is usual

avatar image fuego_see_money · Jul 18, 2015 at 12:28 AM 0
Share

Yes, there is a huge problem with what you are doing. The problem is, Photon is not meant for doing that. At all. Photon is made so that it works well with unity, including object oriented framework.

This is why you should do what I posted earlier, because that will have each object tell everyone where it is. Photon Unity Networking already has a server - this is how PhotonNetwork.Instantiate is used. It talks to the main server and tells everyone to spawn something on their local computers.

You should see this video - it explains how Photon works. It will help you greatly. Watch the whole thing, you wont regret it.

avatar image UFO_505 · Jul 18, 2015 at 07:35 PM 0
Share

I understand what you are saying and I realise that what I am doing is not normal. However, the game is working well so far and it has a great advantage over the usual model that you are suggesting because everybody's game is always completely in sync.

avatar image fuego_see_money · Jul 19, 2015 at 04:16 AM 0
Share

You should listen to the people you asked on this forum - both of whom told you not to do it the way you are doing it. One of whom (not me, Tobias) is a lead engineer for Photon. If you continue to program your game in this way, which is fine by me, just note two things:

1) You are not using Photon the way it's meant to be used, when its relatively easy to switch over to how it should be used now. If you wait until too long, you might find that it's impossible to implement things into your game that you need.

2) If you have no intentions of listening to the people who answer your questions, why bother asking the question at all? I understand you think the way you are doing it will work out, but this forum is not just meant for individuals getting answers to their questions, but it's also a permanent reference for googlers across the world after the questions have been answered. I've used answers to past questions on this site countless times, as I'm sure you have too. This question will simply point other users who have similar problems to the wrong way of approaching it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Photon Networking Spawn player 2 0 Answers

Sync game objects across network Photon C# 1 Answer

Photon synchronization 1 Answer

Unable to network remove object 0 Answers

Photon OnPhotonSerializeView - sending a custom struct - "not serializable" 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