Both players are moving together what is wrong with my script?

When I test my game with two players they both move with the same input. What do I need to correct in my Player Controller script?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityStandardAssets.CrossPlatformInput;

public class MultiplayerController : NetworkBehaviour
{

    public float moveSpeed;
    private float currentMoveSpeed;

    private Animator anim;
    private Rigidbody2D myRigidbody;

    private bool playerMoving;
    public Vector2 lastMove;
    private Vector2 moveCrossPlatformInputManager;

    private static bool playerExists;

    public string startPoint;

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
        myRigidbody = GetComponent<Rigidbody2D>();


    }

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

        if (!isLocalPlayer)
            return;
        playerMoving = false;


        moveCrossPlatformInputManager = new Vector2(CrossPlatformInputManager.GetAxisRaw("Horizontal"), CrossPlatformInputManager.GetAxisRaw("Vertical")).normalized;

        if (moveCrossPlatformInputManager != Vector2.zero)
        {
            myRigidbody.velocity = new Vector2(moveCrossPlatformInputManager.x * moveSpeed, moveCrossPlatformInputManager.y * moveSpeed);
            playerMoving = true;
            lastMove = moveCrossPlatformInputManager;

        }
        else {
            myRigidbody.velocity = Vector2.zero;
        }

        anim.SetFloat("MoveX", CrossPlatformInputManager.GetAxisRaw("Horizontal"));
        anim.SetFloat("MoveY", CrossPlatformInputManager.GetAxisRaw("Vertical"));
        anim.SetBool("PlayerMoving", playerMoving);
        anim.SetFloat("LastMoveX", lastMove.x);
        anim.SetFloat("LastMoveY", lastMove.y);


    }
}

Disable the multiplayercontroller script on your playerprefab. Make a new networkbehaviour, replace the start function with OnStartLocalPlayer(), in this function you enable the multiplayercontroller script. This will hopefully fix your problem.

2 Likes

And it did! Thanks a lot bud!

But now I have a different problem, when I move other clients don’t see the animation how do I fix this?

you need the network animator components watch a tut

1 Like

Stupid question about your original script/problem - are you seeing this problem testing locally on a single machine? Or have you seen this issue where each player is running on a separate computer?

Your isLocalPlayer check should be sufficient for blocking this unless you’re getting some weirdness on local tests where both players are receiving input. I’ve seen this happen once I’m pretty sure. Just to verify, have each player get input from a different button perhaps.

If the problem is that, within one game instance, your fixedupate code is running for both player objects (I.e. isLocalPlayer is true on both objects), then I’d probably ask how you’re creating these player objects. If you just let the network manager spawn them, you should end up with 2 player objects on each game instance, and only 1 of those objects on each instance should have isLocalPlayer true.

How do i get the local Player without using NetworkBehaviour

As far as I know isLocalPlayer is only accessible through a NetworkBehaviour or by querying the network identity. I believe you can call the following from a MonoBehaviour script.

GetComponent<NetworkIdentity>().isLocalPlayer
2 Likes