1
votes

I have a game where you control a ball using W,A,S and D. The Main Camera is following the Player (the ball) using this script:

public class CameraMovement : MonoBehaviour {

public GameObject Player;

private Vector3 Set;


void Start()
{
    Set = transform.position - Player.transform.position;
}

void LateUpdate()
{  
        transform.position = new Vector3 (-280f, 15f, 28f);
        transform.rotation = Quaternion.Euler (0.0f, 90.0f, 0.0f);
}
}       

}

Now, I made this game multiplayer using LAN. So you can have more players controlling multiple balls in the same time. However, everytime a player joins the LAN Server, a new ball is created from the Prefabs using the Network Manager. The issue is that as you can see in the script, in the single player version I have to connect the gameobject 'Player' to the camera, but when you are running this game in a LAN Server, there is no Player game object, the Network Manager creates one only after a player joined the game. So my question is, how do I connect the camera to each player that's joining the game? Also, I belive that each player has to have his own camera, since he could move diffrently from another player, am I right?

Thanks for the help!

1

1 Answers

0
votes

First off the question is kind of broad because you are not giving information about the networking you are using. Are you using PUN,UNET or a different one?

I can broadly explain how to fix this issue.

When the player joins you should find the main camera and assign it to him inside its own client.

Because every client has its own main camera it won't be a problem.

//The code will look something like this.
void OnJoinedServer() //When the player joins the server
{
     //find the main camera gameobject
     GameObject camera = Camera.main.gameObject;
     camera.transform.SetParent(player.transform);
     //Maybe set the right position as well
     camera.transform.localPosition = [SomeVector3];
}

Hope this helps.

And remember for next time try to give as much information as possible.