Advertisement
Vlad-00003

[RUST] Custom IsVisible check with an option to include certain objects

Dec 25th, 2021 (edited)
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. private static class Constants
  2. {
  3.         public static readonly string[] TopTierEntities =
  4.     {
  5.         "assets/prefabs/building/door.hinged/door.hinged.toptier.prefab",
  6.         "assets/prefabs/building/door.double.hinged/door.double.hinged.toptier.prefab",
  7.         "assets/prefabs/building/wall.window.bars/wall.window.bars.toptier.prefab",
  8.         //"assets/prefabs/building/wall.window.reinforcedglass/wall.window.glass.reinforced.prefab"
  9.     };
  10. }
  11.  
  12. public static bool IsVisible(BasePlayer player, Vector3 position, bool onlyTopTier = false)
  13. {
  14.     Vector3 a = player.eyes.position - position;
  15.     float magnitude = a.magnitude;
  16.     if (magnitude < Mathf.Epsilon)
  17.         return true;
  18.  
  19.     Vector3 vector = a / magnitude;
  20.     Vector3 b = vector * Mathf.Min(magnitude, 0.01f);
  21.     var ray = new Ray(position + b, vector);
  22.  
  23.     RaycastHit playerToPos;
  24.     if (ray.origin.IsNaNOrInfinity() || ray.direction.IsNaNOrInfinity() || ray.direction == Vector3.zero ||
  25.         !player.WorldSpaceBounds().Trace(ray, out playerToPos))
  26.         return false;
  27.  
  28.     RaycastHit hit;
  29.     if (!GamePhysics.Trace(ray, 0f, out hit, float.PositiveInfinity, onlyTopTier ? 2097152 : 1218519041))
  30.         return true;
  31.  
  32.     if (hit.distance > playerToPos.distance)
  33.         return true;
  34.  
  35.     if (!onlyTopTier)
  36.         return false;
  37.  
  38.     var ent = hit.GetEntity();
  39.     if (Array.Exists(Constants.TopTierEntities, x => x == ent.PrefabName))
  40.         return false;
  41.  
  42.     var buildingBlock = ent as BuildingBlock;
  43.     return buildingBlock == null || buildingBlock.grade != BuildingGrade.Enum.TopTier;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement