#Unity Multiplayer player Movement using Mirror on 2D

15 messages · Page 1 of 1 (latest)

tidal tree
#

Hey guys, I'm watching multiple courses on gamedev.tv, unfortunately I can't seem to find a 2D option for multiplayer and so I went with a course using 3D instead.

everything so far I could figure stuff out, except for the player movement script..

this is my playermovement script so far:

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

public class PlayerMovement : MonoBehaviour
{


    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Animator anim;
    Vector2 movement;
    

    private void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
        

        anim.SetFloat("Horizontal", movement.x);
        anim.SetFloat("Vertical", movement.y);
        anim.SetFloat("Speed", movement.sqrMagnitude);

        if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
        {
            anim.SetFloat("lastMoveX", Input.GetAxisRaw("Horizontal"));
            anim.SetFloat("lastMoveY", Input.GetAxisRaw("Vertical"));
        }

        if(movement.magnitude > 1)
        {
            movement.Normalize();
        }
    }

    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }

}

if I use the Mirror build in multiplayer right now, the player just moves both characters, which is obviously not really what I want..

would really appreciate if someone could help me figure this out!

#

He also went ahead and made these scripts as his NetworkPlayer:

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

public class MyNetworkPlayer : NetworkBehaviour
{
    [SerializeField] private TMP_Text displayNameText = null;

    [SyncVar(hook = nameof(HandleDisplayNameUpdated))]
    [SerializeField]
    private string displayName = "Missing Name";

    #region Server

    [Server]
    public void SetDisplayName(string newDisplayName)
    {
        displayName = newDisplayName;
    }

    private void HandleDisplayNameUpdated(string oldName, string newName)
    {
        displayNameText.text = newName;
    }

    [Command]
    private void CmdSetDisplayName(string newDisplayName)
    {
        if (newDisplayName.Length <= 2 || newDisplayName.Length >= 30) {return;}
        RpcLogNewName(newDisplayName);
        SetDisplayName(newDisplayName);
    }



    #endregion

    #region Client


    [ContextMenu("Set My Name")]
    private void SetMyName()
    {
        CmdSetDisplayName("My New Name");
    }

    [ClientRpc]
    private void RpcLogNewName(string newDisplayName)
    {
        Debug.Log(newDisplayName);
    }

    #endregion
}


#

Unity Multiplayer player Movement using Mirror on 2D

valid jay
#

Are you completely new to game dev?

#

To answer your question

If(!islocalplayer){return;}

Put that before update.

If you are completely new to game dev stop trying to make a multi-player game and just focus on single player games for a year. Doing multi-player adds so much more complexity to everything which new devs don't need. Being a professional is knowing when not to do something.

tidal tree
grim harbor
#

@tidal tree multiplayer tutorial to compare yours against

#

Is client auth bool ticked on network transform?

tidal tree
#

lemme check

grim harbor
#

And like Lord B said above, all scripts and all parts of scripts will run everywhere unless you tell it not to, so my players Start, Update, FixedUpdate etc will all run on your players screen too

#

You need isLocalPlayer or hasAuthority blocks, or disable scripts, components etc

#

The quickstart guide covers that, spend 30 mins blank project, do the quide, it will help a lot for the theory and setup. If you get stuck 👍

tidal tree
grim harbor