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 Kalissar · Jun 14, 2014 at 12:52 PM · c#cameranetworkingphoton

Local (non network) camera for multiplayer game

I have followed this (paladinstudio link) tutorial about how making a small multiplaye game.

Now I want each player to control the camera better.

According to this or this answer, the best way to do it is not to create a different camera for each player, but to make the main camera follow your player in a local way.

I do think this is the best way because it limits the number of network components in the game, but there are a few things I can't work out :

  • If the camera doesn't have any network component (like a photon view), how can it find the player when he gets spawned ?

  • Since the player is a prefab, the camera cannot have a reference to a player, can it ? In a potential CameraController script I mean.

I work in C#. What scripts can I use and what do these scripts have to be attached to ?

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

2 Replies

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

Answer by Kalissar · Jun 14, 2014 at 03:51 PM

I found a way to do it, I hope it's not too ugly. The pros, as said above, is that there is only 1 camera actually instanciated so that, unlike all the other solutions I have come accross, you don't have to disable every other camera/audio source that dioesn't belong to you.

Architecture : MainCamera, with a CameraController script attached. The script has a GameObject target variable.

Only thing left is to assignate this target when the player gets instantiated :

 void OnJoinedRoom()
     {
         Debug.Log("Connected to Room");
 
         // Spawn player
         GameObject player = PhotonNetwork.Instantiate(playerPrefab.name, new Vector3 (0.5f, 5.0f, 0.5f), Quaternion.identity, 0);
         GameObject camera = GameObject.FindWithTag ("MainCamera");
         if (camera != null) 
         {
             CameraController followScript = camera.GetComponent("CameraController") as CameraController;
             if (followScript != null){
                 followScript.target = player;
             }
         }
     }


And here is the cameracontroller script, it's very simple, it was only for some test purpose :

 public class CameraController : MonoBehaviour {
 
     public GameObject target;
     private Vector3 offset;
 
     // Use this for initialization
     void Start () {
         offset = transform.position;
     }
     
     // Update is called once per frame
     void LateUpdate () {
         if (target != null){
             transform.position = target.transform.position + offset;
         }
     }
 }


Hope it will help some people

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 sebastialonso · Jan 11, 2015 at 10:07 PM 0
Share

Do I also have to place the prefab Player in the component as well? It keeps asking me to assigned the target variable first, because it's unassigned. Thank you for your answer!

EDIT: Yes, you have to place the prefab.

avatar image
0

Answer by Aerozo · Jun 14, 2014 at 03:50 PM

If you have a look in the first block of code above the "State Synchronization" title, you'll see:

 if (photonView.isMine)
             InputMovement();

Essentially if photonView.isMine is true, it means that this Player object is the one you spawned on the server and that should be your player.

So easiest way to do what you want is it to change the above it to something like this:

     if (photonView.isMine) {
       InputMovement();
       //Find camera and set the camera target to this object
     }

Hope this helps, comment if it's unclear.

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 Kalissar · Jun 14, 2014 at 05:12 PM 0
Share

I thought about this one. Problem with this is that the "find camera and set target to this" would be done every frame, which is not optimal because we need to do it only once. Of course it would work, but i'm trying to work on a clean basis and I really think this is not the way to go.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity3D Photon movement synchronization 0 Answers

[Photon] Looking for example code for late joining clients syncing instantiated game objects that don't exist in the base scene 0 Answers

How do I sync a mesh over network? (Photon) 1 Answer

This is giving an error for the other client and cannot hit other client. 1 Answer

How can I create a database with a Photon Network Server? 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