Hey All,
I'm having problems trying to find all players near a respawn position. The basic workflow I want to achieve is:
OnPlayerRespawn
Retrieve a List of Spawns
Check Random Spawn if players are nearby(this distance is yet to be decided but I'm mainly after 1foundation's distance)
If player is near spawn go back to Retrieve a List of Spawns(if attempts = 10 just pick a random spawn and go for it)
Spawn player.
I'm able to handle writing all the logic and presenting a Vector3 of the spawn positions, I'm just having a hard time finding the Vector3 of players within a radius of the spawn location
Any help is appreciated, thanks in advance!
Finding players near a position?
Discussion in 'Rust Development' started by pinkstink, Dec 13, 2017.
-
Wulf Community Admin
Unity's Spherecast should handle what you need.
-
Cheers for that, I've got a working example now, but I'm trying to narrow it down to the a Player LayerMask. Would you happen to know how to specify that? I've Got:
Code:private object GetSpawnPoint(BasePlayer player, int attempt = 0) { if (attempt >= 10) { PrintWarning("Unable to find free spawn, spawning random"); return (Vector3)ZoneTracking[PlayerTracking[player.userID].activeZone].spawns.GetRandom(); } bool objs; Vector3 random = new List<Vector3>(ZoneTracking[PlayerTracking[player.userID].activeZone].spawns); Vector3 position = random.GetRandom(); objs = Physics.CheckSphere(position, 5f); PrintWarning(objs.ToString()); if (objs) { PrintWarning("Going Again"); return GetSpawnPoint(player, ++attempt); } PrintWarning("Returning Vector"); return (Vector3)position; }
I've also tried scouring through the Assembly-CSharp.dll but I'm either blind or it's not in there :/ -
Code:
private class ProximityDetector : MonoBehaviour { private BasePlayer _player; private void Awake() { _player = GetComponent<BasePlayer>(); InvokeRepeating("CheckProximity", 0, _instance._configuration.CheckTimer); } private void CheckProximity() { var colliders = Physics.OverlapSphere(_player.transform.position, _instance._configuration.CheckDistance, _instance._playerLayer) .Select(x => x.GetComponentInParent<BasePlayer>()) .Where(x => x != null); foreach (var collider in colliders) { var target = collider.GetComponentInParent<BasePlayer>(); if (target == null) continue; _instance.PrintToChat(target, "Hello!"); } } public void Deactivate() { CancelInvoke("CheckProximity"); Destroy(this); } }_playerLayer = LayerMask.GetMask("Player (Server)");
-
_playerLayer = LayerMask.GetMask("Player (Server)");
That one line it the money shot! Thanks Very Much!
I actually managed to somehow guess it. I literally opened up calculator, knowing the mask will be divisible by 2. Went 2*2*2.......... until I randomly stopped at 131072.... and what would you bloody know... that is the player layer.......
Thanks all once again!