Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Multiplayer' started by Deleted User, Jan 25, 2017.

  1. Deleted User

    Deleted User

    Guest

    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?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. using UnityStandardAssets.CrossPlatformInput;
    6.  
    7. public class MultiplayerController : NetworkBehaviour
    8. {
    9.  
    10.     public float moveSpeed;
    11.     private float currentMoveSpeed;
    12.  
    13.     private Animator anim;
    14.     private Rigidbody2D myRigidbody;
    15.  
    16.     private bool playerMoving;
    17.     public Vector2 lastMove;
    18.     private Vector2 moveCrossPlatformInputManager;
    19.  
    20.     private static bool playerExists;
    21.  
    22.     public string startPoint;
    23.  
    24.     // Use this for initialization
    25.     void Start()
    26.     {
    27.         anim = GetComponent<Animator>();
    28.         myRigidbody = GetComponent<Rigidbody2D>();
    29.  
    30.  
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void FixedUpdate()
    35.     {
    36.  
    37.         if (!isLocalPlayer)
    38.             return;
    39.         playerMoving = false;
    40.  
    41.  
    42.         moveCrossPlatformInputManager = new Vector2(CrossPlatformInputManager.GetAxisRaw("Horizontal"), CrossPlatformInputManager.GetAxisRaw("Vertical")).normalized;
    43.  
    44.         if (moveCrossPlatformInputManager != Vector2.zero)
    45.         {
    46.             myRigidbody.velocity = new Vector2(moveCrossPlatformInputManager.x * moveSpeed, moveCrossPlatformInputManager.y * moveSpeed);
    47.             playerMoving = true;
    48.             lastMove = moveCrossPlatformInputManager;
    49.  
    50.         }
    51.         else {
    52.             myRigidbody.velocity = Vector2.zero;
    53.         }
    54.  
    55.         anim.SetFloat("MoveX", CrossPlatformInputManager.GetAxisRaw("Horizontal"));
    56.         anim.SetFloat("MoveY", CrossPlatformInputManager.GetAxisRaw("Vertical"));
    57.         anim.SetBool("PlayerMoving", playerMoving);
    58.         anim.SetFloat("LastMoveX", lastMove.x);
    59.         anim.SetFloat("LastMoveY", lastMove.y);
    60.  
    61.  
    62.     }
    63. }
    64.  
     
    Last edited by a moderator: Jan 25, 2017
  2. Deleted User

    Deleted User

    Guest

    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.
     
    MuntyMcFly and Ronitstar89 like this.
  3. Deleted User

    Deleted User

    Guest

    And it did! Thanks a lot bud!
     
  4. Deleted User

    Deleted User

    Guest

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

    UnityUser9860

    Joined:
    Dec 8, 2015
    Posts:
    179
    you need the network animator components watch a tut
     
    MuntyMcFly likes this.
  6. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    242
    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.
     
  7. TheDarkVoyager

    TheDarkVoyager

    Joined:
    May 30, 2020
    Posts:
    1
    How do i get the local Player without using NetworkBehaviour
     
  8. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    126
    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.
    Code (CSharp):
    1. GetComponent<NetworkIdentity>().isLocalPlayer
     
    MuntyMcFly and Joe-Censored like this.