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 Rosehardt · May 12, 2013 at 11:52 AM · cameramultiplayerplayernetworkviewismine

Camera attached to the wrong player

Hi, the title says it all. I'm working on a online multiplayer game. When I start a server everything is fine, but when I connect as client a new prefab is made and controlable by the client. But the camera snaps to the wrong player. I tried it the camera.enabled=true; in the awake but that isn't working.

Does anyone have any idea how to fix it?

Here's my code:

 #pragma strict
 #pragma implicit
 #pragma downcast
 
 public var playerPrefab : Transform;
 
 
 function OnServerInitialized(){
     Spawnplayer();
 }
 
 function OnConnectedToServer(){
     Spawnplayer();
 
             
             
 }
 
 function Spawnplayer(){
 
     var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
             if(networkView.isMine){
             camera.enabled = true;
             }
 }
 
 
 
 
 function OnPlayerDisconnected(player: NetworkPlayer) {
     Debug.Log("Clean up after player " + player);
     Network.RemoveRPCs(player);
     Network.DestroyPlayerObjects(player);
 }
 
 function OnDisconnectedFromServer(info : NetworkDisconnection) {
     Debug.Log("Clean up a bit after server quit");
     Network.RemoveRPCs(Network.player);
     Network.DestroyPlayerObjects(Network.player);
     
     /* 
     * Note that we only remove our own objects, but we cannot remove the other players 
     * objects since we don't know what they are; we didn't keep track of them. 
     * In a game you would usually reload the level or load the main menu level anyway ;).
     * 
     * In fact, we could use "Application.LoadLevel(Application.loadedLevel);" here instead to reset the scene.
     */
     Application.LoadLevel(Application.loadedLevel);
 }
Comment
Add comment · Show 6
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 ByteSheep · May 12, 2013 at 11:56 AM 0
Share

Are the cameras of the prefabs disabled by default? If not, then you will want to change your code to:

 if(!networkView.is$$anonymous$$ine){
   camera.enabled = false;
 }

This will disable all the cameras that do not belong to the current player.

avatar image Rosehardt · May 12, 2013 at 12:04 PM 0
Share

Thanks for replying! Where should I put this? I tried it under the
var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0); but it didn't work

avatar image ByteSheep · May 12, 2013 at 12:11 PM 0
Share

Yes you would replace line 22-24 with the code above.
Got a couple questions though:

  • Are the cameras on the player prefab you are instantiating enabled by default?

  • What is 'camera' referencing? - It should be referencing the camera attached to the prefab that this script is being executed on

avatar image Rosehardt · May 12, 2013 at 12:21 PM 0
Share

You were right, I hadn't attatached the prefab. I have now, code looks like this now. But the problem is still there. Did I miss anything? Thanks for your help I have it running on www.adoets.nl if you want to take a look

 public var playerPrefab : Transform;
 public var cameraPrefab : Camera;
 
 function OnServerInitialized(){
     Spawnplayer();
 }
 
 function OnConnectedToServer(){
     Spawnplayer();
 
             
             
 }
 
 function Spawnplayer(){
 
     var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
 
 if(!networkView.is$$anonymous$$ine){
 cameraPrefab.enabled=false;
 }
 }
 
 
 
 
 function OnPlayerDisconnected(player: NetworkPlayer) {
     Debug.Log("Clean up after player " + player);
     Network.RemoveRPCs(player);
     Network.DestroyPlayerObjects(player);
 }
 
 function OnDisconnectedFromServer(info : NetworkDisconnection) {
     Debug.Log("Clean up a bit after server quit");
     Network.RemoveRPCs(Network.player);
     Network.DestroyPlayerObjects(Network.player);
     
 
     Application.LoadLevel(Application.loadedLevel);
 }
avatar image ByteSheep · May 12, 2013 at 12:54 PM 0
Share

You would want to add a script to your player prefab that checks which camera should be enabled - something like:

 var cam : GameObject;
 
 function Awake(){
 
     if(!networkView.is$$anonymous$$ine)
     {
         //We aren't the network owner
         cam.SetActive(false);
     }
 }

Of course make sure the prefab you add this script to has a network view.. You could also add this script directly to the camera, which would save having to reference it. I'm afraid networking is a large topic - I would suggest going over how network view components work and how to use them in your code.

Show more comments

1 Reply

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

Answer by ByteSheep · May 12, 2013 at 06:02 PM

Ok, I decided to make a quick example project showing you how the basic setup works - here it is.


multiplayer_camera.zip (328.5 kB)
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 Rosehardt · May 12, 2013 at 07:01 PM 0
Share

THAN$$anonymous$$ YOU! Oh my god, I could kiss you. It works now!

avatar image ByteSheep · May 12, 2013 at 07:04 PM 0
Share

If this has answered your question then please click the green tick beside the answer to mark it as accepted. Cheers!

avatar image Rosehardt · May 12, 2013 at 07:06 PM 0
Share

Done, again, thank you so much!

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

16 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

Related Questions

Cinemachine Input Provider requires InputActionReference. Any workarounds? 0 Answers

c# set main camera to player (multiplayer) 1 Answer

camera doesnt follow player after I added networkIdentiy and networkTransform to the player 0 Answers

Multiplayer| Attaching camera to player (if i am the owner 1 Answer

How can I get "!networkView.isMine" to function properly? 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