Can anyone help me with a code example of how to detect the player being on the ground in C# ?
Detecting if player is on ground? (C#)
Discussion in 'Rust Development' started by isuelt, Mar 6, 2016.
-
Pseudo code:
Code:BasePlayer player; if (player.IsOnGround()) { //do something }
-
Code:Puts($"player.IsOnGround() = {player.IsOnGround().ToString()}");
-
-
I don't really know how to, "ray cast downwards from the player position and determine on what he is standing on" -
Code:
bool IsOnTerrain(BasePlayer player) { if (!player.IsOnGround()) return false; // if the player isn't grounded we don't need to do anything special RaycastHit hit; // if he is grounded we need to raycast down and if we hit something (which we should anyways, since we are grounded) output the raycast hit into hit if (Physics.Raycast(player.transform.position, Vector3.down, out hit)) { // now that we hit something we can further determine what we hit and act accordingly // we determine whether we hit the "Terrain" (actual ground) layer or the "World" (rocks, radtown buildings, etc.) layer if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Terrain") || hit.collider.gameObject.layer == LayerMask.NameToLayer("World")) { return true; // if we indeed hit either the "Terrain" or the "World" layer we return true } } return false; // if we didn't yet return a value we return false }
I used the following resources to put the code above together:
To get the names of the LayerMasks Rust uses I looked at this plugin:
I'm aware that there are other ways to acquire the names of the LayerMasks Rust uses but it was the quickest for me.
To get more information on how to properly work with LayerMasks and RayCasts I used:
- Unity - Scripting API:
- Unity - Scripting API:
- Unity - Scripting
API:
- Unity - Scripting API:
- How to tell if my character hit a collider of a certain layer - Unity Answers
- Unity - Scripting API:
-
AWESOME ! Thanks!
-
Code:bool IsOnTerrain(BasePlayer player) { return !(!player.IsOnGround() || Physics.Raycast(player.transform.position, Vector3.down, Mathf.Infinity, ~LayerMask.GetMask("Terrain", "World", "Water"))); }
-
Is there another way to detect if a player is in the Radtown.
-
-
Code:bool IsOnTerrain(BasePlayer player) { if (!player.IsOnGround()) return false; RaycastHit hit; if (Physics.Raycast(player.transform.position, Vector3.down, out hit)) { if (hit.collider.name.Equals("Terrain")) { return true; } } return false; }
Last edited by a moderator: Mar 7, 2016 -
It won't work for Radtown buildings either because their "hit.collider.name" is specific to the actual building, e.g. "industrial_bld_j" or "sewer_drainwall_1800".
[DOUBLEPOST=1457367278][/DOUBLEPOST]Because Facepunch recently started to keep track of the OwnerIDs of Entitys, we can check if the OwnerID of something we hit is not 0 (meaning it's probably assigned to a player and hence built by a player) and that way determine if people are on Terrain or not. This fixes the Radtown issue.
Code:bool IsOnTerrain(BasePlayer player) { if (!player.IsOnGround()) return false; RaycastHit hit; if (Physics.Raycast(player.transform.position + player.GetOffset(), Vector3.down, out hit, player.GetHeight(), ~LayerMask.GetMask("Terrain", "World", "Water"))) { if (hit.GetEntity() != null && hit.GetEntity().OwnerID != 0) { return false; } } return true; }
Last edited by a moderator: Mar 7, 2016 -
Also, if somebody is need:
Code:bool isOnRT(Vector3 position) { var allColliders = Physics.OverlapSphere(position, 0); foreach (Collider collider in allColliders) { if (collider.name.Contains("prevent_building") && collider.gameObject.ToBaseEntity() == null) return true; } return false; }
-
Great thanks !