You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.2 KiB

using UnityEngine;
using UnityEngine.AI;
namespace Ai
{
public class PanicEnemy : TargetingEnemy
{
[SerializeField] private float _idleTime;
[SerializeField] private float _moveTime;
[SerializeField] private float _walkRadius;
private bool _idle = true;
private float _currentTime;
protected override void Init()
{
base.Init();
_currentTime = 0.0f;
}
public override void Proceed(Vector3 playerPosition)
{
if (Vector3.Distance(playerPosition, transform.position) > _visibilityDistance)
return;
if (_idle)
{
_currentTime += Time.deltaTime;
if (_currentTime >= _idleTime)
{
_agent.SetDestination(GetRandomPosition());
_agent.isStopped = false;
_idle = false;
}
}
if (!_idle)
{
_currentTime += Time.deltaTime;
if (_currentTime >= _idleTime + _moveTime)
{
_currentTime = _currentTime - _moveTime - _idleTime;
_agent.isStopped = true;
_idle = true;
}
}
}
private Vector3 GetRandomPosition()
{
var randomDirection = Random.insideUnitSphere * _walkRadius;
randomDirection.y = 0;
randomDirection += transform.position;
NavMeshHit hit;
NavMesh.SamplePosition(randomDirection, out hit, _walkRadius, 1);
return hit.position;
}
}
}