void Update() { if (_playerCollider != null) { // Check if the player box fits completely inside the collider, which // means there's nothing left outside the hole, so can fall inside. // This allows larger player to walk "over" smaller holes. bool allInside = true; allInside = allInside && _selfCollider.OverlapPoint(_playerCollider.bounds.min); allInside = allInside && _selfCollider.OverlapPoint(new Vector2(_playerCollider.bounds.min.x, _playerCollider.bounds.max.y)); allInside = allInside && _selfCollider.OverlapPoint(_playerCollider.bounds.max); allInside = allInside && _selfCollider.OverlapPoint(new Vector2(_playerCollider.bounds.max.x, _playerCollider.bounds.min.y)); if (allInside) { // Fall into the hole and die var director = Director.Instance; director.PlayerController.enabled = false; director.PlaySfx(FallClip); var sr = Director.Instance.Player.GetComponent <SpriteRenderer>(); var co = FadeToHole(Director.Instance.Player.transform, sr); StartCoroutine(co); // Forget about the player, so avoid repeating all of that _playerCollider = null; } } }
private void BoxFillOrErase() { Vector3Int boxLocationInInt = new Vector3Int(Mathf.RoundToInt(rectDraw.gameObject.transform.position.x), Mathf.RoundToInt(rectDraw.gameObject.transform.position.y), Mathf.RoundToInt(rectDraw.gameObject.transform.position.z)); Vector3Int boxSizeInInt = new Vector3Int(Mathf.RoundToInt(box.width / pixelSizeMultiplier), Mathf.RoundToInt(box.height / pixelSizeMultiplier), 1); BoundsInt boxToFill = new BoundsInt(boxLocationInInt, boxSizeInInt); if (isDrawing) { foreach (Vector3Int position in boxToFill.allPositionsWithin) { if (validAreaCollider.OverlapPoint(new Vector2(position.x, position.y)) && !hazardsTilemap.HasTile(position) && !foreGroundTilemap.HasTile(position)) { foreGroundTilemap.SetTile(position, tile); tileCount++; levelController.DecreaseBlockCount(); //decrease because user has used one of his available blocks and now has one less block left } } } else if (isErasing) { foreach (Vector3Int position in boxToFill.allPositionsWithin) { if (validAreaCollider.OverlapPoint(new Vector2(position.x, position.y)) && foreGroundTilemap.HasTile(position)) { foreGroundTilemap.SetTile(position, null); tileCount--; levelController.IncreaseBlockCount(); //increase because removing a block means user has one more block available to play } } } }
private void UpdateCurrentCollider() { if (!(currentCollider != null && currentCollider.OverlapPoint(target.position))) { GetCurrentCollider(); } }
// Start is called before the first frame update void Start() { if (objectToSpawn == null) { Debug.LogError("Cannot spawn a nonexistant object!"); return; } if (spawnAreas.Length == 0) { Debug.LogError("Cannot spawn an object in no areas!"); return; } Renderer objectRenderer = objectToSpawn.GetComponent <Renderer>(); Vector2 extents = objectRenderer != null ? new Vector2(objectRenderer.bounds.extents.x, objectRenderer.bounds.extents.y) : Vector2.zero; extents = objectToSpawn.transform.TransformVector(extents); int numSpawned = 0; int i = 0; while (numSpawned < numToSpawn) { CompositeCollider2D spawnArea = spawnAreas[Mathf.FloorToInt(spawnAreas.Length * Random.Range(0, .9999999f))]; CompositeCollider2D.GeometryType geoType = spawnArea.geometryType; spawnArea.geometryType = CompositeCollider2D.GeometryType.Polygons; Bounds spawnBounds = spawnArea.bounds; Vector2 spawnExtents = spawnBounds.extents; spawnExtents -= extents; Vector2 point = spawnArea.offset + new Vector2(spawnBounds.center.x, spawnBounds.center.y) + new Vector2( Random.Range(-spawnBounds.extents.x, spawnBounds.extents.x), Random.Range(-spawnBounds.extents.y, spawnBounds.extents.y) ) * .9f; if (spawnArea.OverlapPoint(point)) { GameObject go = Instantiate(objectToSpawn, transform); go.transform.position = new Vector3(point.x, point.y, 0f); go.transform.rotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f)); ++numSpawned; } spawnArea.geometryType = geoType; ++i; if (i > 100000) { Debug.LogError("Over 100000 iterations for spawning! Something must be wrong..."); return; } } }
bool destinationIsWall(Vector3 newPosition) { Vector3[] pointsToCheck = new Vector3[] { newPosition + Vector3.up * radius, newPosition + Vector3.down * radius, newPosition + Vector3.left * radius, newPosition + Vector3.right * radius }; foreach (Vector3 point in pointsToCheck) { if (gridCollider.OverlapPoint(point)) { return(true); } } return(false); }
private Vector2 FindOverlappedPoint() { Vector2 overlappedMousePos = new Vector2(); Vector2 closestPoint = compositeRoomCollider.ClosestPoint(mousePos); Vector2[] stepPoints = new Vector2[4] { new Vector2(closestPoint.x + 0.1f, closestPoint.y), new Vector2(closestPoint.x, closestPoint.y + 0.1f), new Vector2(closestPoint.x - 0.1f, closestPoint.y), new Vector2(closestPoint.x, closestPoint.y - 0.1f), }; foreach (var stepPoint in stepPoints) { if (compositeRoomCollider.OverlapPoint(stepPoint)) { overlappedMousePos = stepPoint; } } return(overlappedMousePos); }
private bool IsValidPlacement(Vector2 position) { return(freelyPlaceable || !enemyPath.OverlapPoint(position)); //!path.bounds.Contains(position); }
/** * initiate navigation map */ public void initNavigationMap() { calculatedPaths = new Dictionary <((int, int), (int, int)), List <NodeItem> >(); for (int i = 0; i < WallMarks.transform.childCount; i++) { Destroy(WallMarks.transform.GetChild(i).gameObject); } for (int i = 0; i < PathMarks.transform.childCount; i++) { PathMarks.transform.GetChild(i).gameObject.SetActive(false); } w = Mathf.RoundToInt(tilemapEnd.x - tilemapStart.x + 1); h = Mathf.RoundToInt(tilemapEnd.y - tilemapStart.y + 1); map = new NodeItem[w, h]; CompositeCollider2D wallCollider = wall.GetComponent <CompositeCollider2D>(); CompositeCollider2D floorCollider = GetComponent <CompositeCollider2D>(); CompositeCollider2D pitCollider = pit.GetComponent <CompositeCollider2D>(); // write unwalkable node for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { Vector2 pos = new Vector2(tilemapStart.x + x, tilemapStart.y + y); // check walkable or not bool isWall = wallCollider.OverlapPoint(pos + new Vector2(0.5f, 0.5f)); bool isPit = pitCollider.OverlapPoint(pos + new Vector2(0.5f, 0.5f)); int nearWall = 0; bool nearPit = false; // print("isWall: " + isWall.ToString()); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (!(i == 1 && j == 1)) { if (wallCollider.OverlapPoint(pos + new Vector2(0.5f + i - 1, 0.5f + j - 1))) { ++nearWall; } } nearPit = nearPit || pitCollider.OverlapPoint(pos + new Vector2(0.5f + i - 1, 0.5f + j - 1)); } } // print("nearWall: " + nearWall.ToString()); // new a node map [x, y] = NodeItem.init(isWall || isPit, pos, x, y, 100000); if (floorCollider.OverlapPoint(pos + new Vector2(0.5f, 0.5f))) { // print("Setting floor cost..."); if (nearWall > 0 || nearPit) { if (nearWall > 1) { map[x, y].cost = GetComponent <NodeCost>().cost *nearWallCost; } else { map[x, y].cost = GetComponent <NodeCost>().cost; } if (nearPit) { map[x, y].cost = GetComponent <NodeCost>().cost *nearWallCost; } } else if (!(isWall || isPit)) { map[x, y].cost = GetComponent <NodeCost>().cost; } } // mark unwalkable node if ((nearWall > 1 || nearPit) && !(isWall || isPit) && showWallMark && WallMark) { GameObject obj = Instantiate(WallMark, new Vector3(pos.x + 0.5f, pos.y + 0.5f, 0), Quaternion.identity); obj.transform.SetParent(WallMarks.transform); } } } }
public bool OverlapPoint(Vector2 point) => compositeCollider.OverlapPoint(point);