| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using UnityEngine;
- public class TeleportEffect : BallEffectBase
- {
- private float _timer;
- private TeleportEffectDefinition Config => definition as TeleportEffectDefinition;
- public override void OnBallLaunched(BallScript ball)
- {
- _timer = 0f;
- }
- public override void OnBallReset(BallScript ball)
- {
- _timer = 0f;
- }
- public override void ModifyVelocity(BallScript ball, ref Vector2 velocity, float deltaTime)
- {
- TeleportEffectDefinition config = Config;
- if (config == null)
- return;
- _timer += deltaTime;
- if (_timer < config.IntervalSeconds)
- return;
- _timer = 0f;
- Vector2 direction = velocity.sqrMagnitude > 0.0001f ? velocity.normalized : Vector2.up;
- Vector2 targetPos = (Vector2)ball.transform.position + direction * config.TeleportDistance;
- // Move instantly without collision resolution along the traveled path.
- ball.transform.position = targetPos;
- // If teleport lands inside blocks, destroy those blocks.
- Collider2D[] hits = Physics2D.OverlapCircleAll(targetPos, config.OverlapCheckRadius);
- for (int i = 0; i < hits.Length; i++)
- {
- if (hits[i] == null)
- continue;
- blockScript block = hits[i].GetComponent<blockScript>();
- if (block != null)
- block.ApplyDamage(9999f);
- }
- }
- }
|