Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by TheYumasi · Mar 03, 2016 at 10:37 AM · cameranetworkingclient

[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 ?

Comment
Add comment · Show 1
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 Phaz0r18 · Jun 17, 2016 at 03:16 PM 0
Share

Any updates with this @TheYumasi? I'm trying to do something similar and co$$anonymous$$g up short...

5 Replies

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

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.

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 Phaz0r18 · Jun 17, 2016 at 05:12 PM 0
Share

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?

avatar image Salim · Jun 17, 2016 at 07:19 PM 0
Share

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

avatar image washington711 · Jul 23, 2016 at 09:15 PM 0
Share

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

avatar image
1

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.

Comment
Add comment · 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
0

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.

Comment
Add comment · 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
0

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!

Comment
Add comment · Show 1 · 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 macattackbok · Aug 25, 2020 at 02:35 AM 0
Share

Where did you write this in the script?

avatar image
0

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;
             }
       . . .
         }
     }
 

Comment
Add comment · Show 2 · 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 macattackbok · Aug 25, 2020 at 02:34 AM 0
Share

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.

avatar image AIRyndon · Aug 25, 2020 at 02:50 AM 0
Share

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

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

83 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

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


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