//<summary>左右の2方向をそれぞれ単位ベクトルに変換</summary> static public Vector2 convertToVector(DirectionH aDirection) { switch (aDirection) { case DirectionH.left: return(new Vector2(-1, 0)); case DirectionH.right: return(new Vector2(1, 0)); } return(new Vector2(0, 0)); }
void OnTriggerStay2D(Collider2D collision) { if (collision.tag == "Enemy" && !invulnerable) { if (collision.transform.position.x - transform.position.x > 0) { Facing = DirectionH.RIGHT; Velocity = new Vector2(-Convert("00.80"), Convert("01.00")); } else { Facing = DirectionH.LEFT; Velocity = new Vector2(Convert("00.80"), Convert("01.00")); } Stunned = true; invulnerable = true; invulnCount = 0; health--; GameHandler.healthBarImage.fillAmount = health / 10f; if (health == 0) { Die(); } else { GameHandler.PlayAudio(GameHandler.playerHitAudio); } } if (collision.tag == "Lava" && !invulnerable) { health = 0; GameHandler.healthBarImage.fillAmount = health / 10f; Die(); } if (collision.gameObject.tag == "Level2") { SceneManager.LoadScene(2); } if (collision.tag == "Win") { GameHandler.Win(); Destroy(gameObject); } }
private void Controls() { if (Stunned) { return; } float moveSpeed = Convert(OnGround ? GroundSpeed : AirSpeed); int dir = GameHandler.vc.Right.IntValue - GameHandler.vc.Left.IntValue; if (dir != 0 && dir != oldDir) { accelerating = true; } if (accelerating) { if (accelCount < 7) { moveSpeed = Convert(AccelSpeed); accelCount++; } else { accelerating = false; maxSpeed = true; } } else { accelCount = 0; } if (dir == 0 && oldDir != 0 && maxSpeed) { decelerating = true; } if (decelerating) { if (decelCount < 8) { dir = oldDir; moveSpeed = Convert(decelSpeed); decelCount++; } else { decelerating = false; } } else { decelCount = 0; } if (dir == 0) { accelerating = false; maxSpeed = false; } if (dir != 0) { decelerating = false; } oldDir = dir; float belt = 0; if (lastGroundTag == "Belt Left" && OnGround) { belt = -0.05f; } else if (lastGroundTag == "Belt Right" && OnGround) { belt = 0.05f; } Velocity = new Vector2(dir * moveSpeed + belt, Velocity.y); if (OnGround && GameHandler.vc.Jump.Press) { Velocity = new Vector2(Velocity.x, Convert(JumpPower)); OnGround = false; Jumping = true; } if (Movement.y > Convert(MinJump) && !GameHandler.vc.Jump.Value && Jumping) { Movement = new Vector2(Movement.x, Convert(JumpCancelSpeed)); Velocity = new Vector2(Velocity.x, Convert(JumpCancelSpeed)); Jumping = false; } if (Movement.y < 0f) { Jumping = false; } List <Pellet> removal = new List <Pellet>(); foreach (Pellet p in pellets) { if (p == null) { removal.Add(p); } } foreach (Pellet p in removal) { pellets.Remove(p); } if (GameHandler.vc.Shoot.Press && pellets.Count < 3) { Pellet pellet = Instantiate(pelletPrefab, transform.position + new Vector3(Facing == DirectionH.RIGHT ? 1f : -1f, 0.77f, 0f), Quaternion.Euler(0f, Facing == DirectionH.RIGHT ? 0f : 180f, 0f)); pellets.Add(pellet); Shooting = true; shootCount = 0; GameHandler.PlayAudio(GameHandler.playerShootAudio); } if (Shooting) { shootCount++; if (shootCount >= 40) { shootCount = 0; Shooting = false; } } if (dir == 1) { Facing = DirectionH.RIGHT; } if (dir == -1) { Facing = DirectionH.LEFT; } }