Hello, I would like to program an AI for animals like in the video attached below, I have watched several tutorials on YouTube and that did not get me anywhere. I tried to solve the whole thing with a while loop, which randomly selects a direction, but that doesn’t work very well either. The AI should have the following: Change direction and stop. Thanks in advance!
Video Link: 2022-08-05 14-52-35.mp4 - Google Drive
Source: Fairlii (YouTube)
If you want to move ai without tracking player you can make like this on below im in hurry now i just writed trashy code.
If you want to track player you can make with playerPosition inside of playermovement.moveplayer
public abstract class States
{
/// <summary>
/// State machine enters <c>OnStateEnter</c> method in first sequence.
/// </summary>
public abstract void OnStateEnter(DependencyState playerMovement);
/// <summary>
/// <c>OnStateStay</c> will be work in update.
/// </summary>
public abstract void OnStateStay(DependencyState playerMovement);
/// <summary>
/// <c>OnStateExit</c> works when player exits on state
/// </summary>
public abstract void OnStateExit(DependencyState playerMovement);
}
public class DependencyState
{
public PlayerMovement PlayerMovement;
public StateMachine StateMachine;
}
public class IdleState : States
{
private float _minTime = 1, _maxTime=3;
private DependencyState _dependencyState;
public override void OnStateEnter(DependencyState dependencyState)
{
_dependencyState = dependencyState;
var randomSecond = Random.Range(_minTime, _maxTime);
WaitRandomTime(randomSecond);
}
public override void OnStateStay(DependencyState dependencyState)
{
}
public override void OnStateExit(DependencyState dependencyState)
{
}
IEnumerator WaitRandomTime(float timeSecond)
{
yield return new WaitForSeconds(timeSecond);
_dependencyState.StateMachine.ChangeState(new MovementState());
}
}
public class MovementState : States
{
public override void OnStateEnter(DependencyState dependencyState)
{
}
public override void OnStateStay(DependencyState dependencyState)
{
// 5 is max "X" axis movement num.
var randomPos = RandomPosition(5);
playerMovement.MovePlayer(randomPos);
}
public override void OnStateExit(DependencyState dependencyState)
{
}
public Vector2 RandomPosition(float maxValue)
{
return Vector2.right * Random.Range(-maxValue, maxValue);
}
}
public class StateMachine : MonoBehaviour
{
private PlayerMovement _playerMovement;
private States _currentState;
private DependencyState _dependencyState;
private void Start()
{
_playerMovement = GetComponent<PlayerMovement>();
_dependencyState.PlayerMovement = _playerMovement;
_dependencyState.StateMachine = this;
ChangeState(new IdleState());
}
private void Update()
{
if (_currentState != null)
{
_currentState.OnStateStay(_dependencyState);
}
}
public void ChangeState(States newState)
{
if (_currentState != null)
{
_currentState.OnStateExit(_dependencyState);
}
_currentState = newState;
_currentState.OnStateEnter(_dependencyState);
}
}
public class PlayerMovement : MonoBehaviour
{
private float _speedPlayer = 2f;
public void MovePlayer(Vector2 position)
{
transform.Translate(position * _speedPlayer);
}
}