Skip to content

Commit

Permalink
Replace faulty network instantiation with networked position
Browse files Browse the repository at this point in the history
  • Loading branch information
joebinns committed Mar 10, 2024
1 parent 534c25d commit 47308b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
14 changes: 8 additions & 6 deletions FGNetworking/Assets/Scripts/AmmoPacks/StandardAmmoPack.cs
Expand Up @@ -16,16 +16,18 @@ private void OnTriggerEnter2D(Collider2D other)

ammo.GainAmmo(AMMO);

var spawnPosition = new Vector3(
var newPosition = new Vector3(
Random.Range(-4f, 4f),
Random.Range(-2f, 2f),
0f);

var newAmmoPack = Instantiate(ammoPackPrefab, spawnPosition, Quaternion.identity);
var newNetworkObject = newAmmoPack.GetComponent<NetworkObject>();
newNetworkObject.Spawn();
transform.position = newPosition;
SetPositionClientRpc(newPosition);
}

var networkObject = gameObject.GetComponent<NetworkObject>();
networkObject.Despawn();
[ClientRpc]
private void SetPositionClientRpc(Vector3 newPosition)
{
transform.position = newPosition;
}
}
20 changes: 12 additions & 8 deletions FGNetworking/Assets/Scripts/Mines/StandardMine.cs
Expand Up @@ -13,15 +13,19 @@ void OnTriggerEnter2D(Collider2D other)
if (!health) return;
health.LoseHealth(25);

int xPosition = Random.Range(-4, 4);
int yPosition = Random.Range(-2, 2);
var newPosition = new Vector3(
Random.Range(-4f, 4f),
Random.Range(-2f, 2f),
0f);

GameObject newMine = Instantiate(minePrefab, new Vector3(xPosition, yPosition, 0), Quaternion.identity);
NetworkObject no = newMine.GetComponent<NetworkObject>();
no.Spawn();

NetworkObject networkObject = gameObject.GetComponent<NetworkObject>();
networkObject.Despawn();
transform.position = newPosition;
SetPositionClientRpc(newPosition);
}
}

[ClientRpc]
private void SetPositionClientRpc(Vector3 newPosition)
{
transform.position = newPosition;
}
}

0 comments on commit 47308b4

Please sign in to comment.