Ok, here is a code for the AI assuming that you have all the animations you need:
- Code: Select all
//this is in the draw actor -> script editor of the ai
if(x > Player.x + 90)
{
x = x - 4;
if(animindex !=3)
{
ChangeAnimation("Event Actor", "WalkLeft", FORWARD);
}
}
else
{
if(x < Player.x + 91 && x > Player.x - 51)
{
CreateTimer("Event Actor", "AttackLeft", 500);
}
if(x < Player.x + 90 && x > Player.x - -75)
{
x = x + 4;
if(animindex !=3)
{
ChangeAnimation("Event Actor", "WalkLeft", BACKWARD);
}
}
}
if(x < Player.x - 90)
{
x = x + 4;
if(animindex !=2)
{
ChangeAnimation("Event Actor", "WalkRight", FORWARD);
}
}
else
{
if(x > Player.x - 91 && x < Player.x + 51)
{
CreateTimer("Event Actor", "AttackRight", 500);
}
if(x > Player.x - 89 && x < Player.x + -75)
{
x = x - 4;
if(animindex !=2)
{
ChangeAnimation("Event Actor", "WalkRight", BACKWARD);
}
}
}
Now I will explain it
The top part says that if the player is more than 90 pixels away, then change the animation to walk left (or right) as long as it is not already that animation. Now you probably will have to change the "animindex" which is simply the order of the animations. For example: My walkleft animation for my AI was the 4th animation that I added. Because of that, I will say if(animindex !=
3)blablabla... It says three because the count begins at 0. So if your walkleft animation was the 5th that you added, you would change it to: if(animindex ! =
4)blablabla. It then says to move left.
The next part says that if the player is not as far away as 90 pixels, then create a timer called "attack left". For this to work you must go to: add event -> timer -> add action -> change animation, (change it to attackleft). The next part says that if the player is too close, it will walk backwards. To do this, you must have the animation backwards. The AI then moves backwards.
The other half of the code repeats itself except for the right side instead of the left.
WHAT YOU WILL NEED TO DO: You will need to change a bunch words. They are the following: Player -> to whatever your player is called. All the animation names such as: Attack left, or, Walkright, all of those, to what they are called in your game. Also, like I said you will need to change the animindex numbers to the corresponding numbers for your animations. One last thing is that you will need to make animation finish. For example, go to add event -> animation finish-> (make sure its an animation that needs to be ended such as attack right) -> add action -> change animation (change it in this case to stand right).
I hope Im being clear about that change.
I think that thats about it.
If you have any further questions about something, or if the code is not working just ask.