hello. im trying to make a lan network game and i have big problems with my camera. i now about unity scripting to singleplay. but all that network and networkplayer and network.player. all that new functions. i have no idea how to start.. i have read alot about it. but i dont se the point in anything:S total lost. any ideas how to start. ?? my first problem is my camera. i got players connectet. but thay use the same camera..
Do you mean you cant see your other players?And they cant see you.Im not quite understanding your problem exactly.
i can se the other players. but they use the same camera. i just put the camera under my player with no scripts on. my clients and my servers use the same camera..
hehe the problem is that.. both players use the same camera.. they need a camera each to follow.. just like a ect. world of warcraft. so each player got thire own camera. Sry for my bad bad english btw. and ty for fast reply.
The easiest way to handle setting the camera in a Unity networked multiplayer game, in my experience, is to only have one camera in the scene, and have whatever follow script you have on it have no target. Then, when a player's prefab is instantiated, use networkView.IsMine to check if the current player is the owner of that player prefab, and then set the camera target to the player prefab. If you are using Network.Instantiate to spawn player prefabs when new players connect, you would put something like this on your player prefab (for the sake of example, I'm pretending you're using the SmoothFollow script on your camera):[ Code (csharp): OnNetworkInstantiate(info : NetworkMessageInfo){ if(networkView.IsMine){ //if I am the owner of this prefab Camera.main.GetComponent(SmoothFollow).target = transform; } }
what does this have to do with a camera ? Just as Legend says, attach a script to the camera with a target and set the target at spawning.
Hi, I used this in the smooth follow script. It works fine at the server side, but at the client side, the camera does follow th car but it doesnt rotate as per the rotation of the car. What is wrong here?
You should try using this: Code (csharp): function OnNetworkInstantiate (info : NetworkMessageInfo) { if (networkView.isMine) { //if I am the owner of this prefab GameObject.Find("Camera").GetComponent(SmoothFollow).target = transform; } } It'll directly find the camera on both sides of the server and It'll assign the transform. If you camera is not named "Camera", dont forget to change the object name to look for in the GameObject.Find Hope that helps
Create a new script; Code (csharp): void Update(){ if(networkView.isMine){ camera.gameObject.enabled = true; } else{ camera.gameObject.enabled = false; } Save it, apply the script to your Camera that's in your prefab. You have to make sure that there can only be one camera active when you play multiplayer games, otherwise Unity 3D will be confused with several other camera in the scene too. P.s. Be sure to have another script that toggles audiolistener too! It slows down your performance if you have more than one!
I'm trying to use this in my game but I'm getting a NullReferenceException error, are you sure this code works? if so how do I avoid this issue?
As I said in my last reply, you must change the name in the GameObjet.Find field to the name of the respective camera, but I just realized that using the Find isn't the best idea. Use this: Code (JavaScript): function OnNetworkInstantiate (info : NetworkMessageInfo) { if (networkView.isMine) { //if I am the owner of this prefab Camera.main.GetComponent(SmoothFollow).target = transform; } } If this doesn't work, please give the full error so we can help fully.
I think the error is coming from " = transform" I'm woefully beginner, still having issues with this one. My camera is named "Camera", and I am using the SmoothFollow script aswell. Here is the error: NullReferenceException: Object reference not set to an instance of an object Player_mover.OnNetworkInstantiate (NetworkMessageInfo info) (at Assets/Player_mover.js:87) UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32) NetworkManagerScript:spawnPlayer() (at Assets/NetworkManagerScript.js:48) NetworkManagerScript:OnServerInitialized() (at Assets/NetworkManagerScript.js:56) UnityEngine.Network:InitializeServer(Int32, Int32, Boolean) NetworkManagerScript:startServer() (at Assets/NetworkManagerScript.js:28) NetworkManagerScript:OnGUI() (at Assets/NetworkManagerScript.js:74) And I would like to thank you sincerely for replying so fast and helping me out. I've been pulling my hair out over this one cause your code looks like it should work perfectly. Edit: I put the code in my player game object (thats just where it made sense to me) I just tossed it in his movement script but I've also tried putting it in it's own script to no avail. edit edit: I set up my multiplayer through this tutorial, I am running a master server aswell
Okay, new edit to the code. Code (JavaScript): var player : Transform; function OnNetworkInstantiate (info : NetworkMessageInfo) { if (player == null) { // quit the function if the player var is null Debug.LogError("Player variable is null. Please define variable in Inspector"); return; } if (networkView.isMine) { // if I am the owner of this prefab Camera.main.GetComponent(SmoothFollow).target = player; } } This new code should definitely work no matter where the script is, but make sure to define the player variable in the inspector.
Still getting: NullReferenceException: Object reference not set to an instance of an object Player_mover.OnNetworkInstantiate (NetworkMessageInfo info) (at Assets/Player_mover.js:71) UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32) NetworkManagerScript:spawnPlayer() (at Assets/NetworkManagerScript.js:48) NetworkManagerScript:OnServerInitialized() (at Assets/NetworkManagerScript.js:56) UnityEngine.Network:InitializeServer(Int32, Int32, Boolean) NetworkManagerScript:startServer() (at Assets/NetworkManagerScript.js:28) I see it says something about the mt network manager script in my error, does that have anything to do with it? My network script is as follows: var playerPrefab:GameObject; var spawnObject:Transform; var gameName:String = "TestGame"; private var refreshing:boolean; private var hostData:HostData[]; private var btnX:float; private var btnY:float; private var btnW:float; private var btnH:float; function Start(){ btnX=Screen.width *0.05; btnY=Screen.width *0.05; btnW=Screen.width *0.1; btnH=Screen.width *0.1; } function startServer(){ Network.InitializeServer(32,25001,!Network.HavePublicAddress); MasterServer.RegisterHost(gameName,"TestGame","This is a test"); } function refreshHostList(){ MasterServer.RequestHostList(gameName); refreshing = true; } function Update(){ if (refreshing){ if(MasterServer.PollHostList().Length > 0){ refreshing = false; Debug.Log(MasterServer.PollHostList().Length); hostData = MasterServer.PollHostList(); } } } function spawnPlayer(){ Network.Instantiate(playerPrefab,spawnObject.position, Quaternion.identity, 0); } //Messages function OnServerInitialized(){ Debug.Log("Server initialized!"); spawnPlayer(); } function OnConnectedToServer(){ spawnPlayer(); } function OnMasterServerEvent(mse:MasterServerEvent){ if(mse == MasterServerEvent.RegistrationSucceeded){ Debug.Log("Registered Server"); } } // GUI function OnGUI(){ if(!Network.isClient && !Network.isServer){ if(GUI.Button(Rect(btnX, btnY, btnW, btnH), "Start Server")){ Debug.Log("Starting Server"); startServer(); } if(GUI.Button(Rect(btnX, btnY * 1.2+ btnH, btnW, btnH), "Refresh Hosts")){ Debug.Log("Refreshing"); refreshHostList(); } if(hostData){ for(var i:int=0; i<hostData.length; i++){ if(GUI.Button(Rect(btnX*1.5+btnW,btnY*1.2+(btnH*i),btnW*3,btnH*0.5),hostData.gameName)) { Network.Connect(hostData); }} }} } Sorry for having so much trouble with this
Could you please put your code into the code inserter thingy? When writing your reply, look for the toolbar tab that says insert and click it, then click code, and paste your code in there. It makes it easier to read.
I resolved my issue. In case anyone else is having this same issue, what I did was I just bypassed setting up a 3rd person camera all together, I noticed that most of the tutorials I've watched have been using 1st person cameras, so I just made a camera the child of the prefab, with a code that disables the camera if it's not owned by that particular client, and set the coordinates for the camera to be behind the player. I loose the advantage of a smooth follow, but I'm going to be adding camera controls to zoom the camera in and out and pivot it around, so it might end up working out better for me. Thanks for your help with all this.
would you mind me asking about the code that you used for this? I'm working on some similar stuff and facing the same problem.