I’ve created a small multiplayer game to get familiar with unity and multiplayer. I’ve started with the Network Manager component and got it all to work.
The following screenshot shows how it should look (and it does without the lobby). You can see the ray coming from the camera and hit the groundplane. I use the ScreenPointToRay(Input.mousePosition) method as input. The player always turns in the direction of the mouse cursor.
If I add the lobby to the menu scene and start the game the following happens (see screen). The ray which should start from the main camera now has it’s origin in the player. I only have one camera in this scene and you can see that it’s above the player and looks to the ground like in the working example screenshot. This only happens on the host player.
The following snippets are from my Player.cs which handles my input for the player. You can see where i draw the debug ray.
Camera viewCamera;
// Use this for initialization
void Start ()
{
viewCamera = Camera.main;
}
void Update ()
{
// Movement input
Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
Vector3 moveVelocity = moveInput.normalized * moveSpeed;
controller.Move(moveVelocity);
// Look input
//viewCamera = GameObject.Find("Main Camera").GetComponent<Camera>(); // tested while debugging
Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayDistance;
if (groundPlane.Raycast(ray, out rayDistance))
{
Rect screenRect = new Rect(0, 0, Screen.width, Screen.height);
if (screenRect.Contains(Input.mousePosition))
{
Vector3 point = ray.GetPoint(rayDistance);
Camera[] cams = Camera.allCameras;
Debug.DrawLine(ray.origin, point, Color.red);
controller.LookAt(point);
While debugging i found out that the viewCamera.transform has it’s position in the player. But obviously the camera is not in the player. The coordinates shown in the inspector while selecting the camera in the hierarchy are correct.
To be sure I reference the right camera in the code I switched it off with viewCamera.gameObject.setActive(false) and my view was gone. I also can switch between the working and non working version by just disable the LobbyManager object and enable the NetworkManager (and vice versa) in the Hierarchy.
I have no clue what the LobyManager does to my hosts camera. Can anyone give mit a hint what I’m doing wrong. Just let me know when I described my problem unclear or I should provide more code.
Thanks for help
Peter

