- Home /
Trying to follow tutorial to set up Multiplayer camera follow on UNET... and it goes wrong.
Hello! So, I am following this tutorial. It is supposed to teach how to set up a third person camera for Unity multiplayer videogames that use UNET, (I am using Unity2018.2.15f1, so UNET works fine there).
I followed the previous tutorial (which was on different parts of the following code, not the camera part. The other parts work perfectly). But after following the camera follow tutorial, I run the game on one window and it parents a camera to the player, and then, on other window, I create a new player, but it does not create a new camera for the new player. It kind of steals the camera from the previous player and parents it to the new player (I looked on the hierarchy) and it leaves the previous player without a camera. It is weird, because it worked correctly for the person that made the tutorial, but it does not work for me.
The code is assigned to the player prefab.
This is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class SetupLocalPlayer : NetworkBehaviour {
[SyncVar]
public string pname = "player";
void OnGUI()
{
if(isLocalPlayer)
{
pname = GUI.TextField (new Rect (25, Screen.height - 40, 100, 30), pname);
if(GUI.Button(new Rect(130,Screen.height - 40,80,30), "Change"))
{
CmdChangeName(pname);
}
}
}
[Command]
public void CmdChangeName(string newName)
{
pname = newName;
}
// Use this for initialization
void Start () {
if(isLocalPlayer)
GetComponent<PlayerMovement>().enabled = true;
Camera.main.transform.position = this.transform.position - this.transform.forward*4 +
this.transform.up*4;
Camera.main.transform.LookAt(this.transform.position);
Camera.main.transform.parent = this.transform;
}
void Update()
{
this.GetComponentInChildren<TextMesh>().text = pname;
}
}
(I put the whole code in there just in case it is relevant). Well, to make it less twisted here's the camera part only:
void Start () {
if(isLocalPlayer)
GetComponent<PlayerMovement>().enabled = true;
Camera.main.transform.position = this.transform.position - this.transform.forward*4 +
this.transform.up*4;
Camera.main.transform.LookAt(this.transform.position);
Camera.main.transform.parent = this.transform;
}
The channel that uploaded that tutorial does not seem to answer questions anymore, and a ton of people comment that they have the same issue.
I have done a lot of research, and I cannot find a solution to this problem, nor I can find another way of making the camera follow the players that works for me.
I am very new to Unity and programming and (obviously) networking.
If anyone could give me some idea of how to fix this, I would be very grateful!
Thank you for reading this!
Your answer
![](https://koobas.hobune.stream/wayback/20220612224409im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Camera Follow a Target, but not Centered on it 1 Answer
How to change the focus of the camera? 0 Answers
camera follow gameobject moving in angular path. 0 Answers
Camera follow players z axis 0 Answers