Search Unity

Help Wanted Finding player position - Mirror Networking

Discussion in 'Multiplayer' started by ApexSeal, Nov 21, 2020.

  1. ApexSeal

    ApexSeal

    Joined:
    Sep 5, 2020
    Posts:
    6
    Hello,

    I've been learning about Mirror the past couple days, but I cannot find any information on how to get the position of a player. Normally in a non-networked game, you can make a Player type and FindObjectOfType<Player>() to instantiate the player object. From there you can easily use player.transform.position to get the player position.

    I would like to do something like this but with networking (using Mirror). I want to get the position of the player so that my enemy will track / chase it, however I do not understand how to get the position of a networked player. In addition, the solution should be able to scale to 2 or 3 players within a room, right?

    Also, if it helps, the way I have configured the game, is that the players are prefabs and spawn in once the host creates the room, and additionally spawns in when connected as the client.

    Can someone point me in the right direction? Thank you.
     
    BetaMark likes this.
  2. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    109
    I'm not sure of your case without looking through your client/host code flow -- but I think what you are asking for is in identity under your connectionToClient.identity.transform.

    Take a look at https://mirror-networking.com/docs/api/Mirror.NetworkConnection.html for what is going on in that class.

    On the server side, when you are iterating through a bunch of objects which have NetworkBehaviour on them (like you would for your objects with Player on them if you were doing a local/multiplayer game) you can find which one is *this* player by comparing the object's connectionId against the "This" connectionId e.g.:
    if (unit.connectionToClient.connectionId == connectionToClient.connectionId) {
    Debug.Log($"The object {unit.name} has the same connectionId as {gameObject.name}");
    }


    Now, over on your *client* side, you can check to see if the current client has authority over an object (e.g. it was spawned by this particular game client) just by checking the bool of
    hasAuthority
    .

    With that said, the tiny little bit I know about mirror, I've picked up from a the Unity Multiplayer class built by Dapper Dino over at Udemy. If you've got the time to just watch all the videos in fast forward, he covers pretty much end to end building an online multiplayer RTS with Mirror and Unity and it feels like "the missing manual" for mirror to me.
     
unityunity