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.

Unity Multiplayer Question - Loss of virtual joystick control when a player joins the server (PUN)

Discussion in 'Multiplayer' started by Despaircito, Jun 24, 2022.

  1. Despaircito

    Despaircito

    Joined:
    May 26, 2022
    Posts:
    1
    I'm trying to create a mobile phone multiplayer game which uses a virtual joystick control to move the player around (airplanes). Currently, when the first player creates and joins an empty game room, they are able to move around as intended. Once a second player connects, both players lose control of their characters. I've noticed that the positions of both players are still synced but not the direction they're facing.

    Here is the movement script:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Photon.Pun;
    6.  
    7. public class PlaneMovement : MonoBehaviour
    8. {
    9.     public float Speed;
    10.     public float Acceleration;
    11.     public float RotationControl;
    12.     public Joystick joystick;
    13.  
    14.     Rigidbody2D rb;
    15.  
    16.     PhotonView view;
    17.  
    18.     public Animator afterburnerAnimator;
    19.     private string currentState;
    20.  
    21.     //Animation states
    22.     const string AFTERBURNER_IDLE = "idle";
    23.     const string AFTERBURNER_P1 = "Power 1";
    24.     const string AFTERBURNER_P2 = "Power 2";
    25.     const string AFTERBURNER_P3 = "Power 3";
    26.  
    27.  
    28.     //Joystick params
    29.     float MovY, MovX = 1;
    30.     Vector2 JoystickDir = Vector2.zero;
    31.  
    32.     //Wing behavior
    33.     public float wingDrag = 1f; //this is for how much you slow down when you turn and how much of
    34.     //this force gets converted into forward movement, like wingsize
    35.     public float wingLift = 0.5f; //this for when you don't want your plane to crash (I'd rename it)
    36.     //crashability but like that's ultimately unhelpful
    37.  
    38.     //Directional change vars
    39.     bool rightFacing = true;
    40.     float timeSinceDirChange = 0;
    41.  
    42.  
    43.     // Start is called before the first frame update
    44.     void Start()
    45.     {
    46.         rb = GetComponent<Rigidbody2D>();
    47.         view = GetComponent<PhotonView>();
    48.     }
    49.  
    50.     // Update is called once per frame
    51.     void Update()
    52.     {
    53.         if(view.IsMine)
    54.         {
    55.             MovY = joystick.Vertical;
    56.             MovX = joystick.Horizontal;
    57.             JoystickDir = joystick.Direction;
    58.         }
    59.      
    60.     }
    61.  
    62.     private void FixedUpdate()
    63.     {
    64.         if(view.IsMine)
    65.         {
    66.             float joystickMagnitude = (float) Math.Sqrt(MovX*MovX + MovY*MovY);
    67.  
    68.             //adding thrust based on magnitude of joystick displacement
    69.             Vector2 Vel = transform.right * (joystickMagnitude * Acceleration);
    70.             rb.AddForce(Vel);
    71.  
    72.  
    73.             //changing animation state of afterburner
    74.             if (joystickMagnitude == 0)
    75.             {
    76.                 ChangeAnimationState(AFTERBURNER_IDLE);
    77.             } else if (joystickMagnitude > 0 && joystickMagnitude < 0.33f)
    78.             {
    79.                 ChangeAnimationState(AFTERBURNER_P1);
    80.             }
    81.             else if (joystickMagnitude >= 0.33f && joystickMagnitude < 0.66 )
    82.             {
    83.                 ChangeAnimationState(AFTERBURNER_P2);
    84.             }
    85.             else
    86.             {
    87.                 ChangeAnimationState(AFTERBURNER_P3);
    88.             }
    89.  
    90.             //turning, speed based on rotation control
    91.             if(Acceleration > 0)
    92.             {
    93.                 //transform.right will be fixed as the forward direction
    94.                 float Dir = Vector2.SignedAngle(JoystickDir, transform.right);
    95.                 rb.rotation -= Dir * RotationControl;
    96.             }
    97.  
    98.  
    99.             //speed limit enforced using Speed
    100.             if(rb.velocity.magnitude > Speed)
    101.             {
    102.                 rb.velocity = rb.velocity.normalized * Speed;
    103.             }
    104.  
    105.             //adding wing drag
    106.             Vector2 bleed = wingDrag * transform.up.normalized * Vector2.Dot(transform.up, rb.velocity);
    107.             rb.AddForce(bleed * -1.0f);
    108.             rb.AddForce(bleed.magnitude * transform.right);
    109.  
    110.             //adding nose droop like when control surfaces are disabled
    111.             if(MovX == 0 && MovY == 0)
    112.             {
    113.                 float Dir = Vector2.SignedAngle(rb.velocity, transform.right);
    114.                 rb.rotation -= Dir * RotationControl;
    115.             }
    116.  
    117.             //adding wing lift
    118.             //rb.AddForce(rb.velocity.x * transform.up * wingLift);
    119.  
    120.             float horizontalVelocity = Vector2.Dot(rb.velocity, Vector2.right);
    121.             rb.AddForce(horizontalVelocity * Vector2.up * wingLift);
    122.             rb.AddForce(horizontalVelocity * -Vector2.right * wingLift);
    123.  
    124.  
    125.             //adding check to flip y scale of playerobject
    126.             //
    127.             if(rb.velocity.x < 0)
    128.             {
    129.                 if (rightFacing)
    130.                 {
    131.                     rightFacing = false;
    132.                     Flip();
    133.                 }
    134.             } else
    135.             {
    136.                 if (!rightFacing)
    137.                 {
    138.                     rightFacing = true;
    139.                     Flip();
    140.                 }
    141.             }
    142.         }
    143.     }
    144.  
    145.     private void Flip()
    146.     {
    147.         //TODO: trigger to activate animation
    148.         transform.Rotate(180f, 0f, 0f);
    149.     }
    150.  
    151.     void ChangeAnimationState(string newState)
    152.     {
    153.         //stop the same animation from interrupting itself
    154.         if (currentState == newState) return;
    155.  
    156.         //play the animation
    157.         afterburnerAnimator.Play(newState);
    158.     }
    159. }
    Here is the player instantiation:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class SpawnPlayers : MonoBehaviour
    7. {
    8.     public GameObject playerPrefab;
    9.  
    10.     public float minX;
    11.     public float maxX;
    12.     public float minY;
    13.     public float maxY;
    14.  
    15.     private void Start()
    16.     {
    17.         Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
    18.         PhotonNetwork.Instantiate(playerPrefab.name, randomPosition, Quaternion.identity);
    19.     }
    20.  
    21. }
    Here is the hierarchy of the player prefab which might be the issue:
    upload_2022-6-25_3-5-32.png

    upload_2022-6-25_3-10-22.png

    Any help/advice is much appreciated, thanks!