Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 pheromoth · Apr 05, 2020 at 05:23 PM · networkinginstantiatephotoncontrolspun

Photon Unity Networking - Player Camera Instantiate

Hello, I'm trying to integrate PUN 2 in a working game. It is basically an arena where each player can control a ball; it should be a mobile game but for now I'm testing it as a desktop build. The ball is working correctly in single player mode.

Using PUN documentation and examples, I've made a login panel to go into a room (main scene). If I do it with 1 player, everything works fine: I login, create a room, join it and can control the player inside the main scene. If I log 2 players, from 2 different game instances, the player controls will be swapped (player 1 will be controlling player 2 and vice-versa).

In the lobby, I use this to get into the main scene (called "Test"):

 public void OnStartGameButtonClicked()
         {
             PhotonNetwork.CurrentRoom.IsOpen = false;
             PhotonNetwork.CurrentRoom.IsVisible = false;
             PhotonNetwork.LoadLevel("Test");
         }

Inside the main scene, I have a GameManager object with a GameManager script inside it (following PUN standards). In the GameManager script, I run this method to instantiate players:

 private void StartGame()
         {
                 GameObject player = PhotonNetwork.Instantiate("OnlinePlayerPrefab", SpawnPoints[0].position, Quaternion.identity, 0);
                 GameObject camera = player.transform.Find("Main Camera").gameObject as GameObject;
                 CameraController camController = camera.GetComponent("CameraController") as CameraController;
                 camController.target = player;
 
                 Player playerMainScript = player.GetComponent("Player") as Player;
                 GameObject playerUI = Instantiate(playerMainScript.playerUIPrefab);
                 playerMainScript.variableJoystick = GameObject.Find(playerUI.name).GetComponentInChildren<VariableJoystick>();
                 playerUI.SendMessage("SetTarget", playerMainScript, SendMessageOptions.RequireReceiver);
             }
         }

It basically instantiates a player prefab; then it finds the camera (that is a child object of the player prefab) and then gets the "CameraController" scripts inside it and sets the camera target to the player prefab that has just been instantiated. It then instantiate a "Player" (the script for the player movement and several commands), and the instantiates an UI that is defined in the Player script. The player UI contains buttons for movement of the player, so we set the instantiated ball as its target.

Here is the code of the Player script, if it can be useful to understand: https://pastebin.com/p95MMq9J And here is the PlayerUI script: https://pastebin.com/jA5ivq51

Here is the Player prefab structure and the main scene hierarchy:

alt text

You can see that I have a (non networked) canvas in the scene; each player will put its PlayerUI prefab in there; could this be causing the swapping issue?

What I've tried so far: Instead of having the Camera as a child of the player prefab, I tried (in the GameManager script) to use PhotonNetwork.Instantiate to create a camera; this isn't working, as when I try to log in, no camera is found.

Really can't get this problem solved in any way. Really appreciate any solution or tips to start from :) Thank you!

prefabandscene.png (175.7 kB)
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

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

200 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 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 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

How do I instantiate and set the parent of a Gameobject using Photon? 0 Answers

Photon Networking Instantiating Problem 0 Answers

Player Won't Instantiate with Photon Unity Network [PUN] 0 Answers

OnConnectedToMaster() vs IsConnectedAndReady? 1 Answer

script always instantiates monster even when its not supposed to? 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