43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
|
|
using OTG.Lab06.Abilities;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace OTG.Lab06.Effects
|
||
|
|
{
|
||
|
|
public sealed class BlinkEffect : AbilityEffect
|
||
|
|
{
|
||
|
|
[SerializeField] private float obstaclePadding = 0.8f;
|
||
|
|
[SerializeField] private LayerMask obstacleMask = ~0;
|
||
|
|
[SerializeField] private float visualLifetime = 0.8f;
|
||
|
|
|
||
|
|
public override void Activate(AbilityCastContext context)
|
||
|
|
{
|
||
|
|
Vector3 start = context.Caster.position;
|
||
|
|
Vector3 direction = context.Forward;
|
||
|
|
float distance = Mathf.Max(0.1f, context.Ability.castRange);
|
||
|
|
Vector3 target = start + direction * distance;
|
||
|
|
CharacterController controller = context.Caster.GetComponent<CharacterController>();
|
||
|
|
|
||
|
|
if (controller != null)
|
||
|
|
{
|
||
|
|
controller.enabled = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Physics.SphereCast(start + Vector3.up, 0.45f, direction, out RaycastHit hit, distance, obstacleMask, QueryTriggerInteraction.Ignore))
|
||
|
|
{
|
||
|
|
target = hit.point - direction * obstaclePadding;
|
||
|
|
target.y = start.y;
|
||
|
|
}
|
||
|
|
|
||
|
|
context.Caster.position = target;
|
||
|
|
|
||
|
|
if (controller != null)
|
||
|
|
{
|
||
|
|
controller.enabled = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
transform.position = target + Vector3.up * 0.05f;
|
||
|
|
Destroy(gameObject, visualLifetime);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|