Hi, I want to make a simple enemy AI based on it’s root motion. I’ve exported and .FBX of my enemy character with left and right movement and a shooting animation.
I basically need to make a script that tells my enemy character to move from left to right randomly and shoot from time to time also randomly and as time goes by I need it to go faster and faster.
I’ve trying to find tutorials on state machines and enemy AI, but can’t find anything that is as simple as what I need done.
Can anybody help med with ideas or scripting as to how I get this to work randomly?
bare bones state machine can be achieved with enum to hold the available states, a variable to hold the current state and a switch statement to handle the behaviour of the states in the update function, probably want a switchState function to handle any cleanup/one off setup when moving from state to state too.
Okay this is all very unfamiliar territory for me but also very interesting This is my code so far
private Animator anim;
private bool idle;
private bool left;
private bool right;
private bool shoot;
private bool dead;
public enum EnemyStates {
idle,
left,
right,
shoot,
dead
}
public EnemyStates enemyStates;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
switch (enemyStates)
{
case EnemyStates.idle:
anim.SetBool ("idle", true);
break;
case EnemyStates.left:
anim.SetBool ("left", true);
break;
case EnemyStates.right:
anim.SetBool ("right", true);
break;
case EnemyStates.shoot:
anim.SetBool ("shoot", true);
break;
case EnemyStates.dead:
anim.SetBool ("dead", true);
break;
}
}
}
I have a couple of issues already, the first being that when I switch from one state to another it doesn’t deactivate the original state. Meaning my bools are activated at the same time if I switch manually in the inspector. I’ve never used “switch” so please excuse my n00bness.
The other thing is that It has to be activated randomly, I guess it has to have a timer like chubbspet said and I have no idea how to implement that yet but at least we’re getting somewhere, thats always good
the switch statement syntax is correct. What you are missing is the control over the state changes. This is where the “ChangeState” function comes in. When you move from one state to another it will need to set the old bool to false and the new bool to true, rather than setting it in the update. It handles the the one off “things” that need to happen when the state is changed.
The switch in the update is for the logic within each state, i.e.
pseudo
switch(state)
{
case state.idle:
// idle wandering
// check state "end" case
if(CheckHasSpottedPlayer())
{
SwitchState(state.chasePlayer)
}
else
{
if(hasRandomWanderTarget && Distance(me, there) > somevalue)
{
approach(there)
}
else
{
GetRandomWanderTarget();
}
}
break;
case state.chasePlayer:
// do chasing logic
...
}
oh, and you might get a little mixed up between the animator and the “logic” state machine you’re looking to make. The Animator controller is a state machine in it’s own right, so be a little careful when thinking these things through. (you can actually use the animator interface to make a non animation based state machine but that’s not “bare bones” stuff really).
Since I want to use root motion on the animations of my character, wouldn’t it be an animator state machine that id have to make in order to make my character move? Could I make an if - else statement that activates/deactivates the different animations? I’ve looked at the video tutorial and it explains a lot but all the tutorials i can find are about on trigger enter. My enemy character doesn’t need that, all it needs is to move left and right and shoot randomly, and eventually die. I’m really looking for the simplest solution to make this happen.
this code doesn’t work but i mean something of this sort in my last comment. I activate the animations with root motion through states and check if an animation is active, if so, deactivate the last one. Again this doesn’t work but wouldn’t something like this be simpler for my needs? heres the code
void Update ()
{
switch (enemyStates)
{
case EnemyStates.idle:
anim.SetBool ("idle", true);
if (left == true || right == true || shoot == true || dead == true)
{
anim.SetBool ("idle", false);
}
break;
case EnemyStates.left:
anim.SetBool ("left", true);
if (idle == true || right == true || shoot == true || dead == true)
{
anim.SetBool ("left", false);
}
break;
case EnemyStates.right:
anim.SetBool ("right", true);
if (idle == true || left == true || shoot == true || dead == true)
{
anim.SetBool ("right", false);
}
break;
case EnemyStates.shoot:
anim.SetBool ("shoot", true);
if (idle == true || left == true || right == true || dead == true)
{
anim.SetBool ("shoot", false);
}
break;
case EnemyStates.dead:
anim.SetBool ("dead", true);
if (idle == true || left == true || right == true || shoot == true)
{
anim.SetBool ("dead", false);
}
break;
}
}
}
I just want to add that even you add break; in a state-machine, the true statement will hammer on that frame, i highly recommend using a trigger each time you enter the state-machine to avoid hammering. You can read some of my other post how to make a trigger. This way you don’t need to have lots of bools inside the state-machine.