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 /
avatar image
0
Question by GiViZed · Oct 24, 2016 at 10:03 PM · c#unity5photon

Instantiating scene objects in Photon Multiplayer.

This code works correctly only for master client, but for other player objects spawn in the air in the wrong place, also, players see different instanciated objects in their games and do not see objects of each other. (instanciated objects have photon view with transform attached to it).

  void Update ()
 {    
     // if time to spawn a new game object
     if (Time.time  >= nextSpawnTime) {
         // Spawn the game object through function below
         GetComponent<PhotonView>().RPC("MakeThingToSpawn", PhotonTargets.All);
         // determine the next time to spawn the object
         nextSpawnTime = Time.time+secondsBetweenSpawning;
     }    
 }
 [PunRPC]
 void MakeThingToSpawn ()
 {
     Vector3 spawnPosition;
     Vector3 spawnPosition2;

     // get a random position between the specified ranges
     spawnPosition.x = Random.Range (xMinRange, xMaxRange);
     spawnPosition.y = Random.Range (yMinRange, yMaxRange);
     spawnPosition.z = Random.Range (zMinRange, zMaxRange);
     spawnPosition2.x = Random.Range (xMinRange, xMaxRange);
     spawnPosition2.y = Random.Range (yMinRange, yMaxRange);
     spawnPosition2.z = Random.Range (zMinRange2, zMaxRange2);        

     GameObject spawnedObject = GameObject.Instantiate(spawnObjects, spawnPosition, transform.rotation) as GameObject;
         GameObject spawnedObject2 = GameObject.Instantiate(spawnObjects, spawnPosition2, transform.rotation) as GameObject;        
     // make the parent the spawner so hierarchy doesn't get super messy
         spawnedObject.transform.parent = gameObject.transform;
         spawnedObject2.transform.parent = gameObject.transform;

 }
Comment
Add comment · Show 12
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 AlucardJay · Oct 25, 2016 at 12:25 AM 0
Share

You are telling each client to choose a random position. Also this will be running on every client.

The master should be the only one running the timer, selecting a random position, then sending that position to all clients which then instantiate the object.

avatar image GiViZed AlucardJay · Oct 26, 2016 at 03:41 PM 0
Share

Can you tell me how to send these parameters to other players please?

avatar image AlucardJay AlucardJay · Oct 26, 2016 at 07:25 PM 1
Share
 void Update()
 {
     if ( PhotonNetwork.is$$anonymous$$asterClient ) 
     {
         // if time to spawn a new game object
         if ( Time.time > nextSpawnTime ) 
         {
             // get a random position between the specified ranges
             Vector3 spawnPosition;
             Vector3 spawnPosition2;
             spawnPosition.x = Random.Range (x$$anonymous$$inRange, x$$anonymous$$axRange);
             spawnPosition.y = Random.Range (y$$anonymous$$inRange, y$$anonymous$$axRange);
             spawnPosition.z = Random.Range (z$$anonymous$$inRange, z$$anonymous$$axRange);
             spawnPosition2.x = Random.Range (x$$anonymous$$inRange, x$$anonymous$$axRange);
             spawnPosition2.y = Random.Range (y$$anonymous$$inRange, y$$anonymous$$axRange);
             spawnPosition2.z = Random.Range (z$$anonymous$$inRange2, z$$anonymous$$axRange2); 
 
             // Spawn the game object through function below
             GetComponent<PhotonView>().RPC( "$$anonymous$$akeThingToSpawn", PhotonTargets.All, spawnPosition, spawnPosition2 );
 
             // deter$$anonymous$$e the next time to spawn the object
             nextSpawnTime = Time.time + secondsBetweenSpawning; 
         }      
     }
 }
 
 [PunRPC]
 void $$anonymous$$akeThingToSpawn( Vector3 spawnPosition, Vector3 spawnPosition2 )
 {       
     GameObject spawnedObject = GameObject.Instantiate(spawnObjects, spawnPosition, transform.rotation) as GameObject;
     GameObject spawnedObject2 = GameObject.Instantiate(spawnObjects, spawnPosition2, transform.rotation) as GameObject;        
 
     // make the parent the spawner so hierarchy doesn't get super messy
     spawnedObject.transform.parent = gameObject.transform;
     spawnedObject2.transform.parent = gameObject.transform;
 }
avatar image GiViZed AlucardJay · Oct 27, 2016 at 11:07 AM 0
Share

Thanks a lot for your answer, now objects spawn in the same place, but for non-master player they are stuck in one place, and don`t have gravity applied to them.

Show more comments
avatar image GiViZed AlucardJay · Oct 27, 2016 at 04:13 PM 0
Share

I changed GameObject.Instantiate to PhotonNetwork.Instantiate, in Alucard`s code. Now 2 objects are being created near each other, and one of them i can controll with another player, and it is also visible for all players. I feel like i`m getting closer to solution. Thanks for your help

avatar image GiViZed GiViZed · Oct 27, 2016 at 05:57 PM 0
Share

I also tried my old code with PhotonNetwork.Instantiate but it has the same problem - 2 objects are created, one of them is moving for one player, other - for second player. They are both visible. Do you know why is that? It seems to me, that gameObject should be instantiated only once, because we check if the player is master client and then call the function $$anonymous$$akeThingToSpawn().

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

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

Related Questions

How to request access for actions in PUN multiplayer? 0 Answers

How to make game objects move for all players in photon multiplayer? 0 Answers

Destroying entire parent object on one client but on the other destroying just the child. 1 Answer

How to make script work for all players Unity Photon? 1 Answer

Unity Collision Doesn't Work when Two Players Instantiated 1 Answer


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