Player control swapping?

Options
Howdy rather interesting issue I am running into when players enter a room. When the first player enters the room there is no issue he is able to control his local player like you would expect. However, when another player enters the room. the control... swaps. Now Player 1 moves Player 2 and vice versa. Is there anything obvious that I am missing that may be causing this issue to occur? (Oculus VR Platform)
private void Start()   
{
        Instance = this;

        if (playerPrefab == null)
        {
            Debug.LogError("<Color=Red>Missing</Color> playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
        }
        else
        {
            if (PlayerManager.LocalPlayerInstance == null)
            {
                Debug.LogFormat("We are Instantiating LocalPlayer from {0}", SceneManagerHelper.ActiveSceneName);
                
                GameObject Player = PhotonNetwork.Instantiate(playerPrefab.name, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
                Player.name = PhotonNetwork.LocalPlayer.NickName;
            }
            else
            {
                Debug.LogFormat("Ignoring scene load for {0}", SceneManagerHelper.ActiveSceneName);
            }

Comments

  • Malace
    Options
    Depending on your game design. You most likely need to remove all playercontroler scripts from the client copies of other players. So player 1 removes the script from the player 2 object. While player 2 removes the script from player 1 object. Since each client has their own scene instance. They dont delete the controller from thier own objects. This is done with the photonView.IsMine bool. I believe this is shown in the tutorial. But im on my phone so i can verify and draft code after work if needed.
  • Malace
    Options
    I couldn't remember the method names off the top of my head at work. But here is the code you would use to accomplish this. Place the following in your Player Controller script awake method. Just change the component name from Player to match your input controller script name. This should correct your issue with players crossing control.
            private void Awake()
    
            {
    
                if (!photonView.IsMine && GetComponent<Player>() != null)
                {
                    Destroy(GetComponent<Player>());
                }
            }
  • That is what I needed! Thank you very much for your help!
  • jramos
    jramos
    edited April 2019
    Options
    Hello Guys,

    I'm facing the same issue!

    I tried the approach suggested by @Malace.
    This is my code:

    PlayerController.cs

    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Pun;
    
    [RequireComponent(typeof(Animator))]
    [RequireComponent(typeof(PlayerMotor))]
    [RequireComponent(typeof(ConfigurableJoint))]
    public class PlayerController : MonoBehaviourPun
    {
    
        [SerializeField]
        private float speed = 5f; // Velocidade do personagem
    
        [SerializeField]
        private float lookSensitivity = 3f;
    
        [SerializeField]
        private float thrusterForce = 1000f;
    
        [Header("Spring settings:")]
        [SerializeField]
        private JointDriveMode jointMode = JointDriveMode.Position;
    
        [SerializeField]
        private float jointSpring = 20f;
    
        [SerializeField]
        private float jointMaxForce = 40f;
    
        // Cache de componentes
        private PlayerMotor motor;
        private ConfigurableJoint joint;
        private Animator animator;
    
        private void Awake()
        {
    
            if (!photonView.IsMine && GetComponent<PlayerController>() != null)
            {
                Debug.Log(" DISABLE CONTROLER ");
                Destroy(GetComponent<PlayerController>());
            }
        }
        void Start()
        {
            motor = GetComponent<PlayerMotor>();
            joint = GetComponent<ConfigurableJoint>();
            animator = GetComponent<Animator>();
            SetJointSettings(jointSpring);
        }
    
        void Update()
        {
            Debug.Log(PhotonNetwork.IsMasterClient);
            Debug.Log(photonView.IsMine);
            if (photonView.IsMine == false & PhotonNetwork.IsConnected == true)
            {
                return;
            }
            // Calcular volocidade do movimento
    
            //Horizontal
    
            float _xMove = Input.GetAxis("Horizontal");
    
            float _zMove = Input.GetAxis("Vertical");
    
            Vector3 _movHorizontal = transform.right * _xMove;
    
            Vector3 _movVertical = transform.forward * _zMove;
    
            // Vetor do movimento final do personagem
            Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
    
            // Anima o movimento do personagem
            animator.SetFloat("ForwardVelocity", _zMove);
    
            motor.Move(_velocity);
    
            if (Input.GetMouseButtonDown(0))
            {
                Attack();
            }
    
            // Calcula rotacao no vetor 3D (Move em torno)
    
            float _yRot = Input.GetAxisRaw("Mouse X");
    
            Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
    
            // Aplica rotação
    
            motor.Rotate(_rotation);
    
            // Calcula rotacao da cemera no vetor 3D (Move em torno)
    
            float _xRot = Input.GetAxisRaw("Mouse Y");
    
            float _cameraRotationX = _xRot * lookSensitivity;
    
            // Aplica rotação
    
            motor.RotateCamera(_cameraRotationX);
        }
    
        private void SetJointSettings(float _jointSpring)
        {
            joint.yDrive = new JointDrive
            {
                mode = jointMode,
                positionSpring = _jointSpring,
                maximumForce = jointMaxForce
            };
        }
    
        void Attack()
        {
            animator.SetTrigger("Attack");
        }
    
    }

    The players keep swaping their movements.

    Peace
  • lizondo
    Options
    [Solved similar issue]
    jramos I had the same issue. Does your player have a camera attached to it? if so, deleting the "!IsMine" camera may solve the issue.

    solved it for me.
  • zunaira
    Options

    Hi @lizondo from where you deleted IsMine From camera script?

  • Debasish
    Options

    For reference , if the thread is still active you can add the check parameter at the start() and continue doing so in each check if you feel.




  • Lancmo
    Lancmo
    edited April 2023
    Options

    I'm having the same issue and tried many things to fix it but it's not working. I know this thread is old but its the only thread that has the exact same issue that I am having.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    
    
    public class Movement : MonoBehaviour
    {
      public float walkSpeed = 4f;
      public float sprintSpeed = 8f;
      public float maxVelocityChange = 10f;
      [Space]
      public float airControl = 0.5f;
       
      [Space]
      public float jumpHeight = 5f;
    
    
    
      private Vector2 input;
      private Rigidbody rb;
    
      private bool sprinting;
      private bool jumping;
    
      private bool grounded = false;
    
    
    
      // Start is called before the first frame update
      void Start()
      {
        rb = GetComponent<Rigidbody>();
      }
    
      // Update is called once per frame
      void Update()
      {
        input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        input.Normalize();
    
        sprinting = Input.GetButton("Sprint");
        jumping = Input.GetButton("Jump");
      }
    
      private void OnTriggerStay(Collider other)
      {
        grounded = true;
      }
    
    
      void FixedUpdate()
      {
        if (grounded)
        {
          if (jumping)
          {
            rb.velocity = new Vector3(rb.velocity.x, jumpHeight, rb.velocity.z);
          } else if (input.magnitude > 0.5f)
          {
            rb.AddForce(CalculateMovement(sprinting ? sprintSpeed : walkSpeed), ForceMode.VelocityChange);
          }
          else
          {
            var velocity1 = rb.velocity;
            velocity1 = new Vector3(velocity1.x * 0.2f * Time.fixedDeltaTime, velocity1.y, velocity1.z * 0.2f * Time.fixedDeltaTime);
            rb.velocity = velocity1;
          }
        }
        else
        {
          if (input.magnitude > 0.5f)
          {
            rb.AddForce(CalculateMovement(sprinting ? sprintSpeed * airControl : walkSpeed * airControl), ForceMode.VelocityChange);
          }
          else
          {
            var velocity1 = rb.velocity;
            velocity1 = new Vector3(velocity1.x * 0.2f * Time.fixedDeltaTime, velocity1.y, velocity1.z * 0.2f * Time.fixedDeltaTime);
            rb.velocity = velocity1;
          }
        }
    
        grounded = false;
      }
    
    
    
      Vector3 CalculateMovement(float _speed)
      {
        Vector3 targetVelocity = new Vector3(input.x, 0, input.y);
        targetVelocity = transform.TransformDirection(targetVelocity);
    
        targetVelocity *= _speed;
    
        Vector3 velocity = rb.velocity;
    
    
        if (input.magnitude > 0.5f)
        {
          Vector3 velocityChange = (targetVelocity - velocity);
    
          velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
          velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    
          velocityChange.y = 0;
    
    
          return velocityChange;
        }
        else
        {
          return new Vector3();
        }
      }
    }