Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by rak1n · Mar 19, 2016 at 10:12 AM · cameramultiplayer

Making my camera follow player in MultiPlayer

Hi I want each player to have their own pov as all multiplayer games do, and have it follow around the player. I have a camera script tied to my camera, with a target of player object. I have a player prefab which i assign to the camera.

This is my camera script

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class CameraFollow : NetworkBehaviour {
 
     public Transform target;
     public float m_speed = 0.1f;
     Camera mycam;
 
     // Use this for initialization
     void Start () {
 
         mycam = GetComponent<Camera>();
     
     }
     
     // Update is called once per frame
     void Update () {
 
         if (!isLocalPlayer)
         {
             mycam.enabled = false;
         }
         else
         {
             mycam.enabled = true;
         }
 
         mycam.orthographicSize = (Screen.height / 100f) / 2f;
 
         if (target) {
             transform.position = Vector3.Lerp(transform.position, target.position, m_speed) + new Vector3(0, 0, -10);
         }
     
     }
 }
 

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 meat5000 ♦ · Mar 19, 2016 at 10:10 AM 0
Share

I dont believe the camera needs to be a networked object unless you intend to view or control it from another client.

If you are using the networking to only use isLocalPlayer, consider finding the camera from your player ins$$anonymous$$d of finding the player from the camera. If it is not a networked object there wont be multiple cameras.

Dont forget, you can inject information with GetComponent as well as retrieve it.

4 Replies

· Add your reply
  • Sort: 
avatar image
15

Answer by Jah0 · Nov 09, 2016 at 08:30 AM

This is how I got it workin:

Script attached to my Main Camera:

 using UnityEngine;
 
 public class CameraFollow : MonoBehaviour
 {
     public Transform playerTransform;
     public int depth = -20;
 
     // Update is called once per frame
     void Update()
     {
         if(playerTransform != null)
         {
             transform.position = playerTransform.position + new Vector3(0,10,depth);
         }
     }
 
     public void setTarget(Transform target)
     {
         playerTransform = target;
     }
 }

And I set the target in my PlayerController script:

     public override void OnStartLocalPlayer()
     {
         Camera.main.GetComponent<CameraFollow>().setTarget(gameObject.transform);
     }

Hope this helps!

Comment
Add comment · Show 8 · 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 Unshackled · Nov 15, 2016 at 02:03 AM 0
Share

Thanks UDN this worked for me and I implemented it in under 2 $$anonymous$$utes. Well Happy.

avatar image lui2k · Jan 05, 2017 at 09:22 PM 0
Share

Worked perfectly! Thanks for sharing!

avatar image sachinbirajdar · Aug 03, 2017 at 02:03 PM 0
Share

Thanks.It worked.

avatar image zergnick · Sep 18, 2017 at 02:43 AM 0
Share

Thanks indeed. I have been trying different ways but which didn't work till I got this.

avatar image elloPropello · Oct 17, 2017 at 06:30 PM 0
Share

awesome... thank you. exactly what was bugging me for some hours now :)

Show more comments
avatar image
1

Answer by carlosabreuRD · Oct 07, 2016 at 07:53 PM

http://answers.unity3d.com/questions/1164189/top-down-multiplayer-camera-follow.html

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
1

Answer by domapalb · Dec 09, 2018 at 10:44 PM

Edited to fix problem pointed out by @ystetsyk

I know this is a little old, but I found a very simple way to get it to work, Just put this in the PlayerController script:

 private Camera myCamera; // Copy of the main camera, attached to the player
 
 public override void OnStartLocalPlayer()
 {
     myCamera = Instantiate(Camera.main);
     myCamera.transform.rotation = transform.rotation;
     myCamera.transform.position = transform.position + new Vector3(offsetX, offsetY, offsetZ);
     myCamera.transform.SetParent(transform);
 }
 void Update()
 {
     // Do update stuff, including moving the player
     // ...
     Camera.main.CopyFrom(myCamera);
 }

First, make a copy of the main camera and position it where you want it for the player, then make it a child of the player. If you were to make the camera a child of the player prefab, then syncing the players would cause the problem of everyone seeing the same camera. Here the camera is not made a part of the prefab so it isn't synced, instead, it is now a child of the Client's player instance.

In Update(), the main camera is now following our player's camera, so that when the player stops the session, only the camera attached is destroyed, not the main camera. They can then join a new game, and it will again just make a camera for the main camera to follow.

Now you can simply manipulate the myCamera camera however you want.

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 ystetsyk · Dec 13, 2018 at 10:39 PM 0
Share

Thanks simple solution but I found problem here: when you disconnect from the server your player is destroyed as well as your camera.

avatar image
0

Answer by rak1n · Mar 19, 2016 at 07:30 PM

so I added camera object to player and changed use my player prefab. but I seem to only have one camera in scene, when I join a game it only follows one player.

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 subzer0 · Sep 24, 2016 at 10:23 AM 0
Share

I am having the same problem. Did you find a solution ?

avatar image vhzhang2020 · Oct 18, 2020 at 10:51 PM 0
Share

just delete the other camera: if (!isLocalPlayer) { mycam.enabled = false; } else { mycam.enabled = true; }

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

17 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

Related Questions

MLAPI - Lost with instantiate 1 Answer

PUN fps help 0 Answers

How can I change the viewport rect of two active cameras at runtime? - Splitscreen Multiplayer 1 Answer

Multiplayer Join / Create Room ( Map Selection ) 0 Answers

Should occlusion culling be used in multiplayer games? 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