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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fusion;
using Unity.VisualScripting;
public class Player_Script : NetworkBehaviour
{
#region Variables
public Transform weapon;
public float ShootCooldown;
public bool ableToShoot = true;
public float radius;
public GameObject player;
public GameObject projectilePrefab;
public GameObject firePoint;
public float projectileSpeed = 40;
public SpriteRenderer sRend;
public Collider2D bulletCollider;
private Transform pivot;
#endregion
void Start()
{
ShootCooldown = 0.5f;
pivot = weapon.transform;
transform.parent = pivot;
transform.position += Vector3.up * radius;
}
#region Shooting
public void Update()
{
if (HasStateAuthority == false)
{
return;
}
AngleUpdate();
if (Input.GetMouseButtonDown(0))
{
AngleUpdate();
StartCoroutine("Shoot");
print("Shot Bullet From Player");
}
}
public void AngleUpdate()
{
Vector3 weaponVector = Camera.main.WorldToScreenPoint(weapon.position);
weaponVector = Input.mousePosition - weaponVector;
float angle = Mathf.Atan2(weaponVector.y, weaponVector.x) * Mathf.Rad2Deg;
pivot.position = weapon.position;
pivot.rotation = Quaternion.Euler(0f, 0f, angle - 90);
}
public IEnumerator Shoot()
{
{
if (ableToShoot == true)
{
NetworkObject projGO = Runner.Spawn(projectilePrefab, pivot.position, pivot.rotation);
projGO.transform.position = firePoint.transform.position;
print(pivot.position);
print(projGO.transform.position);
Rigidbody2D rigidB = projGO.GetComponent<Rigidbody2D>();
rigidB.velocity = pivot.up * projectileSpeed;
ableToShoot = false;
yield return new
WaitForSeconds(ShootCooldown);
ableToShoot = true;
#endregion
}
}
}
}
using Fusion;
using UnityEngine;
public class PlayerSpawner : SimulationBehaviour, IPlayerJoined
{
public GameObject PlayerPrefab;
public int LobbyCount;
public void PlayerJoined(PlayerRef player)
{
if (player == Runner.LocalPlayer)
{
Runner.Spawn(PlayerPrefab, new Vector3(0, 1, 0), Quaternion.identity);
print("player spawned");
}
}
}
using Fusion;
using Photon.Realtime;
using System.Collections;
using System.Drawing;
using System.Linq;
using UnityEngine;
public class PlayerCotroller : NetworkBehaviour
{
public TrailRenderer trailRender;
public float speed;
public float jump;
public Rigidbody2D rb;
float moveVelocity;
public bool isDead;
public SpriteRenderer spriteRenderer;
public Vector2[] spawnPointsList;
public void Start()
{
spriteRenderer.color = Random.ColorHSV();
}
//Grounded Vars
public bool isGrounded = true;
//Check if Grounded
private void OnCollisionEnter2D(Collision2D collision)
{
isGrounded = true;
#region CheckingForBulletHit
if (collision.gameObject.tag == "Bullet")
{
{
print("PlayerDead");
gameObject.transform.position = new Vector2(100, 0);
StartCoroutine("DieAndRespawn");
}
}
#endregion
}
private void Update()
{
trailRender.startColor = spriteRenderer.color; // <---- Seting color to player color client side
if (HasStateAuthority == false)
{
return;
}
#region Movement Code
//Jumping
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
moveVelocity = -speed;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveVelocity = speed;
}
if (Input.GetKey(KeyCode.S))
{
rb.velocity = Vector2.Lerp(rb.velocity, new Vector2(rb.velocity.y, -jump), 0.1f);
}
rb.velocity = Vector2.Lerp(rb.velocity, new Vector2(moveVelocity, rb.velocity.y), 0.1f);
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
{
if (isGrounded)
{
print("player is grounded");
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y + jump);
isGrounded = false;
}
}
}
#endregion
IEnumerator DieAndRespawn()
{
gameObject.transform.position = new Vector2(500, 0);
yield return new
WaitForSeconds(5);
Vector2 SpawnPoint = spawnPointsList[Random.Range(0, spawnPointsList.Length)];
}
}