[UNet] How to make a client-side camera ?
Hi !
I'm currently making a multiplayer third-person shooter game and I can't find how to make each client having its own camera.
I tried making the camera a part of the player prefab, but when a second client is connected it ends up with one the two players seeing the other one camera. Is there a way to make the server unaware of the cameras and make each client manage their own ?
Any updates with this @TheYumasi? I'm trying to do something similar and co$$anonymous$$g up short...
Answer by TheYumasi · Jun 17, 2016 at 05:03 PM
I figured it out, it works with a few lines in the Update() function of the camera script. (WARNING : This is overkill but it works)
Here is how it works. I added a camera as a child of the player prefab, so that each player has its own camera. Then I have modified the Update() function of the camera script (which is a component of the camera) to have it disabling other players' camera. Here is the code :
void Update()
{
if (!this.transform.parent.GetComponent<CharacterControllerMulti>().isLocalPlayer)
{
gameObject.GetComponent<Camera>().enabled = false;
gameObject.GetComponent<AudioListener>().enabled = false;
}
GetInput();
OrbitTarget();
ZoomInOnTarget();
}
The "CharacterControllerMulti" component is the player script, which is a component of the player prefab.
It might also work by replacing the lines in the if with (I haven't tested it):
gameObject.SetActive(false);
(Feel free to correct me if I've made any english mistakes, I'm not a native speaker)
EDIT: @Phaz0r18 I put those lines in the Update() function because the Start() function is executed only when a new client is joining the game. If you put it in the Start() function, the new camera from the new client won't be disabled on the other clients, and it will end up by having them share the new camera again.
I admit doing it in the Update() function is a bit overkill, and there might be a way to do it only when a new client is joining. I was short on time (deadline) when I coded it (and it was 2 AM), so I couldn't afford spending more time trying to do it in a clean way.
Hey! Thanks for the speedy response, you're English is perfect ;) I'm doing something similar but I'm having issues getting Android and UNet to play nice locally. $$anonymous$$y prefab and code is set up the same way at the moment but I'm only disabling other cameras in the Start() of a client joining the server, is it necessary to disable all other cameras on EVERY update for EACH client?
Good solution, By the way you don't have to call it on every update, you can use OnStartClient(); it's called on every version of the object when a new client joins: http://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.OnStartClient.html
Noob here, How do you edit the the camera script in the first place? I know it's the component but I dont see a way to open in the script editor
Answer by eovento · Mar 24, 2018 at 06:30 PM
Thanks @ TheYumasi! There are hundreds of people absolutely lost in something that's actually simple. And some people try to explain a long theory of network without ever getting there.
So, for new comers to the thread, the ultimate solution is to just attach a CameraController script to the child camera that goes exactly like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (!this.transform.parent.GetComponent<PlayerController>().isLocalPlayer)
{
gameObject.GetComponent<Camera>().enabled = false;
gameObject.GetComponent<AudioListener>().enabled = false;
}
}
}
And it will simply work. Having in mind that PlayerController is the name of the script on my player prefab. If you named it differently, just change it. Thank you again for this one-and-only thread that actually gets to the point.
Answer by Dark_Alpha · Sep 07, 2018 at 07:43 AM
This does seem to work okey, but on more then two players the third, fourth and so on seem to again share a camera with the second player. Only the first has a completely unique camera now. Is there a sollution to this problem?
Sorry for reacting on an old thread, but this is the first semi working sollution i have found yet.
Answer by graham30 · Dec 06, 2019 at 05:54 PM
Thanks a mil guys! I struggled with this for 4 f*ckin days, before finally finding this post!! You are right about the long winded explanations, I just get lost in the translation, and the answer is so simple when you see it in front of you lol What cured it for me was the line " gameObject.GetComponent().enabled = false; " which I wrote into my player movement script. Thanks again!
Answer by AIRyndon · Aug 18, 2020 at 03:34 PM
Thanks Everyone!
I also just started with Unity. Using Mirror ( since UNet is gone now), the same thing works. but as @Salim pointed out, it is better to call it on OnStartClient on your class that inherits NetworkBehavior, and most likely this gameObject also has NetworkIdentity in Unity. Code below is the PlayerMovement class
public class PlayerMovement : NetworkBehaviour
{
. . .
public override void OnStartClient()
{
Cursor.lockState = CursorLockMode.Locked;
if (!isLocalPlayer)
{
//I also disabled this component so it also doesn't move the other player
enabled = false;
var camera = transform.Find("Main Camera");
camera.GetComponent<Camera>().enabled = false;
camera.GetComponent<AudioListener>().enabled = false;
}
. . .
}
}
It still didn't work. I'm having the same problem they are all having. I host and start a server, have someone join, and then the first person sees through the second persons camera. Plz tell me there is a fix, I don't want to scrap another project.
I have a different class for camera movement and player movement. The main idea is if the client is not the local player, disable the camera. You can look for the camera in code ( like how I did transform.Find). How your playerObject is setup might be different, on $$anonymous$$e, the camera is a child of the player object. Which is why transform.Find worked
Your answer
Follow this Question
Related Questions
Have each player of a multiplayer game have their own camera? (client-side camera) 1 Answer
How to correctly change scene with a Lobby Manager? 0 Answers
ServerDisconnected due to error: Timeout 1 Answer
multiplayer game -how to connect each game-object to specific network player 0 Answers
Networking - Host can spawn objects but client cant 0 Answers