41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class AnimeApproach : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private EnemySettings enemySettings;
|
|
private GameObject _target;
|
|
|
|
void FixedUpdate()
|
|
{
|
|
if (_target == null)
|
|
{
|
|
_target = findTarget();
|
|
}
|
|
var d = Vector3.Distance(transform.position, _target.transform.position);
|
|
if (d <= enemySettings.ApproachDistance)
|
|
{
|
|
return;
|
|
}
|
|
var dx = enemySettings.Speed * Time.fixedDeltaTime;
|
|
var p = Vector3.MoveTowards(transform.position, _target.transform.position, dx);
|
|
transform.position = p;
|
|
}
|
|
|
|
private GameObject findTarget()
|
|
{
|
|
var party = GameObject.FindGameObjectsWithTag("Player");
|
|
if (party.Length == 0)
|
|
{
|
|
// Everyone is dead...?
|
|
// Return self so we don't chase nothing.
|
|
return gameObject;
|
|
}
|
|
// Eventually we'll want to check party threat.
|
|
var k = Random.Range(0, party.Length);
|
|
return party[k];
|
|
}
|
|
}
|