public IEnumerator Dash(DashDir _dir, Vector2 dashZonePos) { m_Controllable = false; Vector2 dir = Vector2.zero; switch (_dir) { case DashDir.Up: dir = Vector2.up; break; case DashDir.Down: dir = Vector2.down; break; case DashDir.Left: dir = Vector2.left; break; case DashDir.Right: dir = Vector2.right; break; } RaycastHit2D hit = Physics2D.Raycast(dashZonePos, dir, dashDistance, 1 << LayerMask.NameToLayer("Wall") | 1 << LayerMask.NameToLayer("Floor")); float distance = !hit ? dashDistance : Vector3.Distance(hit.point, dashZonePos) - GetComponent <BoxCollider2D>().size.x / 2; m_Rigidbody2D.velocity = Vector3.zero; int dashCount = 10; Vector3 destination = (Vector3)dashZonePos + (Vector3)dir * distance - transform.position; for (int i = 0; i < dashCount; i++) { yield return(null); transform.position += destination / dashCount; } m_Rigidbody2D.velocity = dir * 10; m_Controllable = true; }
void Update() { rigidBody.AddForce(new Vector2(Input.GetAxis("Horizontal") * Time.deltaTime * Speed, 0), ForceMode2D.Force); if (rigidBody.velocity.x > MaxSpeed) SetVeloctiyXAxis(MaxSpeed); if (rigidBody.velocity.x < -MaxSpeed) SetVeloctiyXAxis(MaxSpeed); RaycastHit2D hit = Physics2D.Linecast(new Vector2(transform.position.x, transform.position.y), new Vector2(transform.position.x, transform.position.y - 1f), JumpLayerMask); if (Input.GetKeyDown(JumpButton) && hit.collider != null) { rigidBody.AddForce(new Vector2(0, JumpHeight), ForceMode2D.Impulse); } if (Input.GetAxis("Horizontal") > 0.1f) sr.sprite = FacingRightSprite; if (Input.GetAxis("Horizontal") < -0.1f) sr.sprite = FacingLeftSprite; if (Input.GetKeyDown(DashRight)) { if (currentDash == DashDir.DashingRight && DashTimer >= 0.3f) { rigidBody.AddForce(new Vector2(MaxSpeed * 0.5f, 1)); currentDash = DashDir.NotDashing; } else if (currentDash == DashDir.NotDashing) { currentDash = DashDir.DashingRight; DashTimer = 0; } } if (Input.GetKeyDown(DashLeft)) { if (currentDash == DashDir.DashingLeft && DashTimer >= 0.3f) { rigidBody.AddForce(new Vector2(-MaxSpeed * 0.5f, 1)); } else if (currentDash == DashDir.NotDashing) { currentDash = DashDir.DashingLeft; DashTimer = 0; } } if (DashTimer > 0.3f) currentDash = DashDir.NotDashing; if (currentDash == DashDir.DashingRight || currentDash == DashDir.DashingLeft) DashTimer += Time.deltaTime; Debug.Log(currentDash); }