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