Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 KingKongFu · Dec 13, 2012 at 05:30 PM · cameranetworking

Camera switches player in network game

Ok so what is happening is my camera is switching between players so the other player looks though my screen/camera and i look though his camera.

how i set it up is that one player is the batmobile and the other is a race car. they are both prefabs with its own camera attached to it and movement script.

when I start the server on my end before anyone connects then it works fine im behind the batmobile. But the second he joins the game i switch behind the race car and his camera is behind the batmobile.

The movement script still works fine I control the batmobile and he controls the race car but the cameras are screwy.

Thanks for any help with this Im stumped about this.

here is the network scripts Thanks again for any help

 var playerPrefab : GameObject;
 var batPrefab : GameObject;
 var sp1 : Transform;
 var sp2: Transform;
 
 private var hostData: HostData[];
 private var refreshing : boolean;
 
 var bX : float;
 var bY : float;
 var bW : float;
 var bH : float;
 
 function Start()
 {
     bX = Screen.width * 0.1;
     bY = Screen.width * 0.1;
     bW = Screen.width * 0.1;
     bH = Screen.width * 0.1;
 }
 function Update()
 {
     if(refreshing)
     {
         if(MasterServer.PollHostList().Length > 0)
         {
             refreshing = false;
             Debug.Log(MasterServer.PollHostList().Length);
             hostData = MasterServer.PollHostList();
             Debug.Log("Host data lenght ;" + hostData.Length);
         }
     }
 }
 
 
 function startServer()
 {
     Network.InitializeServer(32,25000,!Network.MovePublicAddress);
     MasterServer.RegisterHost(gameName, "Batman Driver game", "This is a test");
 }
 function OnServerInitialized()
 {
     Debug.Log("Server initilized");
     spawnPlayer();
 }
 function OnMasterServerEvent(mse:MasterServerEvent)
 {
     if(mse == MasterServerEvent.RegistrationSucceeded)
     {
         Debug.Log("Server Registered");
     }
 }
 function refreshHostList()
 {
     Debug.Log("Refreshing");
     MasterServer.RequestHostList(gameName);
     refreshing = true;
 }
 
 function OnConnectedToServer()
 {
     spawnPlayer();
 }
 
 function spawnPlayer()
 {
     if(Network.isClient)
     {
         Network.Instantiate(playerPrefab, sp2.position, Quaternion.identity, 0);
     }
     if(Network.isServer)
     {
         Network.Instantiate(batPrefab, sp1.position, Quaternion.identity, 0);
     }
     
 }
 
 function OnGUI()
 {
     if(!Network.isClient && !Network.isServer)
     {
         if(GUI.Button(Rect(bX, bY, bW, bH), "Start Server"))
         {
             Debug.Log("Server Start");
             startServer();
         }
         if(GUI.Button(Rect(bX, bY * 1.2 + bH, bW, bH), "Refreash Host"))
         {
             refreshHostList();
         }    
         //this works
         //GUI.Button(Rect(bX * 1.5  + bW, bY * 1.2  + (bH * 0), bW *3 , bH * 0.5), "Game test");
         
         if(hostData)//if statement works
         {
             var hdLenght = hostData.Length;
             
             //GUI.Button(Rect(bX * 1.5  + bW, bY * 1.2  + (bH * 0), bW *3 , bH * 0.5), "Game test");\
             
                 for(var i: int = 0; i < hdLenght; i++)
                 {
                     Debug.Log("Game name :" + hostData[0].gameName);
                     
                     if(GUI.Button(Rect(bX * 1.5  + bW, bY * 1.2  + (bH * i), bW *3 , bH * 0.5), hostData[i].gameName))
                     {
                         Debug.Log("Connecting to game");
                         Network.Connect(hostData[i]);
                     }
                     
                 
                 }
                
            
         }
     }
 }
 
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

1 Reply

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

Answer by Jake.OConnor · Dec 13, 2012 at 07:34 PM

Well nothing in the code you posted has anything to do with a camera, but I am led to believe that your cameras are probably children of the vehicle prefabs, which would mean that when a new vehicle is instantiated, there are two cameras in the scene rendering to the same screen space. Remove the cameras from the vehicle prefabs and instead have a single camera in the scene that targets and follows the local player.

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 KingKongFu · Dec 14, 2012 at 02:55 PM 0
Share

Yes that's right each prefab has its own camera. I wanted to do that because i have to place the camera in a certain position on the model. It is very hard to do that in code

The camera is used like a fps camera so its from the players perspective.

Any suggestions?

avatar image Jake.OConnor · Dec 14, 2012 at 04:17 PM 0
Share

Pretty easy. $$anonymous$$ake a new dummy GameObject as a child of each model, then place them at the same position as the cameras. Delete the cameras and just leave one in the scene. Once a vehicle is instantiated for the local player, have the main camera's position and rotation be set to that of the dummy object and parent the camera to the vehicle so that it follows it.

avatar image KingKongFu · Dec 14, 2012 at 04:21 PM 0
Share

brilliant I was hitting my head against the wall to figure out how to get the can attached to the model thank a lot!

avatar image Jake.OConnor · Dec 14, 2012 at 04:24 PM 0
Share

No problem. I had difficulties like these when I was making my multiplayer game a few months back, so I learned the hard way.

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

10 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

Related Questions

How do I make a camera look for a specific GameObject while networking? 1 Answer

How to make camera position relative to a specific target. 1 Answer

How to apply target, while created at runtime? 1 Answer

camera only sticks to the master client 0 Answers

Follow camera jitters on multiplayer clients 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