Started make greybox for main location
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
public sealed class PlayerSpawnController : NetworkBehaviour, IPlayerSystem
|
||||
{
|
||||
[Header("Random spawn area")]
|
||||
[SerializeField] private Vector3 spawnCenter = new(0f, 1f, 0f);
|
||||
[SerializeField, Min(0f)] private float spawnRadius = 8f;
|
||||
|
||||
private readonly NetworkVariable<Vector3> assignedSpawnPosition = new(
|
||||
Vector3.zero,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server);
|
||||
|
||||
public void Initialize(PlayerFacade facade) { }
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
assignedSpawnPosition.OnValueChanged += HandleSpawnPositionChanged;
|
||||
|
||||
if (IsServer)
|
||||
{
|
||||
Vector2 randomOffset = Random.insideUnitCircle * spawnRadius;
|
||||
assignedSpawnPosition.Value = spawnCenter + new Vector3(randomOffset.x, 0f, randomOffset.y);
|
||||
}
|
||||
|
||||
ApplySpawnPosition(assignedSpawnPosition.Value);
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
assignedSpawnPosition.OnValueChanged -= HandleSpawnPositionChanged;
|
||||
}
|
||||
|
||||
private void HandleSpawnPositionChanged(Vector3 _, Vector3 position) =>
|
||||
ApplySpawnPosition(position);
|
||||
|
||||
private void ApplySpawnPosition(Vector3 position)
|
||||
{
|
||||
if (IsOwner)
|
||||
transform.position = position;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user