public bool isGrounded() { bool grounded = false; Debug.DrawRay(rigidbody2d.position + Vector2.down * collider.bounds.extents.y, Vector2.down, Color.magenta); Debug.DrawRay(rigidbody2d.position - new Vector2(collider.bounds.extents.x, collider.bounds.extents.y), Vector2.down, Color.magenta); Debug.DrawRay(rigidbody2d.position + new Vector2(collider.bounds.extents.x, -collider.bounds.extents.y), Vector2.down, Color.magenta); RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.down * collider.bounds.extents.y, Vector2.down, 1, collisionMask); RaycastHit2D hitL = Physics2D.Raycast(rigidbody2d.position - new Vector2(collider.bounds.extents.x, collider.bounds.extents.y), Vector2.down, 1, collisionMask); RaycastHit2D hitR = Physics2D.Raycast(rigidbody2d.position + new Vector2(collider.bounds.extents.x, -collider.bounds.extents.y), Vector2.down, 1, collisionMask); if (hit) { levelgrid.CompareScenePositions(hit.collider.gameObject.scene.buildIndex); if (hit.distance <= groundDistance) { grounded = true; } } if (hitL) { if (!hit) { levelgrid.CompareScenePositions(hitL.collider.gameObject.scene.buildIndex); } if (hitL.distance <= groundDistance) { grounded = true; } } if (hitR) { if (!hit && !hitL) { levelgrid.CompareScenePositions(hitR.collider.gameObject.scene.buildIndex); } if (hitR.distance <= groundDistance) { grounded = true; } } return(grounded); }
// Update is called once per frame void FixedUpdate() { if (facing == Direction.Left && spriteRenderer.flipX == false) { spriteRenderer.flipX = true; } else if (facing == Direction.Right && spriteRenderer.flipX == true) { spriteRenderer.flipX = false; } boxCollider.bounds.GetTopLeft(ref start); boxCollider.bounds.GetBottomLeft(ref end); left = ParallelCast(hits, start, end, 1, numCastsVertical); boxCollider.bounds.GetTopRight(ref start); boxCollider.bounds.GetBottomRight(ref end); right = ParallelCast(hits, start, end, -1, numCastsVertical); boxCollider.bounds.GetTopLeft(ref start); boxCollider.bounds.GetTopRight(ref end); top = ParallelCast(hits, start, end, -1, numCastsHorizontal); boxCollider.bounds.GetBottomLeft(ref start); boxCollider.bounds.GetBottomRight(ref end); bottomHit = ParallelCast(hits, start, end, 1, numCastsHorizontal); if (bottomHit) { levelgrid.CompareScenePositions(bottomHit.collider.gameObject.scene.buildIndex); canDash = true; } currentState.Execute(this); if (debug) { GetComponent <MeshRenderer>().material.color = currentState.DebugColor; } if (rigidbody2d.velocity.x != 0) { facing = rigidbody2d.velocity.x > 0 ? Direction.Right : Direction.Left; } if (Input.GetKey(KeyCode.Mouse0)) { animator.SetBool("Attack", true); Vector2 direction = facing == Direction.Left ? Vector2.left : Vector2.right; Debug.DrawRay(rigidbody2d.position, direction * maxAttackDistance, Color.red); RaycastHit2D[] hits = Physics2D.BoxCastAll(rigidbody2d.position, attackBoxSize, 0, direction, maxAttackDistance, attackLayerMask); foreach (RaycastHit2D hit in hits) { //maintain a list of objects that can be attacked in a scriptable object? //using: //hit.collider.GetInstanceID() //and a scriptable object? BreakableTileInstance bti = hit.collider.GetComponent <BreakableTileInstance>(); if (bti == null || bti.spriteRenderer.sprite != bti.tileRef.sprites[0]) { continue; } bti.StartCoroutine(bti.BreakTile()); } } }