Hey
I'm trying to figure out how to check if a player is in range of their cupboard.
I have a list called ProtectedCupboard which contains the player as type BasePlayer and the cupboard as type BaseEntity. Is there a way I can check if the player is within the range of that cupboard?
Something like:Thanks!Code:if (ProtectedCupboard[player].GetComponent<BuildingPriviledge>.PlayersInRange.Contains(player)
Check if player is in cupboard range
Discussion in 'Rust Development' started by PsychoTea, Aug 24, 2016.
-
Wulf Community Admin
You can check if they are in a building privilege zone, but not sure if that would help get who the main user for that cupboard is.
-
I already have a system in place to track the Tool Cupboards and who owns them, I just need to find a way to check if a player is in Tool Cupboard range and get the Tool Cupboard that they're in range of as a BaseEntity.
-
Code:FieldInfo _buildingPrivilege = typeof(BasePlayer).GetField("buildingPrivilege", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));List<BuildingPrivlidge> buildingPrivilege = (List<BuildingPrivlidge>)_buildingPrivilege.GetValue(entity as BasePlayer); foreach (var cup in buildingPrivilege) Debug.Log(cup as BaseEntity);
-
Thankyou! I'm now using this code which (I hope) should work:
Code:if (ProtectedCupboard.ContainsKey(HitPlayer)) { FieldInfo _buildingPrivilege = typeof(BasePlayer).GetField("buildingPrivilege", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic)); List<BuildingPrivlidge> buildingPrivilege = (List<BuildingPrivlidge>)_buildingPrivilege.GetValue(entity as BasePlayer); foreach (var cup in buildingPrivilege) { BaseEntity c = cup as BaseEntity; if (c == ProtectedCupboard[HitPlayer]) { //they are protected info.damageTypes = new DamageTypeList(); info.HitMaterial = 0; info.PointStart = Vector3.zero; BasePlayer AttackingPlayer = info.Initiator as BasePlayer; SendReply(AttackingPlayer, "This person is currently protected by starter protection for x hours."); } } }
-
Code:// UnityEngine.Physics /// <summary> /// <para>Computes and stores colliders touching or inside the sphere into the provided buffer.</para> /// </summary> /// <param name="position">Center of the sphere.</param> /// <param name="radius">Radius of the sphere.</param> /// <param name="results">The buffer to store the results into.</param> /// <param name="layerMask">A that is used to selectively ignore colliders when casting a ray.</param> /// <param name="queryTriggerInteraction">Specifies whether this query should hit Triggers.</param> /// <returns> /// <para>The amount of colliders stored into the results buffer.</para> /// </returns> public static int OverlapSphereNonAlloc(Vector3 position, float radius, Collider[] results, [DefaultValue("AllLayers")] int layerMask, [DefaultValue("QueryTriggerInteraction.UseGlobal")] QueryTriggerInteraction queryTriggerInteraction) { return Physics.INTERNAL_CALL_OverlapSphereNonAlloc(ref position, radius, results, layerMask, queryTriggerInteraction); }
-
I got this error in console when adding that function to my plugin:
Code:error CS0117: 'UnityEngine.Physics' does not contain a definition for 'INTERNAL_CALL_OverlapSphereNonAlloc'
I've written this code:
Code:if (entity is BuildingBlock && info.Initiator is BasePlayer) { BasePlayer AttackingPlayer = info.Initator as BasePlayer; BuildingBlock AttackedBuildingBlock = entity as BuildingBlock; List<Collider> hitColliders = Physics.OverlapSphere(entity.CenterPoint(), 25f).ToList(); if (hitColliders.Contains(AttackedBuildingBlock.GetComponent<Collider>)) { SendReply(AttackingPlayer, "This building is protected by starter protection for x hours."); } }
-
Nevermind - I fixed it.
I ended up just getting the position of the cabinet and the position of the building block, and then checking if they were within a radius of 25 (the TC radius) using Vector3.Distance.