Search Unity

Beginner Networking Help!

Discussion in 'Multiplayer' started by RagsWasTaken, Mar 5, 2017.

  1. RagsWasTaken

    RagsWasTaken

    Joined:
    Mar 5, 2017
    Posts:
    2
    Hello,
    So I just started messing around with networking for a school project and I'm having a little bit of trouble with one part in my game. Overview of my game: each player starts in a kind of lobby where they choose one of two roles. They either are the navigator (can see the entire map and can just move around) or the mover (can only see key parts and must follow the maze). Once each player has chosen a role they game fades out and the player objects are moved to their correct positions. In order to make it so that the mover player can't see the map, I have two different maps, one with a black background the is the same shade as the maze, and one without that. In order for the player objects to communicate with each other, I have a second mover and navigator object that will begin mimicking the actual player object's moves just on the other map. I am trying to use pretty much the base Networking Manager and HUD that Unity supplies me with.

    Now to get to the problem. I have discovered two errors. One where for some reason the host appears to not go where it is supposed to go. It just stays in the lobby. The problem with this code I believe is to be found in this script, this controls the player movements and where they're supposed to go when the game starts:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;

    public class PlayerPickingMove : NetworkBehaviour {

    public GameObject mainCamera;

    Rigidbody2D rb;

    public float speed = 5f;
    public float xMove;
    public float yMove;
    public int xVar = 0;
    public int yVar = 0;

    public bool mouseUse = false;
    public bool keyboardUse = false;

    public bool canMove = true;

    public bool isNav = false;
    public bool isMov = false;

    bool isActive = false;

    public override void OnStartLocalPlayer()
    {
    GetComponent<SpriteRenderer> ().color = new Color (0f, .78f, .42f);
    }

    // Use this for initialization
    void Start()
    {
    mainCamera = GameObject.Find ("Camera");
    rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {

    if (!isLocalPlayer)
    {
    return;
    }
    // if (!GameObject.Find ("GameManager").GetComponent<Manager> ().gameStart) {

    // } else {
    if (GameObject.Find ("GameManager").GetComponent<Manager> ().gameStart||isActive) {
    isActive = true;
    PickCharacter ();
    }
    CalcVars ();
    ChangeMove ();
    rb.velocity = new Vector2 (xMove, yMove);

    }


    void ChangeMove()
    {
    xMove = speed * xVar;
    yMove = speed * yVar;
    }


    void CalcVars()
    {
    if ((Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S)) || !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
    {
    yVar = 0;
    }
    else if (Input.GetKey(KeyCode.W))
    {
    yVar = 1;
    }
    else if (Input.GetKey(KeyCode.S))
    {
    yVar = -1;
    }

    if ((Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D)) || !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
    {
    xVar = 0;
    }
    else if (Input.GetKey(KeyCode.A))
    {
    xVar = -1;
    }
    else if (Input.GetKey(KeyCode.D))
    {
    xVar = 1;
    }

    }


    void PickCharacter()
    {
    if(isLocalPlayer && canMove)
    {

    if (transform.position.x < 0) {//nav player
    mainCamera.transform.position = new Vector3 (-20, 0, -10);
    transform.position = new Vector3 (-20f, 0, .4f);
    isNav = true;
    GameObject.Find ("OtherMouse").GetComponent<OtherMouseIcon> ().mouse = this.gameObject;
    GetComponent<Collider2D> ().enabled = false;

    canMove = false;
    } else {//move player
    mainCamera.transform.position = new Vector3 (-40f, 0, -10);
    transform.position = new Vector3 (-40.5f, -3.5f, -9f);
    GameObject.Find ("OtherPlayer").GetComponent<OtherPlayerIcon> ().player = this.gameObject;
    isMov = false;
    canMove = false;


    }
    }
    }
    }


    The other problem is that the mimicked objects are not responding correctly. The non-host one will mimic correctly, but the host one will not. It also will give an error like "x game object variable has not been given" even though I set it using the player object's tag. Code for this can be found here:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;


    public class OtherPlayerIcon : NetworkBehaviour {

    [SyncVar]
    public GameObject player;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void FixedUpdate()
    {
    CopyMouse ();
    }


    void CopyMouse()
    {
    if (GameObject.Find ("GameManager").GetComponent<Manager> ().gameStart) {
    player = GameObject.FindGameObjectWithTag ("Player");
    transform.position = new Vector2 (player.transform.position.x + 20, player.transform.position.y);
    }
    }
    }
     
  2. Oakioats

    Oakioats

    Joined:
    Mar 27, 2016
    Posts:
    7
    When you say, doesnt know where to go, do you mean that when you open up the HUD scene it wont go to the play scene?

    I am sort of a beginner as well, but if you place this character in the network manager then that will the character that is spawned.
     
  3. RagsWasTaken

    RagsWasTaken

    Joined:
    Mar 5, 2017
    Posts:
    2
    Basically the player objects were told to go to vector3(x,y,z) but then the host did not follow that.