TeleportEffect.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using UnityEngine;
  2. public class TeleportEffect : BallEffectBase
  3. {
  4. private float _timer;
  5. private TeleportEffectDefinition Config => definition as TeleportEffectDefinition;
  6. public override void OnBallLaunched(BallScript ball)
  7. {
  8. _timer = 0f;
  9. }
  10. public override void OnBallReset(BallScript ball)
  11. {
  12. _timer = 0f;
  13. }
  14. public override void ModifyVelocity(BallScript ball, ref Vector2 velocity, float deltaTime)
  15. {
  16. TeleportEffectDefinition config = Config;
  17. if (config == null)
  18. return;
  19. _timer += deltaTime;
  20. if (_timer < config.IntervalSeconds)
  21. return;
  22. _timer = 0f;
  23. Vector2 direction = velocity.sqrMagnitude > 0.0001f ? velocity.normalized : Vector2.up;
  24. Vector2 targetPos = (Vector2)ball.transform.position + direction * config.TeleportDistance;
  25. // Move instantly without collision resolution along the traveled path.
  26. ball.transform.position = targetPos;
  27. // If teleport lands inside blocks, destroy those blocks.
  28. Collider2D[] hits = Physics2D.OverlapCircleAll(targetPos, config.OverlapCheckRadius);
  29. for (int i = 0; i < hits.Length; i++)
  30. {
  31. if (hits[i] == null)
  32. continue;
  33. blockScript block = hits[i].GetComponent<blockScript>();
  34. if (block != null)
  35. block.ApplyDamage(9999f);
  36. }
  37. }
  38. }