- Home /
Extending Unity's "Pluggable AI" to Different Enemy Types
I am busy following Unity's tutorial on Pluggable AI, which appears to be a much better approach to designing AI than simply using a single EnemyController script with an incredibly long Update function.
Everything works well in the simple case shown in the video with one type of enemy, but as soon as I want to extend the StateController to create an AlienStateController and ZombieStateController, it breaks, since each Action, State and Decision only accept a StateController, and as such, lose information when a derived class is passed.
I've tried deriving from each of the Action, State and Decision classes to accommodate for this, but can't figure it out?
Here are the important classes which I described above:
Action.cs:
public abstract class Action : ScriptableObject
{
public abstract void Act(StateController controller);
}
Decision.cs:
public abstract class Decision : ScriptableObject
{
public abstract bool Decide(StateController controller);
}
State.cs:
[CreateAssetMenu (menuName = "PluggableAI/State")]
public class State : ScriptableObject
{
public Action[] actions;
public Transition[] transitions;
public Color sceneGizmoColor = Color.gray;
public void UpdateState(StateController controller)
{
DoActions(controller);
CheckTransition(controller);
}
private void DoActions(StateController controller)
{
foreach (Action action in actions)
{
action.Act(controller);
}
}
private void CheckTransition(StateController controller)
{
foreach (Transition transition in transitions)
{
bool decisionSucceeded = transition.decision.Decide(controller);
controller.TransitionToState(decisionSucceeded ? transition.trueState : transition.falseState);
}
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How can I get a character to patrol and follow terrain? 1 Answer
Health not being subtracked. 1 Answer
AI in unity, need help 1 Answer