Search Unity

Resolved Movement swapped for players

Discussion in 'Multiplayer' started by jlorenzi, Feb 5, 2022.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    Fix: I had to deactivate the other players camera, now it works.
    Updated code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     [SerializeField] [Range(0, 30)] private float walkSpeed;
    9.     [SerializeField] [Range(0, 30)] private float sprintSpeed;
    10.     private float speed;
    11.     [SerializeField] private CharacterController controller;
    12.     public bool isBreaking;
    13.     PhotonView view;
    14.  
    15.     private void Start()
    16.     {
    17.         view = GetComponent<PhotonView>();
    18.  
    19.         if (!view.IsMine)
    20.         {
    21.             Debug.Log("View is not yours, deactivating camera");
    22.  
    23.             transform.Find("Main Camera").gameObject.SetActive(false);
    24.         }
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         if (Input.GetKey(KeyCode.LeftShift))
    30.         {
    31.             speed = sprintSpeed;
    32.         }
    33.  
    34.         else
    35.         {  
    36.             speed = walkSpeed;
    37.         }
    38.  
    39.         float x = Input.GetAxis("Horizontal");
    40.         float z = Input.GetAxis("Vertical");
    41.  
    42.         Vector3 move = transform.right * x + transform.forward * z;
    43.  
    44.         if (!isBreaking && view.IsMine)
    45.         {
    46.             controller.Move(move * speed * Time.deltaTime);
    47.         }
    48.     }
    49. }
    50.  
    I'm using Photon to make a multiplayer game. Everything works until another player joins, then the first player is moving the second player and vice versa.
    (OLD CODE)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     [SerializeField] [Range(0, 30)] private float walkSpeed;
    9.     [SerializeField] [Range(0, 30)] private float sprintSpeed;
    10.     private float speed;
    11.     [SerializeField] private CharacterController controller;
    12.     public bool isBreaking;
    13.     PhotonView view;
    14.  
    15.     private void Start()
    16.     {
    17.         view = GetComponent<PhotonView>();
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         if (Input.GetKey(KeyCode.LeftShift))
    23.         {
    24.             speed = sprintSpeed;
    25.         }
    26.  
    27.         else
    28.         {
    29.             speed = walkSpeed;
    30.         }
    31.  
    32.         float x = Input.GetAxis("Horizontal");
    33.         float z = Input.GetAxis("Vertical");
    34.  
    35.         Vector3 move = transform.right * x + transform.forward * z;
    36.  
    37.         if (!isBreaking && view.IsMine)
    38.         {
    39.             controller.Move(move * speed * Time.deltaTime);
    40.         }
    41.     }
    42. }
     
    Last edited: Feb 5, 2022