Host Camera Doesn't Work

Hello, I have a problem where the movement of the camera does not work after doing this lecture. I can move the camera fine as a playing joining but as a host, the camera movements do not work. I tried hosting in Unity and the build and it seems to only affect the host, I can still add buildings and build units, hosting it’s just the camera.

Was wondering if I may have done something when changing codes.

Hi there, sorry you are having trouble. First thing I would check is if your CameraController script is the same as the instructors. Post it here if you want some help, the difference might not be obvious. Another thing to try, is sometimes removing and re-adding the camera makes a difference. Unity is fun that way.

Here’s the code, I doubled checked seems to be fine. It works for the player joining the game but not for the Host, also if the joining player leaves the game, the camera for the host works again.

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 (!hasAuthority|| !Application.isFocused) {return;}

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

        if (previousInput == Vector2.zero)
        {
            Vector3 cursorMovment = Vector3.zero;
            Vector2 cursorPosition = Mouse.current.position.ReadValue();

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

            pos += cursorMovment.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>();
    }
}

You are right, code seems fine. Maybe it is related to how it is setup in the editor. Can you put a debug statement in Update() after the authority condition? Then first run the client in the editor, then the host in the editor with the second player joining via the build. Let me know when that debug gets called.

It’s called once the lobby screen is up.

It’s called for the host and the client? Are there any errors in the console? Does it keep running when the game loads?

Here’s a video of what’s happening, I hope I have the debug log where you wanted it lol.

Hey there, I can’t really tell much about the debug from the video, sorry. Is UpdateCameraPosition() being called on the host during the game? Also, is this at all affected by whether the host is in the editor or the build?

I think it’s a problem in another script, to test it, I change the code so I can play single player and the camera works just fine as a host in the build or editor, so I must have something wrong in another script where if it’s more than one player in the game the camera only seems to work for one player.

Which I think is why when the joined player leaves like in the video, the camera for the host starts to work. Still looking to see where I may have missed up a code in the recent lecture.

Feel so ashamed, at some point my player camera was not off default lol.

1 Like

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

Privacy & Terms