Invoke repeating + add goal distance to neutral behavior
This commit is contained in:
parent
bf010f3476
commit
ca7ce8e645
4 changed files with 11 additions and 13 deletions
|
@ -13,7 +13,6 @@ public class AttackHandler : MonoBehaviour
|
|||
[SerializeField] protected float knockbackHorizontalForce;
|
||||
[SerializeField] protected float knockbackVerticalForce;
|
||||
|
||||
protected float _timer;
|
||||
protected MinecraftUnit _minecraftUnit;
|
||||
|
||||
void Awake()
|
||||
|
@ -23,14 +22,9 @@ public class AttackHandler : MonoBehaviour
|
|||
|
||||
void Start()
|
||||
{
|
||||
// Random to avoid too much synchronicity
|
||||
_timer = cooldown + Random.Range(-cooldown*0.2f, cooldown*0.2f);
|
||||
InvokeRepeating(nameof(Attack), Random.Range(-cooldown*0.2f, cooldown*0.2f), cooldown);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
_timer = _timer - Time.deltaTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Launch an Attack, and return true if it's possible to attack
|
||||
|
@ -38,7 +32,6 @@ public class AttackHandler : MonoBehaviour
|
|||
/// </summary>
|
||||
public virtual bool Attack()
|
||||
{
|
||||
if (_timer > 0) return false;
|
||||
|
||||
Collider[] targets = DetectTargets();
|
||||
foreach (Collider target in targets)
|
||||
|
@ -62,7 +55,6 @@ public class AttackHandler : MonoBehaviour
|
|||
minecraftTarget.StartCoroutine(minecraftTarget.MovementHandler.TakeImpulse(knockbackVector));
|
||||
}
|
||||
}
|
||||
_timer = cooldown + Random.Range(-cooldown*0.2f, cooldown*0.2f);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,12 +10,10 @@ public class AttackSkeleton : AttackHandler
|
|||
|
||||
public override bool Attack()
|
||||
{
|
||||
if (_timer > 0) return false;
|
||||
float launchAngle = findLaunchAngle();
|
||||
//print(launchAngle);
|
||||
// If target not reachable
|
||||
if (launchAngle < 0) return false;
|
||||
_timer = cooldown;
|
||||
|
||||
GameObject arrow = Instantiate(arrowPrefab, spawnPos.position, spawnPos.rotation);
|
||||
ArrowHandler arrowHandler = arrow.GetComponent<ArrowHandler>();
|
||||
|
|
|
@ -2,9 +2,13 @@ using UnityEngine;
|
|||
|
||||
public class NeutralBehaviour : AbstractBehaviour
|
||||
{
|
||||
[SerializeField] private float distanceGoal = 0.0f;
|
||||
protected override void MoveAction()
|
||||
{
|
||||
Unit.MovementHandler.MoveTowardsNearest();
|
||||
Unit.MovementHandler.UpdateNearest();
|
||||
Vector3 targetPos = Unit.MovementHandler.TargetUnit.transform.position;
|
||||
Vector3 goalPos = targetPos + (transform.position - targetPos).normalized * distanceGoal;
|
||||
Unit.MovementHandler.MoveTowards(goalPos);
|
||||
}
|
||||
|
||||
protected override void AttackAction()
|
||||
|
|
|
@ -58,9 +58,13 @@ public class MovementHandler : MonoBehaviour
|
|||
if (agent.enabled) agent.SetDestination(destination);
|
||||
}
|
||||
|
||||
public void MoveTowardsNearest()
|
||||
public void UpdateNearest()
|
||||
{
|
||||
TargetUnit = FindNearestEnemy();
|
||||
}
|
||||
|
||||
public void MoveTowardsNearest()
|
||||
{
|
||||
MoveTowards(TargetUnit.transform.position);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue