BloodAndGore/Assets/Scripts/AI/Behaviours/Follow.cs

45 lines
1.4 KiB
C#

using UnityEngine;
using Saltosion.OneWeapon.Enemies;
namespace Saltosion.OneWeapon.AI.Behaviours {
public class Follow : AIBehaviour {
public Transform Target;
public float CloseEnoughRadius;
public float TargetPositionUpdateFrequency = 5f;
private Vector2 TargetPosition;
private float TargetPositionUpdateCooldown;
private void Update() {
if (Target != null && TargetPositionUpdateCooldown <= 0) {
TargetPositionUpdateCooldown = 1.0f / TargetPositionUpdateFrequency;
TargetPosition = Target.position;
} else if (Target == null) {
TargetPositionUpdateCooldown = 0.0f;
} else {
TargetPositionUpdateCooldown -= Time.deltaTime;
}
}
public override bool CanBehave(Enemy subject) {
return Target != null;
}
public override bool Execute(Enemy subject) {
if (Target != null) {
Vector2 position = subject.transform.position;
Vector2 delta = TargetPosition - position;
if (delta.magnitude > CloseEnoughRadius) {
delta -= delta.normalized * CloseEnoughRadius;
subject.StartMovingTo(position + delta);
return true;
}
}
subject.StopMoving();
return false;
}
}
}