Camera gets fixed to one posiition after a client joins the game

when i join as host everthing is fine but as soon as a client joins the camera in the host window shifts to the clients base and is stuck that is the host can no longer use camera movement not in the screen nor in the minimap … so I tried setting the virtual camera priority for host in the editor to 11 and the camera started to work again …can someone help me with this …thank you

Hi there,
Are there any errors in the console? Can you share your camera controller script?

Do you see more than one camera in your scene when running it form the editor?

using System.Collections;
using System.Collections.Generic;
using Mirror;
using UnityEngine;
using UnityEngine.InputSystem;

public class CameraController : NetworkBehaviour
{
[SerializeField] private Transform playerCameraTransform = null;
[SerializeField] private float speed = 20f;
[SerializeField] private float screenBorderThickness = 10f;
[SerializeField] private Vector2 screenXLimits = Vector2.zero;
[SerializeField] private Vector2 screenZLimits = Vector2.zero;

private Vector2 previousInput;

private Controls controls;

public override void OnStartAuthority()
{
    playerCameraTransform.gameObject.SetActive(true);

    controls = new Controls();

    controls.Player.MoveCamera.performed += SetPreviousInput;
    controls.Player.MoveCamera.canceled += SetPreviousInput;

    controls.Enable();
}

[ClientCallback]
private void Update()
{
    if (!isOwned || !Application.isFocused) { return; }

    UpdateCameraPosition();
}

private void UpdateCameraPosition()
{
    Vector3 pos = playerCameraTransform.position;

    if (previousInput == Vector2.zero)
    {
        Vector3 cursorMovement = Vector3.zero;

        Vector2 cursorPosition = Mouse.current.position.ReadValue();

        if (cursorPosition.y >= Screen.height - screenBorderThickness)
        {
            cursorMovement.z += 1;
        }
        else if (cursorPosition.y <= screenBorderThickness)
        {
            cursorMovement.z -= 1;
        }
        if (cursorPosition.x >= Screen.width - screenBorderThickness)
        {
            cursorMovement.x += 1;
        }
        else if (cursorPosition.x <= screenBorderThickness)
        {
            cursorMovement.x -= 1;
        }

        pos += cursorMovement.normalized * speed * Time.deltaTime;
    }
    else
    {
        pos += new Vector3(previousInput.x, 0f, previousInput.y) * speed * Time.deltaTime;
    }

    pos.x = Mathf.Clamp(pos.x, screenXLimits.x, screenXLimits.y);
    pos.z = Mathf.Clamp(pos.z, screenZLimits.x, screenZLimits.y);

    playerCameraTransform.position = pos;
}

private void SetPreviousInput(InputAction.CallbackContext ctx)
{
    previousInput = ctx.ReadValue<Vector2>();
}

}

and no there is no two maincameras in the scene when I run in editor

the error occurs only when i join in as client that is i lose the control from the camera from the host and to regian control i had to set the priority of virtual camera of play with conid 0 to 11

playerid
priority

so basically the two virtual camera from two players must not contain same priority values … i can do this in editor …do I have to hard code this somehow so that each client joined will have thier virtual camera priority values different rather than being 10 … i think this is happening because the server is controlling the camera movement and it is finding difficult to differentiate between the virtual camera …

Looing at my own project, I noticed that we have direct reference to the virtual camera for each player. I also noticed that this virtual camera is inactive in the prefab, and is only set to active in OnStartAuthority. I think the goal here is to only activate the camera on the relevant client.

Is yours inactive in the prefab? Perhaps we need another logic check here to make sure this does not get activated in the host, or to deactivate it, if it’s not the host. There should only be 1 active virtual camera at a time.

1 Like

yes u were right two instances of virtual camera were being created after adjusting a code a bit it worked thank you

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms