Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Photon Fusion: Player is despawning as soon as they are spawned in

Discussion in 'Multiplayer' started by BraedonHorizon, Feb 9, 2024.

  1. BraedonHorizon

    BraedonHorizon

    Joined:
    Aug 7, 2023
    Posts:
    22
    Hello, I am currently working on a 2d multiplayer platformer shooter and I was working on my project and all of the sudden when play testing my player despawns about half a second after being spawned in. I have looked through my code and can't find anything, I double checked that I was using a fusion server and the same thing still happens. I will provide my spawn code, player controller code and secondary player controller.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Fusion;
    5. using Unity.VisualScripting;
    6.  
    7.  
    8. public class Player_Script : NetworkBehaviour
    9. {
    10.     #region Variables
    11.     public Transform weapon;
    12.     public float ShootCooldown;
    13.     public bool ableToShoot = true;
    14.     public float radius;
    15.     public GameObject player;
    16.     public GameObject projectilePrefab;
    17.     public GameObject firePoint;
    18.     public float projectileSpeed = 40;
    19.     public SpriteRenderer sRend;
    20.     public Collider2D bulletCollider;
    21.     private Transform pivot;
    22.     #endregion
    23.     void Start()
    24.     {
    25.         ShootCooldown = 0.5f;
    26.         pivot = weapon.transform;
    27.         transform.parent = pivot;
    28.         transform.position += Vector3.up * radius;
    29.     }
    30.     #region Shooting
    31.     public  void Update()
    32.     {
    33.  
    34.         if (HasStateAuthority == false)
    35.         {
    36.             return;
    37.         }
    38.  
    39.  
    40.            
    41.  
    42.             AngleUpdate();
    43.             if (Input.GetMouseButtonDown(0))
    44.             {
    45.                 AngleUpdate();
    46.                 StartCoroutine("Shoot");
    47.                 print("Shot Bullet From Player");
    48.             }
    49.  
    50.  
    51.     }
    52.     public void AngleUpdate()
    53.     {
    54.         Vector3 weaponVector = Camera.main.WorldToScreenPoint(weapon.position);
    55.         weaponVector = Input.mousePosition - weaponVector;
    56.         float angle = Mathf.Atan2(weaponVector.y, weaponVector.x) * Mathf.Rad2Deg;
    57.  
    58.         pivot.position = weapon.position;
    59.         pivot.rotation = Quaternion.Euler(0f, 0f, angle - 90);
    60.     }
    61.  
    62.     public IEnumerator Shoot()
    63.     {
    64.         {
    65.             if (ableToShoot == true)
    66.             {
    67.  
    68.                 NetworkObject projGO = Runner.Spawn(projectilePrefab, pivot.position, pivot.rotation);
    69.                 projGO.transform.position = firePoint.transform.position;
    70.                 print(pivot.position);
    71.                 print(projGO.transform.position);
    72.                 Rigidbody2D rigidB = projGO.GetComponent<Rigidbody2D>();
    73.                 rigidB.velocity = pivot.up * projectileSpeed;
    74.                 ableToShoot = false;
    75.                 yield return new
    76.                 WaitForSeconds(ShootCooldown);
    77.                 ableToShoot = true;
    78.                 #endregion
    79.             }
    80.         }
    81.     }
    82.  
    83.  
    84. }
    Code (CSharp):
    1. using Fusion;
    2. using UnityEngine;
    3.  
    4. public class PlayerSpawner : SimulationBehaviour, IPlayerJoined
    5. {
    6.     public GameObject PlayerPrefab;
    7.     public int LobbyCount;
    8.  
    9.     public void PlayerJoined(PlayerRef player)
    10.     {
    11.         if (player == Runner.LocalPlayer)
    12.         {
    13.             Runner.Spawn(PlayerPrefab, new Vector3(0, 1, 0), Quaternion.identity);
    14.             print("player spawned");
    15.         }
    16.     }
    17. }
    Code (CSharp):
    1. using Fusion;
    2. using Photon.Realtime;
    3. using System.Collections;
    4. using System.Drawing;
    5. using System.Linq;
    6. using UnityEngine;
    7.  
    8. public class PlayerCotroller : NetworkBehaviour
    9. {
    10.     public TrailRenderer trailRender;
    11.     public float speed;
    12.     public float jump;
    13.     public Rigidbody2D rb;
    14.     float moveVelocity;
    15.     public bool isDead;
    16.     public SpriteRenderer spriteRenderer;
    17.     public Vector2[] spawnPointsList;
    18.     public void Start()
    19.     {
    20.         spriteRenderer.color = Random.ColorHSV();
    21.     }
    22.     //Grounded Vars
    23.     public bool isGrounded = true;
    24.  
    25.  
    26.     //Check if Grounded
    27.  
    28.     private void OnCollisionEnter2D(Collision2D collision)
    29.     {
    30.        
    31.         isGrounded = true;
    32.  
    33.         #region CheckingForBulletHit
    34.         if (collision.gameObject.tag == "Bullet")
    35.         {
    36.             {
    37.                 print("PlayerDead");
    38.                 gameObject.transform.position = new Vector2(100, 0);
    39.                 StartCoroutine("DieAndRespawn");
    40.  
    41.  
    42.             }
    43.         }
    44.  
    45.         #endregion
    46.  
    47.     }
    48.     private void Update()
    49.     {
    50.  
    51.         trailRender.startColor = spriteRenderer.color; // <---- Seting color to player color client side
    52.         if (HasStateAuthority == false)
    53.         {
    54.             return;
    55.  
    56.         }
    57.         #region Movement Code
    58.  
    59.         //Jumping
    60.  
    61.  
    62.         moveVelocity = 0;
    63.  
    64.         //Left Right Movement
    65.         if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
    66.         {
    67.             moveVelocity = -speed;
    68.         }
    69.         if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
    70.         {
    71.             moveVelocity = speed;
    72.         }
    73.         if (Input.GetKey(KeyCode.S))
    74.         {
    75.             rb.velocity = Vector2.Lerp(rb.velocity, new Vector2(rb.velocity.y, -jump), 0.1f);
    76.         }
    77.         rb.velocity = Vector2.Lerp(rb.velocity, new Vector2(moveVelocity, rb.velocity.y), 0.1f);
    78.  
    79.         if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
    80.         {
    81.             if (isGrounded)
    82.             {
    83.                 print("player is grounded");
    84.                 rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y + jump);
    85.                 isGrounded = false;
    86.             }
    87.         }
    88.     }
    89.     #endregion
    90.  
    91.  
    92.  
    93.     IEnumerator DieAndRespawn()
    94.     {
    95.  
    96.         gameObject.transform.position = new Vector2(500, 0);
    97.         yield return new
    98.         WaitForSeconds(5);
    99.         Vector2 SpawnPoint = spawnPointsList[Random.Range(0, spawnPointsList.Length)];
    100.     }
    101. }
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,057
    Are you maybe re/loading a scene?
     
  3. BraedonHorizon

    BraedonHorizon

    Joined:
    Aug 7, 2023
    Posts:
    22
    No, I don't think so, I did find a work around by creating a new player prefab and seemed to just work? Not quite sure what happened. It appears that now Fusion can't spawn the player in build mode, and sometimes playmode doesn't work either. I am using the usw for the server connection.

    The Error Code:
    [Fusion/ [9:00:49 AM] ]</color> StartGame Failed: [StartGameResult: Ok:False, ShutdownReason: Error, ErrorMessage=DnsExceptionOnConnect, StackTrace= at Fusion.CloudServices.ConnectToCloud (Fusion.Photon.Realtime.AppSettings appSettings, Fusion.Photon.Realtime.AuthenticationValues authentication, System.Threading.CancellationToken externalCancellationToken, System.Nullable`1[T] useDefaultCloudPorts) [0x000ef] in Fusion\Fusion.Runtime\CloudServices\CloudServices.cs:226
    at Fusion.NetworkRunner.StartGameModeCloud (Fusion.StartGameArgs args) [0x0027f] in Fusion\Fusion.Runtime\Runner\Startup\NetworkRunner.Startup.cs:386 ]
    UnityEngine.StackTraceUtility:ExtractStackTrace () (at C:/build/output/unity/unity/Runtime/Export/Scripting/StackTrace.cs:37)
    UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])

    Fusion Network Settings:
    upload_2024-2-13_9-20-40.png upload_2024-2-13_9-21-18.png
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,057
    The DnsExceptionOnConnect from the log looks suspicious. This seems to prevent the client from connecting in the first place (and then it doesn't spawn anything for sure).
    Maybe your DNS is causing issues? Is it maybe blocking some addresses? We've been on random / too strict block lists in some cases (for unknown reasons). Or could your network have temporary issues?
    Waiting a second before trying once more in such cases is probably a good enough strategy.
     
  5. BraedonHorizon

    BraedonHorizon

    Joined:
    Aug 7, 2023
    Posts:
    22
    I doesn't look like the issue will resolve itself, the issue is continuing to persist. What do you mean by DNS issues? All of the photon servers seem to be working, although I did notice that my CCU somehow reached -1? Im not sure how that works, but could that have caused the issue?
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,057
    CCU being -1 is a problem with the counters. This is being worked on.

    Some people run a DNS service like PiHole or use a DNS server entry, which blocks some connections (ads, whatever).
    Your device might be off or having a bad connection at that point.

    It makes sense to test in another network and or test one of our demos with your AppId.
     
  7. BraedonHorizon

    BraedonHorizon

    Joined:
    Aug 7, 2023
    Posts:
    22
    Ok, I will try to host the program at home, lets hope that fixes the issue, thank you for your help!