Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Third Party [Photon] Trying to get firing projectiles to work in 2D multiplayer. Strange issue...

Discussion in 'Multiplayer' started by jimkurth81, Jul 24, 2021.

  1. jimkurth81

    jimkurth81

    Joined:
    Apr 19, 2020
    Posts:
    3
    This is my first Unity forum post as I try to solve my own problems but I'm stuck on this for over a month and everything I've tried doesn't work. Any help or guidance would be appreciated. I have a 2D Multiplayer platform game game using PUN2. When I play single player, player can shoot left or right and my projectile travels in the correct facing direction. When I add a 2nd player to the game, it doesn't matter if it's on the editor or a standalone build, the projectile will go in the direction of the other player, not the Master that Instantiated the projectile. To be as simple as possible, the transform of the projectile getting updated is based on the SpriteRenderer's FlipX component of the player. If FlipX is true, the X direction applies a product of -1 and 1 if FlipX is false. The Shoot command places the projectile in the correct position in front of the player direction. The projectile prefab is not a child of the player (should this be?). Here I'll show you my code so you see my code:

    PlayerController.cs (Shoot() method when player presses the fire button. This works as intended):
    [PunRPC]
    public void Shoot()
    {
    int dir = (flipX) ? -1 : 1;
    Vector3 weaponOffset = new Vector3(transform.position.x + dir * 1f, transform.position.y, transform.position.z);
    PhotonNetwork.Instantiate("Soda Rocket", weaponOffset, Quaternion.identity);
    shootTimer = myWeaponRefireSpeed;
    }


    WeaponProjectile.cs (class assigned to projectile prefab, this doesn't work):
    void Start()
    {
    if (!photonView.IsMine) return;
    direction = (PlayerController.i.flipX) ? -1 : 1;
    speed = PlayerController.i.myWeaponSpeed;
    strength = PlayerController.i.myWeaponStrength;
    ownerName = photonView.Owner.NickName;
    myRB.gravityScale = gravity;
    mySR.flipX = (direction == -1) ? true : false;
    }

    // Handle physics of projectile

    private void FixedUpdate()
    {
    if (!photonView.IsMine) return;
    transform.position = new Vector3(transform.position.x + (direction * speed * Time.deltaTime), transform.position.y, transform.position.z);
    }


    I also have an OnPhotonSerializeView() on this class:
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.IsWriting)
    {
    stream.SendNext(direction);
    }
    else
    {
    this.direction = (float)stream.ReceiveNext();
    }
    }
     
  2. jimkurth81

    jimkurth81

    Joined:
    Apr 19, 2020
    Posts:
    3
    Okay, after a month struggling with different ways to fix, this, I just solved it. And I knew that once I post a question in here, I would surely stumble upon the solution. So here is what I did and it makes sense:

    Because I am Instantiating the Weapon Projectile from the PhotonView owner and I set the direction in the Start() method, I do NOT need the OnPhotonSerializeView() in this class. Ugh, so simple and spent hours over hours trying to Instantiate to all players, send the player flipX across the network, and move the projectile over the network to all players, and all failed and it was just this SerializeView().

    Thanks anyone who responds to this and I hope this can help anyone else with this issue.