- Home /
Unity 3rd Person Camera in Multiplayer
Hey People! I've made a 3rd Person Camera. This worked very well without Player and camera prefabs in a Little prototype. I wanted to integrate my code into my Network game but it hasn't worked yet. The Problem is that the camera is turning around a Point if I use my mouse (so this works fine) but if I move my character the camera doesn't make a move. I'm not sure if this has something to do with my Network code so I'll post both codes here. I hope you're able to help me >_<
Camera Code (attached on my main camera):
public GameObject target;
public float rotateSpeed = 5;
Vector3 offset;
void Start()
{
offset = target.transform.position - transform.position;
}
void LateUpdate()
{
if(networkView.isMine)
{
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
target.transform.Rotate(0, horizontal, 0);
float desiredAngle = target.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
}
}
And here is my netowrk code: private bool refreshing = false; private HostData[] hostData; public string gameName = "Network Game"; public float buttonX; public float buttonY; public float buttonWidth; public float buttonHeight; private int i = 1;
public GameObject playerPrefab;
public Camera camera;
public Transform spawnObject;
public Transform cameraPosition;
// Use this for initialization
void Start ()
{
buttonX = Screen.width * 0.05f;
buttonY = Screen.width * 0.05f;
buttonWidth = Screen.width * 0.1f;
buttonHeight = Screen.width * 0.05f;
}
void startServer()
{
Network.InitializeServer(12, 25001, !Network.HavePublicAddress());
MasterServer.RegisterHost(gameName,"Anime Girls vs Mechs", "Anime Girls vs Mechs Game");
}
void refreshHostList()
{
MasterServer.RequestHostList (gameName);
refreshing = true;
}
void Update()
{
if(refreshing)
{
if(MasterServer.PollHostList ().Length != 0)
{
refreshing = false;
hostData = MasterServer.PollHostList();
Debug.Log (MasterServer.PollHostList ().Length);
}
}
}
void spawnPlayer()
{
Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity, 0);
Network.Instantiate(camera, cameraPosition.position, Quaternion.identity, 0);
}
//Messages
void OnServerInitialized()
{
Debug.Log ("Server initialized");
spawnPlayer();
}
void OnConnectedToServer()
{
spawnPlayer();
}
void OnMasterServerEvent(MasterServerEvent masterServerEvent)
{
if(masterServerEvent == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log ("Server Registrated");
}
}
//Draw GUI
void OnGUI()
{
if(!Network.isClient && !Network.isServer)
{
if(GUI.Button (new Rect(buttonX, buttonY, buttonWidth, buttonHeight), "Start Server"))
{
Debug.Log ("Server Started");
startServer();
}
if(GUI.Button (new Rect(buttonX, buttonY * 1.2f + buttonHeight, buttonWidth, buttonHeight), "Refresh Hosts"))
{
Debug.Log ("Refreshing Hosts");
refreshHostList();
}
if(hostData != null)
{
foreach(HostData hosts in hostData)
{
if(GUI.Button (new Rect(buttonX * 1.5f + buttonWidth, buttonY * 1.2f + (buttonHeight * i), buttonWidth * 3f, buttonHeight * 0.5f), hosts.gameName))
{
Network.Connect(hosts);
}
if(MasterServer.PollHostList ().Length > i)
i++;
}
}
}
}
am I doing it wrong or what is the Problem?
Your answer
Follow this Question
Related Questions
mirror player camera using one camera 0 Answers
Broadcast camera render over network 2 Answers
Multiplayer camera switch issue 0 Answers
How to set up multiple cameras for multiplayer 1 Answer
Lock on opponent in UNET (C#) 1 Answer