public void SetRaycastLayer(params string[] layerNames) { if (layerNames.Length == 0) { this.raycastLayerMask = null; } this.raycastLayerMask = LayerMask.GetMask(layerNames); }
/// <summary> /// Gets the game objects under the given world position, ordered so that highest item comes first. /// /// The top-level matrix gameobject (the one with InteractableTiles) at this point is included at the end (if any of its tilemap gameobjects were at this point) /// </summary> /// <param name="worldPoint">world point to check</param> /// <param name="layerMask">layers to check for hits in. If left null, will use DefaultInteractionLayerMask (basically includes every layer /// that can have interactable things).</param> /// <param name="gameObjectFilter">optional filter to filter out game objects prior to sorting and checking for pixel hits, can improve performance /// by shrinking the amount of sorting and pixel checking that needs to be done. Func should return true if should include the gameobject, otherwise false. /// Be aware that the GameObject passed to this function will be the one that the SpriteRenderer or TilemapRenderer lives on, which may NOT /// be the "root" of the gameobject this renderer lives on.</param> /// <returns>the ordered game objects that were under the mouse, top first</returns> public static IEnumerable<GameObject> GetOrderedObjectsAtPoint(Vector3 worldPoint, LayerMask? layerMask = null, Func<GameObject,bool> gameObjectFilter = null) { worldPoint.z = 0; var matrix = MatrixManager.AtPoint(Vector3Int.RoundToInt(worldPoint), CustomNetworkManager.Instance._isServer).Matrix; if (!matrix) { return new List<GameObject>(); } var tilePosition = Vector3Int.FloorToInt(worldPoint.ToLocal()); List<RegisterTile> resultRegisterTile = new List<RegisterTile>(); //humm probably there's a better way of doing this resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition, false).ToList()); resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition+Vector3Int.up, false).ToList()); resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition+Vector3Int.down, false).ToList()); resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition+Vector3Int.right, false).ToList()); resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition+Vector3Int.left, false).ToList()); resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition+Vector3Int.up+Vector3Int.right, false).ToList()); resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition+Vector3Int.up+Vector3Int.left, false).ToList()); resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition+Vector3Int.down+Vector3Int.right, false).ToList()); resultRegisterTile.AddRange(matrix.GetRegisterTile(tilePosition+Vector3Int.down+Vector3Int.left, false).ToList()); var result = resultRegisterTile.Select(x => x.gameObject); var IInteractableTiles = matrix.GetComponentInParent<InteractableTiles>().gameObject; // var result = Physics2D.RaycastAll(worldPoint, Vector2.zero, 10f, // layerMaskToUse) // failsafe - exclude hidden / despawned things in case they happen to mouse over hiddenpos // .Where(hit => !hit.collider.gameObject.IsAtHiddenPos()) // get the hit game object // .Select(hit => hit.collider.gameObject); if (gameObjectFilter != null) { result = result.Where(gameObjectFilter); } return result //check for a pixel hit .Select(go => IsPixelHit(go.transform)) .Where(r => r != null) //order by sort layer .OrderByDescending(r => SortingLayer.GetLayerValueFromID(r.sortingLayerID)) //then by sort order .ThenByDescending(renderer => renderer.sortingOrder) //get the "parent" game object of each of the hit renderers //for a sprite renderer, the parent is the object that has a RegisterTile. //for a tilemap renderer, the parent is the oject that has a Matrix .Select(r => r is TilemapRenderer ? r.GetComponentInParent<InteractableTiles>().gameObject : r.GetComponentInParent<RegisterTile>().gameObject) //each gameobject should only show up once .Distinct().Append(IInteractableTiles); }
public static Collider GetObject(Vector3?screenPos = null, LayerMask?layer = null, Camera camera = null) { if (_Instance == null) { _Instance = new GameObject().AddComponent <Mouse3D>(); } return(_Instance.InternalGetObject(screenPos, layer, camera)); }
private static void EnsureInit() { if (defaultInteractionLayerMask == null) { defaultInteractionLayerMask = LayerMask.GetMask("Furniture", "Walls", "Windows", "Machines", "Unshootable Machines", "Players", "Items", "Door Open", "Door Closed", "WallMounts", "HiddenWalls", "Objects", "Matrix", "Floor", "NPC", "Lighting"); } }
public static bool MoveToGround(this GameObject obj, LayerMask?mask = null) { Bounds?bounds = null; var colliders = obj.GetComponentsInChildren <Collider>(true); for (var i = 0; i < colliders.Length; i++) { if (colliders[i].isTrigger) { continue; } if (!bounds.HasValue) { bounds = colliders[i].bounds; } else { bounds.Value.Encapsulate(colliders[i].bounds); } } if (!bounds.HasValue) { var renderers = obj.GetComponentsInChildren <Renderer>(true); for (var i = 0; i < renderers.Length; i++) { if (!bounds.HasValue) { bounds = renderers[i].bounds; } else { bounds.Value.Encapsulate(renderers[i].bounds); } } } if (bounds.HasValue) { mask = mask.HasValue ? mask : Physics.DefaultRaycastLayers; var allHits = Physics.RaycastAll(obj.transform.position + Vector3.up * 0.1f, Vector3.down, 1000f, mask.Value); Array.Sort(allHits, (r1, r2) => r1.distance.CompareTo(r2.distance)); for (var i = 0; i < allHits.Length; i++) { if (!allHits[i].transform.IsChildOf(obj.transform)) { obj.transform.position = allHits[i].point + Vector3.up * (bounds.Value.extents.y - 0.1f); return(true); } } } return(false); }
public void Push(IntPtr L, LayerMask?n) { if (n == null) { LuaDLL.lua_pushnil(L); } else { LuaDLL.tolua_pushlayermask(L, n.Value); } }
public static Vector3 GetPos(Vector3?screenPos = null, LayerMask?layer = null, Camera camera = null) { if (_Instance == null) { _Instance = new GameObject().AddComponent <Mouse3D>(); } var pos = _Instance.InternalGetPos(screenPos, layer, camera); return(pos); }
/// <summary> /// Determines whether the actor is on the ground, on top of a collider using raycast /// </summary> /// <returns> /// <c>true</c> if the actor is on top of a collider; otherwise, <c>false</c>. /// </returns> /// <param name="actor">The actor.</param> /// <param name="layer">The layer mask</param> public static bool IsGrounded(this Collider actor, LayerMask?layer = null) { if (null == layer) { return(Physics.Raycast(actor.transform.position, -Vector3.up, actor.bounds.extents.y + 0.1f)); } return(Physics.Raycast(actor.transform.position, -Vector3.up, actor.bounds.extents.y + 0.1f, layer.Value)); }
public static JSValue js_push_structvalue(JSContext ctx, LayerMask?o) { if (o == null) { return(JSApi.JS_NULL); } var proto = FindPrototypeOf <LayerMask>(ctx); JSValue val = JSApi.jsb_new_bridge_value(ctx, proto, sizeof(int) * 1); JSApi.jsb_set_int_1(val, ((LayerMask)o).value); return(val); }
public static bool js_get_structvalue(JSContext ctx, JSValue val, out LayerMask?o) { if (val.IsNullish()) { o = null; return(true); } int pres; JSApi.JS_ToInt32(ctx, out pres, val); o = (LayerMask)pres; // no check return(true); }
/// <returns>Returns number of hits</returns> public static int RaycastFromScreenpointAll(this Camera cam, Vector2 screenPoint, RaycastHit[] hits, float range = 1000f, LayerMask?mask = null) { Ray ray = cam.ScreenPointToRay(screenPoint); int hitCount; if (mask.HasValue) { hitCount = Physics.RaycastNonAlloc(ray, hits, range, (LayerMask)mask); } else { hitCount = Physics.RaycastNonAlloc(ray, hits, range); } return(hitCount); }
/// <summary> /// Determines whether the actor is squeezed between two colliders using raycast /// </summary> /// <param name="actor">The actor.</param> /// <param name="layer">The layer mask</param> /// <returns> /// <c>true</c> if the specified actor is squashed; otherwise, <c>false</c>. /// </returns> public static bool IsSquashed(this Collider actor, LayerMask?layer = null) { bool top; if (null == layer) { top = Physics.Raycast(actor.transform.position, Vector3.up, actor.bounds.extents.y + 0.1f); } else { top = Physics.Raycast(actor.transform.position, Vector3.up, actor.bounds.extents.y + 0.1f, layer.Value); } return(top && actor.IsGrounded(layer)); }
/// <summary> /// 检测攻击 /// </summary> /// <param name="skillType">技能类型</param> /// <param name="WeaponType">武器类型</param> /// <param name="attackSpeed">攻击速度</param> /// <param name="attackLayerMask">内部设置的检测层</param> /// <param name="CheckResultAction">检测到碰撞对象后的回调</param> /// <param name="otherCheck">其他的检测</param> public void CheckAttack(EnumSkillType skillType, EnumWeaponTypeByPlayerState WeaponType, float attackSpeed, LayerMask?attackLayerMask, Func <int, GameObject, bool> CheckResultAction, int otherCheck = 0) { this.CheckResultAction = CheckResultAction; this.AttackSpeed = attackSpeed; if (attackLayerMask != null) { tempAttackLayerMask = attackLayerMask.Value; } else { tempAttackLayerMask = null; } if (checkStructCollectionArray != null) { checkStructCollection = checkStructCollectionArray .Where(temp => temp.SkillType == skillType) //判断技能类型 .Where(temp => temp.WeaponType == WeaponType || temp.WeaponType == EnumWeaponTypeByPlayerState.None) //判断武器类型(如果设置的是None表示什么武器都可以) .Where(temp => (temp.OtherCheck == 0 || otherCheck == 0) ? true : temp.OtherCheck == otherCheck) //判断是否存在其他的检测,并判断是否通过检测 .FirstOrDefault(); } else { checkStructCollection = null; } if (checkStructCollection != null) { //进行设置 checkStructCollection.CheckStructs.ToList().ForEach(temp => { temp.Collider.gameObject.SetActive(true);//打开检测对象 //查找检测脚本 PhysicSkillInjuryDetection_Check physicSkillInjuryDetection_Check = temp.Collider.gameObject.GetComponent <PhysicSkillInjuryDetection_Check>(); //如果为空则添加 if (physicSkillInjuryDetection_Check == null) { physicSkillInjuryDetection_Check = temp.Collider.gameObject.AddComponent <PhysicSkillInjuryDetection_Check>(); } //设置检测层 physicSkillInjuryDetection_Check.checkMask = tempAttackLayerMask != null ? tempAttackLayerMask.Value : this.attackLayerMask; }); } nowCheckTime = 0; skillOrderToObjList = new Dictionary <int, List <GameObject> >(); }
public static bool RaycastFromScreenpoint(Ray ray, out RaycastHit hit, float range = 1000f, LayerMask?mask = null, bool preventUiIntersecting = false) { bool hasHit; if (mask.HasValue) { hasHit = Physics.Raycast(ray, out hit, range, (LayerMask)mask); } else { hasHit = Physics.Raycast(ray, out hit, range); } if (!preventUiIntersecting) { return(hasHit); } return(hasHit && !EventSystem.current.IsPointerOverGameObject()); }
public static Bounds GetCompositeBoundingBounds(this GameObject go, LayerMask?ignore = null) { var boundsInObject = go.GetComponentsInChildren <Renderer>(true) .Where(r => ignore == null || ignore.Value.Contains(r.gameObject.layer) == false) .Where(r => r is ParticleSystemRenderer == false) .Select(r => r.bounds) .Where(bounds => bounds.min != bounds.max) .ToArray(); if (boundsInObject.Length == 0) { return(new Bounds()); } return(boundsInObject .Aggregate((a, b) => { a.Encapsulate(b); return a; })); }
public static Vector3 GetMousePosition3D(Camera?camera = null, LayerMask?layerMask = null) { Camera cam = (camera == null) ? Camera.main : camera; Ray ray = cam.ScreenPointToRay(Input.mousePosition); if (layerMask == null) { if (Physics.Raycast(ray, out RaycastHit raycastHit)) { return(raycastHit.point); } } else { if (Physics.Raycast(ray, out RaycastHit raycastHit, cam.farClipPlane, (LayerMask)layerMask)) { return(raycastHit.point); } } return(Vector3.zero); }
private static RaycastHit?Raytate(Vector3 origin, Vector3 startVec, Vector3 targetVec, float maxDegreesDelta, LayerMask?layerMask = null) { var dir = Vector3.RotateTowards(startVec, targetVec, maxDegreesDelta * Mathf.Deg2Rad, 0); var ray = new Ray(origin, dir); // A LayerMask of int.MaxValue will hit everything if (Physics.Raycast(ray, out var hit, float.MaxValue, layerMask.GetValueOrDefault(int.MaxValue))) { Debug.DrawLine(ray.origin, hit.point, Color.green); return(hit); } // Reaching here means we didn't hit anything Debug.DrawRay(ray.origin, ray.direction * 15, Color.red); return(null); }
public static CustomPhysicsHit RayCast(Vector3 Worldorigin, Vector2 direction, float distance, LayerTypeSelection layerMask, LayerMask?Layermask2D = null, Vector3?WorldTo = null) { Worldorigin.z = 0; //TODO RRT //get to from vector CustomPhysicsHit?ClosestHit = null; CustomPhysicsHit?Checkhit = null; // Vector3 Localdorigin = Vector3.zero; // Vector3 LocalTo = Vector3.zero; if (WorldTo == null) { WorldTo = Worldorigin + (Vector3)(direction.normalized * distance); } if (direction.x == 0 && direction.y == 0) { direction = (WorldTo.Value - Worldorigin).normalized; } if (layerMask != LayerTypeSelection.None) { for (var i = Instance.ActiveMatrices.Count - 1; i >= 0; i--) { MatrixInfo mat = Instance.ActiveMatrices[i]; //if (mat.Matrix == Instance.spaceMatrix) continue; if (LineIntersectsRect(Worldorigin, (Vector2)WorldTo, mat.WorldBounds)) { Checkhit = mat.MetaTileMap.Raycast(Worldorigin.ToLocal(mat.Matrix), Vector2.zero, distance, layerMask, WorldTo.Value.ToLocal(mat.Matrix)); if (Checkhit != null) { if (ClosestHit != null) { if (ClosestHit.Value.Distance < Checkhit.Value.Distance) { ClosestHit = Checkhit; } } else { ClosestHit = Checkhit; } } } } } if (Layermask2D != null) { var Hit2D = Physics2D.Raycast(Worldorigin, direction.normalized, distance, Layermask2D.Value); if (ClosestHit != null) { if (Hit2D.distance != 0 && ClosestHit.Value.Distance > Hit2D.distance) { ClosestHit = new CustomPhysicsHit(Hit2D); } } else { ClosestHit = new CustomPhysicsHit(Hit2D); } } if (ClosestHit == null) { ClosestHit = new CustomPhysicsHit(); } return(ClosestHit.Value); }
public static CustomPhysicsHit Linecast(Vector3 Worldorigin, LayerTypeSelection layerMask, LayerMask?Layermask2D, Vector3 WorldTo) { return(RayCast(Worldorigin, Vector2.zero, 0, layerMask, Layermask2D, WorldTo)); }
///get the farther of the two collision points along the given rays. Null raycastMask defaults to the ground collision layer (8) static protected RaycastHit2D?GetRaycastWithMaxDistance(Vector2 origin, Vector2 directionA, Vector2 directionB, LayerMask?raycastMask = null) { RaycastHit2D testRaycastA = GetRaycastInDirection(origin, directionA, raycastMask); RaycastHit2D testRaycastB = GetRaycastInDirection(origin, directionB, raycastMask); if (testRaycastA.collider == null && testRaycastB.collider == null) { //error! Dev.LogError("Both raycasts returned null! Collision point was found in either direction! (Likely a bad layermask or origin point)"); return(null); } else if (testRaycastA.collider == null) { return(testRaycastB); } else if (testRaycastB.collider == null) { return(testRaycastA); } if (testRaycastA.distance > testRaycastB.distance) { return(testRaycastA); } return(testRaycastB); }
public static WarehouseSelectionWindow StartSelection(Action <IWarehouseKeeper> selectCallback, Action cancelCallback, bool noneCheck, bool physics2D, Vector3 point, float?range = null, LayerMask?layer = null) { return(StartSelection(selectCallback, cancelCallback, null, noneCheck, physics2D, point, range, layer)); }
public static WarehouseSelectionWindow StartSelection(Action <IWarehouseKeeper> selectCallback, Action cancelCallback, Predicate <IWarehouseKeeper> defaultSelector, bool noneCheck, bool physics2D, Vector3 point, float?range = null, LayerMask?layer = null) { return(WindowsManager.OpenWindow <WarehouseSelectionWindow>(selectCallback, cancelCallback, defaultSelector, noneCheck, physics2D, point, range, layer)); }
public static bool IsPointerOverUIObject(this EventSystem current, Vector3 pointerPosition, LayerMask?layerMask = null) { PointerEventData eventDataCurrentPosition = new PointerEventData(current); eventDataCurrentPosition.position = new Vector2(pointerPosition.x, pointerPosition.y); System.Collections.Generic.List <RaycastResult> results = new System.Collections.Generic.List <RaycastResult>(); if (EventSystem.current != null) { EventSystem.current.RaycastAll(eventDataCurrentPosition, results); } if (layerMask != null) { LayerMask mask = layerMask.GetValueOrDefault(); return(results.Find(x => mask == (mask | (1 << x.gameObject.layer))).isValid); } else { return(results.Count > 0); } }
private Collider InternalGetObject(Vector3?screenPos = null, LayerMask?mask = null, Camera camera = null) { var ray = (camera ?? Camera.main).ScreenPointToRay(screenPos ?? Mouse.current.position.ReadValue()); return(Physics.Raycast(ray, out var hit, 10000f, mask ?? 1) ? hit.collider : null); }
/// <returns>Returns true if hitted something</returns> public static bool RaycastFromScreenpoint(this Camera cam, Vector2 screenPoint, out RaycastHit hit, float range = 1000f, LayerMask?mask = null, bool preventUiIntersecting = false) { return(RaycastFromScreenpoint(cam.ScreenPointToRay(screenPoint), out hit, range, mask, preventUiIntersecting)); }
/// <summary> /// Gets the game objects under the mouse, ordered so that highest item comes first. /// The top-level matrix gameobject (the one with InteractableTiles) at this point is included at the end (if any of its tilemap gameobjects were at this point) /// </summary> /// <param name="layerMask">layers to check for hits in. If left null, will use DefaultInteractionLayerMask (basically includes every layer /// that can have interactable things).</param> /// <param name="gameObjectFilter">optional filter to filter out game objects prior to sorting and checking for pixel hits, can improve performance /// by shrinking the amount of sorting and pixel checking that needs to be done. Func should return true if should include the gameobject, otherwise false. /// Be aware that the GameObject passed to this function will be the one that the SpriteRenderer or TilemapRenderer lives on, which may NOT /// be the "root" of the gameobject this renderer lives on.</param> /// <returns>the ordered game objects that were under the mouse, top first</returns> public static IEnumerable <GameObject> GetOrderedObjectsUnderMouse(LayerMask?layerMask = null, Func <GameObject, bool> gameObjectFilter = null) { return(GetOrderedObjectsAtPoint(MouseToWorldPos(), layerMask, gameObjectFilter)); }
/// <summary> /// Gets the game objects under the given world position, ordered so that highest item comes first. /// /// The top-level matrix gameobject (the one with InteractableTiles) at this point is included at the end (if any of its tilemap gameobjects were at this point) /// </summary> /// <param name="worldPoint">world point to check</param> /// <param name="layerMask">layers to check for hits in. If left null, will use DefaultInteractionLayerMask (basically includes every layer /// that can have interactable things).</param> /// <param name="gameObjectFilter">optional filter to filter out game objects prior to sorting and checking for pixel hits, can improve performance /// by shrinking the amount of sorting and pixel checking that needs to be done. Func should return true if should include the gameobject, otherwise false. /// Be aware that the GameObject passed to this function will be the one that the SpriteRenderer or TilemapRenderer lives on, which may NOT /// be the "root" of the gameobject this renderer lives on.</param> /// <returns>the ordered game objects that were under the mouse, top first</returns> public static IEnumerable <GameObject> GetOrderedObjectsAtPoint(Vector3 worldPoint, LayerMask?layerMask = null, Func <GameObject, bool> gameObjectFilter = null) { LayerMask layerMaskToUse = layerMask.GetValueOrDefault(GetDefaultInteractionLayerMask()); var result = Physics2D.RaycastAll(worldPoint, Vector2.zero, 10f, layerMaskToUse) //get the hit game object .Select(hit => hit.collider.transform.gameObject); if (gameObjectFilter != null) { result = result.Where(gameObjectFilter); } return(result //check for a pixel hit .Select(go => IsPixelHit(go.transform)) .Where(r => r != null) //order by sort layer .OrderByDescending(r => SortingLayer.GetLayerValueFromID(r.sortingLayerID)) //then by sort order .ThenByDescending(renderer => renderer.sortingOrder) //get the "parent" game object of each of the hit renderers //for a sprite renderer, the parent is the object that has a RegisterTile. //for a tilemap renderer, the parent is the oject that has a Matrix .Select(r => r is TilemapRenderer ? r.GetComponentInParent <InteractableTiles>().gameObject : r.GetComponentInParent <RegisterTile>().gameObject) //each gameobject should only show up once .Distinct()); }
public static List <RaycastHit> ArcCast(Transform transform, Cube.Face down, Cube.Face forward, float arc, int iterations, LayerMask?terrainMask = null) { var forwardVec = forward.Of(transform); var downVec = down.Of(transform); var origin = transform.position; return(ArcCast(origin, downVec, forwardVec, arc, iterations, terrainMask)); }
/// <summary> /// Gets the game objects under the mouse, ordered so that highest item comes first. /// The top-level matrix gameobject (the one with InteractableTiles) at this point is included at the end (if any of its tilemap gameobjects were at this point) /// </summary> /// <param name="layerMask">layers to check for hits in. If left null, will use DefaultInteractionLayerMask (basically includes every layer /// that can have interactable things).</param> /// <param name="gameObjectFilter">optional filter to filter out game objects prior to sorting and checking for pixel hits, can improve performance /// by shrinking the amount of sorting and pixel checking that needs to be done. Func should return true if should include the gameobject, otherwise false. /// Be aware that the GameObject passed to this function will be the one that the SpriteRenderer or TilemapRenderer lives on, which may NOT /// be the "root" of the gameobject this renderer lives on.</param> /// <returns>the ordered game objects that were under the mouse, top first</returns> public static IEnumerable <GameObject> GetOrderedObjectsUnderMouse(LayerMask?layerMask = null, Func <GameObject, bool> gameObjectFilter = null) { return(GetOrderedObjectsAtPoint(Camera.main.ScreenToWorldPoint(CommonInput.mousePosition), layerMask, gameObjectFilter)); }
public static List <RaycastHit> ArcCast(Vector3 origin, Vector3 arcCenter, Vector3 arcExtreme, float arc, int iterations, LayerMask?layerMask = null) { var allHits = new List <RaycastHit>(); var degPerPart = arc / 2 / iterations; for (int i = 0; i < iterations; i++) { var fHit = Raytate(origin, arcCenter, arcExtreme, degPerPart * i, layerMask); if (fHit.HasValue) { allHits.Add(fHit.Value); } // skip the first back-hit, so we don't double-up on straight-down raycasts if (i == 0) { continue; } var bHit = Raytate(origin, arcCenter, arcExtreme, -degPerPart * i, layerMask); if (bHit.HasValue) { allHits.Add(bHit.Value); } } return(allHits); }