Well then... here is one of the best way to have anti-moonwalk, in my opinion:
You probably have the changeanimations in the keyup and and keydown events right?
I want you to remove them, all. But make sure to leave the movement code!
What creates moonwalk or abnormal animations, is the sequence of the change animations. That means the animation shouldn't be changed depending on what key is pressed.
But on what could animation change be affected by? Very simple actually! xvelocity and yvelocity! Animation should be based on what the actor IS doing and not what it SHOULD be doing ;)
Now I presented the concept, here's how to put it to work!
We'll need to check if our player is standing, what direction he's facing, if he's moving and if he's jumping. Although if you have no jumping animations, you simply check if he's moving and what direction he's facing...
And when should we check these conditions? in draw actor!
But before you start scripting, you need to create a variable that will hold the last direction of the player, 0 will mean Right, 1 will mean Left. Only because something tells me your player will by default point to the right :)
Name this variable something like "P_Dir" or even just "Dir" if you don't want to get sophisticated like me, default settings for this one will be fine.
And we also need another variable, but this time, instead of being an Integer, it should be a Real one, it should be the main dropdown box in the dialog.
Name this variable "Sensitivity", it will be for adjusting the smoothness of your animation, you'll probably need to tweak this one.
We need to have this variable set to a value, so you'll need a "Create Actor" event and add a script like this:
- Code: Select all
Sensitivity=0.4;//Remember to tweak this code to your preference!
//A value near 0 will make it too sensitive
//A value greater than your walking speed will make him always standing
//A value in the negative might make him always walk
now in the draw actor event, here's what a script should look like if you only had moving animations and stopped animations (no jumping/falling animations)
- Code: Select all
//First we need to check what direction the player last moved towards to
if(xvelocity>Sensitivity)//The player is moving to the right, his xvelocity is greater
{
Dir=0;//The player is facing right :)
}
else if(xvelocity<-Sensitivity)//The player is moving to the left, his xvelocity is lesser, we also use else here to remove any CPU load when we can, for good practice :)
{
Dir=1;//The player is facing left :)
}
//Now we can check if he's walking or not!
if(xvelocity<=Sensitivity && xvelocity>=-Sensitivity)//the && is AND and allows us to make more than one condition check
{//if the player is not moving, he surely be near 0 xvelocity if not 0
switch(Dir)//Switch allows us to give action depending on what the value the variable is, this simply saves us time!
{
case 0: //if Dir is 0, or facing right
ChangeAnimation("Event Actor","Stand_Right",NO_CHANGE);//make sure to change the names, we also use NO_CHANGE so the animation doesn't constantly repeats itself
break; //Prevents the code from executing further... otherwise it will run what's below
case 1://if Dir is 1, or facing left
ChangeAnimation("Event Actor","Stand_Left",NO_CHANGE);//Again, make sure to change the names
break;//Never forget these in your switch statement!
}
}
else //Obviously, if the player is not not moving, then he must be moving! ;) so here is what happens otherwise!
{
switch(Dir)//Again, we need to know which direction he's walking!
{
case 0: //seems to me he's walking right!
ChangeAnimation("Event Actor","Walk_Right",NO_CHANGE);//Also don't forget the NO_CHANGE, and to change the names!
break;
case 1: //walking left...
ChangeAnimation("Event Actor","Walk_Left",NO_CHANGE);//To change the names, forget not you should.
break;//Neither these!
}
}
//Seems to me we're done here ;)
Now that should be it.
But if you happen to have falling and jumping animation, that just means one more condition check!
here's how it'd look! I'll omit commenting on places that are the same...
- Code: Select all
if(xvelocity>Sensitivity)
{
Dir=0;
}
else if(xvelocity<-Sensitivity)
{
Dir=1;
}
if(yvelocity>Sensitivity)//When player falls, his yvelocity is greater :)
{
switch(Dir)
{
case 0:
ChangeAnimation("Event Actor","Fall_Right",NO_CHANGE);
break;
case 1:
ChangeAnimation("Event Actor","Fall_Left",NO_CHANGE);
break;
}
}
else if(yvelocity<-Sensitivity)//When player jumps, his yvelocity it lesser!
{
switch(Dir)
{
case 0:
ChangeAnimation("Event Actor","Jump_Right",NO_CHANGE);
break;
case 1:
ChangeAnimation("Event Actor","Jump_Left",NO_CHANGE);
break;
}
}
else//If player is not jumping or falling... he must be standing :)
{
if(xvelocity<=Sensitivity && xvelocity>=-Sensitivity)
{
switch(Dir)
{
case 0:
ChangeAnimation("Event Actor","Stand_Right",NO_CHANGE);
break;
case 1:
ChangeAnimation("Event Actor","Stand_Left",NO_CHANGE);
break;
}
}
else
{
switch(Dir)
{
case 0:
ChangeAnimation("Event Actor","Walk_Right",NO_CHANGE);
break;
case 1:
ChangeAnimation("Event Actor","Walk_Left",NO_CHANGE);
break;
}
}
}
//Done again :D
Hopefully you understand any of this... and that it helped!