0

I have a networked game with a ship that the player flies around. The ship is a prefab with a camera. When the app runs, the player can move the ship around and the camera moves with the prefab as expected. However, when another player joins the game, that new player takes control of the camera. Each player still has control of their ship and they can move independently, but the camera follows the most recently joined player ship.

I'm at a loss becuase the camera is the child of the player prefab, so I don't understand how another player can have control of the camera.

I've tried adding a network identity with local player identity, but not sure what else to try.

I would like the camera to follow the local player ship.

Please let me know if anything else would help diagnose this issue.

There isn't really any relevant code, but this code does set the initial camera to inactive so that the prefab camera becomes the active camera...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class move : NetworkBehaviour {

    public float moveSpeed = 1f;
    public float smoothTimeY;
    public float smoothTimeX;
    private GameObject joystickcont;
    private Camera firstcamera;
    public Camera shipcamera;

    [SyncVar(hook = "OnSetScale")]
    private Vector3 scale;

    [Command]
    public void CmdSetScale(Vector3 vec)
    {
        scale = vec;
    }

    private void OnSetScale(Vector3 vec)
    {
        transform.localScale = vec;
    }

    void Start () {

        if (!isLocalPlayer)
        {
            return;
        }

        firstcamera = GameObject.FindWithTag("firstcamera").GetComponent<Camera>();
        firstcamera.gameObject.SetActive(false);
        shipcamera.gameObject.SetActive(true);
        CmdSetScale(gameObject.transform.localScale);
        gameObject.transform.localScale = new Vector3(0.05f, 0.05f, 1);

    }

    void Update () {

        if (!isLocalPlayer)
        {
            return;
        }
        CmdSetScale(gameObject.transform.localScale);

        Vector3 dir = Vector3.zero;

        dir.x = Input.GetAxis("Horizontal");
        dir.y = Input.GetAxis("Vertical");

        joystick moveJoystick = joystickcont.GetComponent<joystick>();

        if (moveJoystick.InputDirection != Vector3.zero)
        {
            dir.x = moveJoystick.InputDirection.x;
            dir.y = moveJoystick.InputDirection.y;
        }

        var rb = gameObject.GetComponent<Rigidbody2D>();

        if (dir == Vector3.zero)
        {

        }
        else
        {
            float heading = Mathf.Atan2(dir.x, dir.y);
            transform.rotation = Quaternion.Inverse(Quaternion.Euler(0f, 0f, heading * Mathf.Rad2Deg));
            rb.velocity = new Vector3(dir.x * .2f, dir.y * .2f, 0);
        }
    }
}
tintyethan
  • 1,772
  • 3
  • 20
  • 44
  • 1
    Why are you loading a camera for each player? Shouldn't you have a camera just for the primary player for that client? I suggest you make 2 prefabs: primary with camera, and secondary/netowrked player without. – David Culbreth Oct 13 '18 at 17:58
  • You also have an empty `if` statement but a filled `else` statement. You should invert the logic. – Draco18s no longer trusts SE Oct 13 '18 at 19:31
  • Thanks Draco. I'll double check that. Its code from another source for the joystick, but I'll take a look. – tintyethan Oct 13 '18 at 19:37
  • David, the setup I'm using is after trying with 1 camera which was then parented to the player at run time when they joined, but caused issues with scaling and positioning. I found that standard practice seems to be to put the camera in the player prefab instead. This requires a second camera because the player prefab doesn't exist when the game starts. With the camera in the player prefab, it ensures proper scaling and positioning. I may not understand what you're saying though. – tintyethan Oct 14 '18 at 13:57
  • Have you considered trying to deactivate your camera object in the prefab and then upon Start() you enable it for the specific networked player. – Skdy Oct 16 '18 at 07:21

0 Answers0