void FixedUpdate() { Vector2 currentPos = transform.GetChild(0).position; Vector2 targetPos = Game.Instance.player.transform.GetChild(0).position; Vector3 distance = targetPos - currentPos; // Attack if in range if (distance.magnitude < attackRange) { enemy.AttackPlayer(); } distance.Normalize(); Vector3 force = distance * enemy.gameUnit.combinedWalkSpeed; rigidbody.AddForce(force); if (distance.x > 0) { if (distance.y > 0) { sprite.UseWalkAnimation(0.1f, Direction.TR); } else { sprite.UseWalkAnimation(0.1f, Direction.BR); } } else { if (distance.y > 0) { sprite.UseWalkAnimation(0.1f, Direction.TL); } else { sprite.UseWalkAnimation(0.1f, Direction.BL); } } }
void Update() { Direction direction = sprite.lastDirection; float animationSpeed = 0.1f; if (gameUnit.IsWhirlwindEnabled) { sprite.UseWhirlAnimation(animationSpeed); player.Attack(Direction.ALL); return; } float x = 0; float y = 0; if (Input.GetAxis("Horizontal") < 0) { x -= 1f; } if (Input.GetAxis("Horizontal") > 0) { x += 1f; } if (Input.GetAxis("Vertical") < 0) { y -= 1f; } if (Input.GetAxis("Vertical") > 0) { y += 1f; } //movement.Move( x, y ); Vector3 force = Vector3.Normalize(new Vector3(x, y, 0f)); if (x > 0) { if (y > 0) { direction = Direction.TR; } else { direction = Direction.BR; } } else if (x < 0) { if (y > 0) { direction = Direction.TL; } else { direction = Direction.BL; } } else { if (y > 0) { direction = Direction.TR; } else if (y < 0) { direction = Direction.BL; } } if (Input.GetButtonDown("Fire1")) { if (player.Attack(direction)) { sprite.UseAttackAnimation(0.2f, direction); } } if (force != Vector3.zero) { sprite.UseWalkAnimation(animationSpeed, direction); } else { sprite.UseStandAnimation(animationSpeed); } if (Input.GetButtonDown("Fire2")) { if (gameUnit.whirlwind > 0) { gameUnit.IsWhirlwindEnabled = true; gameUnit.WhirlWindEndTime = Time.time + 10f; gameUnit.whirlwind--; } } if (Input.GetButtonDown("Jump")) { if (gameUnit.magnet > 0) { gameUnit.useMagnet = true; gameUnit.magnet--; } } }