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 ATCuriosity · Jun 22, 2015 at 08:53 PM · multiplayerphotonsynchronization

[PhotonNetwork] How can I stock a allocateViewID from a player and reuse it later with another player ?

Hi everybody,

I'm trying to create a multiplayers game for fun. But I have a little problem with OnPhotonSerializeView() and/or the viewID (i don't know the real cause of my issue).

In short, when you join a room, you are not in the game yet. There is a list of players split in 2 teams. Here is the problem :

  • if the "master" creates the room and begins to play, and another player joins the game later, only the first one sees the other moves.

  • but if the 2 players enter the game together, they see each other moving... Strange...

Now, how i tried to do that : in the room, if you are the master, you start the game by opening the map and sending a rpc to the ready players to join.

When a player joins the game, he sends a buffered rpc in order to :

  • spawn in the map for each player in game

  • add his name in a list of players in game that all the players hold (and when a player joins the others, he knows who he has to spawn in his map)

so, the code to spawn (in short) followed by the script to synchronize the movement :

 public void SpawnOnNetwork ()
 {
     photonView.RPC("NewPlayer", PhotonTargets.AllBuffered, PhotonNetwork.player, PhotonNetwork.AllocateViewID());
 }

 [RPC] void NewPlayer (PhotonPlayer p, int id)
 {
     // If i'm inGame => spawn the player
     if ((State)PhotonNetwork.player.customProperties["s"] == State.inGame)
         StartCoroutine(SpawnPlayer(p, id));

     Add (p, id);  // List<Player> avec Player = (PhotonPlayer , int)
 }
 
 IEnumerator SpawnPlayer (PhotonPlayer p, int id)
 {
     Transform newPlayer = Instantiate(player, Vector3.zero, player.transform.rotation) as Transform;
     newPlayer.GetComponent<PhotonView>().viewID = id;

     if (p.isLocal) {
         newPlayer.name = "MyPlayer";
         newPlayer.position = new Vector3 (Random.Range(0.5f,1.5f), 1.5f, Random.Range(0.5f,1.5f));
         ActivePlayer (newPlayer); // set scripts to control the player
     }
     else {
         newPlayer.name = "OtherPlayer";
         yield return StartCoroutine(AskPosition(p)); // get the position of the player
         Player_Controller pc = newPlayer.GetComponent<Player_Controller>() as Player_Controller;
         pc.startPos = lastOtherPlayerPos;
         pc.endPos = lastOtherPlayerPos;
         lastOtherPlayerPos = Vector3.zero;
     }
 }

 IEnumerator AskPosition (PhotonPlayer p)
 {
     photonView.RPC("Give", p, PhotonNetwork.player);
     while (lastOtherPlayerPos.Equals(Vector3.zero))
         yield return null;
 }

 [RPC] void Give (PhotonPlayer p)
 {
     photonView.RPC("Receive", p, GameObject.Find("MyPlayer").transform.position);
 }

 [RPC] void Receive (Vector3 position)
 {
     lastOtherPlayerPos = position;
 }

 public void SpawnPlayers ()
 {
     foreach (Player p in playersInGame)
         StartCoroutine(SpawnPlayer (p.p, p.id));
 }

public class Player_Controller : Photon.MonoBehaviour {

 ...
 public Vector3 startPos;
 public Vector3 endPos;
 ...

 void Update ()
 {
     if (photonView.isMine)
     {
         // control of the player
     }
     else
     {
         SyncedMovement();
     }
 }

 void SyncedMovement()
 {
     ...
     syncTime += Time.deltaTime;
     transform.position = Vector3.Lerp(startPos, endPos, syncTime / syncDelay);
     transform.rotation = Quaternion.Lerp(startRot, endRot, syncTime / syncDelay);
 }

 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
 {
     if (stream.isWriting)
     {
         stream.SendNext(transform.position);
         stream.SendNext(transform.rotation);
         ...
     }
     else
     {
         endPos = (Vector3)stream.ReceiveNext();
         startPos = transform.position;
         endRot = (Quaternion)stream.ReceiveNext();
         startRot = transform.rotation;
         ...
         
         syncTime = 0f;
         syncDelay = Time.time - lastSyncTime;
         lastSyncTime = Time.time;
     }
 }

}

last time, i had a alert in unity console about it : "Received OnSerialization for view ID 1001. We have no such PhotonView!" but i don't know where it comes from...

if you have a solution, or a better idea to do sth similar, i'm interested !!!
Thanks a lot for your time !!!
Antoine
ps: sorry for any language mistakes... ;)

Comment
Add comment · Show 1
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 godofwarfare115 · Feb 10, 2016 at 12:46 PM 0
Share

Just wondering if anyone has a fix for this issue? I am having the same problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ATCuriosity · Jun 24, 2015 at 10:26 AM

Actually, I'm pretty sure the issue comes from the viewID and the AllocateViewID...
However, i don't understand why if the both players spawn at the same time, it works.. But, if the id is stocked in the list and used when the player joins the game later, it doesn't work anymore :/

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Photon Networking - What function(s) are called when a player (not me) is spawned? 2 Answers

how to synchronize other player actions in multiplayer environment using photon multiplayer ? 3 Answers

multiplayer variable synchronization problem 1 Answer

Syncing particles 1 Answer

Multiplayer client and server aren't talking to each other properly after changing scenes 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