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

44 lines
1.4 KiB
C#
Raw Normal View History

2019-08-03 17:36:17 +02:00
using UnityEngine;
namespace Saltosion.OneWeapon.AI.Behaviours {
2019-08-04 15:44:44 +02:00
public class Follow : AIBehaviour {
2019-08-03 17:36:17 +02:00
public Transform Target;
public float CloseEnoughRadius;
2019-08-04 17:35:24 +02:00
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;
}
}
2019-08-03 17:36:17 +02:00
public override bool CanBehave(Enemy subject) {
2019-08-04 17:35:24 +02:00
return Target != null;
2019-08-03 17:36:17 +02:00
}
public override bool Execute(Enemy subject) {
if (Target != null) {
Vector2 position = subject.transform.position;
2019-08-04 17:35:24 +02:00
Vector2 delta = TargetPosition - position;
2019-08-03 17:36:17 +02:00
if (delta.magnitude > CloseEnoughRadius) {
delta -= delta.normalized * CloseEnoughRadius;
subject.StartMovingTo(position + delta);
return true;
}
}
subject.StopMoving();
return false;
}
}
}