Move if Oponent Body dist > 7pixels

StrikerX

Member
Hi, I’m trying to create a script that I use in some Attack...

  • If opponent is on the right → face right and move (+7)
  • If on the left → face left and move (-7)
  • If very close (between -7 and 7) → stop moving (do nothing)

Code:
@script
if(frame == 0)
{
    void self = getlocalvar("self");
    void opponent = getentityproperty(self, "opponent");

    if(opponent)
    {
        float x  = getentityproperty(self, "x");
        float ox = getentityproperty(opponent, "x");
        float dist = ox - x;

        // Flip
        if(dist > 0)
        {
            changeentityproperty(self, "direction", 1);
            changeentityproperty(self, "velocity", 7, 0, 0);
        }
        else if(dist < 0)
        {
            changeentityproperty(self, "direction", 0);
            changeentityproperty(self, "velocity", -7, 0, 0);
        }

        // if close
        if(dist <= 7 && dist >= -7)
        {
            changeentityproperty(self, "velocity", 0, 0, 0);
        }
    }
}
@end_script

but it doesn’t work well — whenever the character is close (<7 pixels), it keeps moving. It must move only if Oponent Body dist > 7 only this. I also put direction to prevent a bug if oponent run back
 
Hi, I’m trying to create a script that I use in some Attack...

  • If opponent is on the right → face right and move (+7)
  • If on the left → face left and move (-7)
  • If very close (between -7 and 7) → stop moving (do nothing)

Code:
@script
if(frame == 0)
{
    void self = getlocalvar("self");
    void opponent = getentityproperty(self, "opponent");

    if(opponent)
    {
        float x  = getentityproperty(self, "x");
        float ox = getentityproperty(opponent, "x");
        float dist = ox - x;

        // Flip
        if(dist > 0)
        {
            changeentityproperty(self, "direction", 1);
            changeentityproperty(self, "velocity", 7, 0, 0);
        }
        else if(dist < 0)
        {
            changeentityproperty(self, "direction", 0);
            changeentityproperty(self, "velocity", -7, 0, 0);
        }

        // if close
        if(dist <= 7 && dist >= -7)
        {
            changeentityproperty(self, "velocity", 0, 0, 0);
        }
    }
}
@end_script

but it doesn’t work well — whenever the character is close (<7 pixels), it keeps moving. It must move only if Oponent Body dist > 7 only this. I also put direction to prevent a bug if oponent run back
I suggest using the didhitscript event for the attacker, in order to trigger the opponent's velocity/direction. Additionally, put the onmovexscript in the opponent in order to manage the stop point.

Just create two script files, didhit.c and onmovex.c and then call them in the proper characters.

didhitscript data/scripts/didhit.c ## ATTACKER
onmovexscript data/scripts/onmovex.c ## OPPONENT

After that, create the proper logic for each one. Keep in mind that the "velocity" property does not manage the pixel distance, only the character's speed when moving.
Instead, you will need to check the distance using "position" property for comparisons.
 
I suggest using the didhitscript event for the attacker, in order to trigger the opponent's velocity/direction. Additionally, put the onmovexscript in the opponent in order to manage the stop point.

Just create two script files, didhit.c and onmovex.c and then call them in the proper characters.

didhitscript data/scripts/didhit.c ## ATTACKER
onmovexscript data/scripts/onmovex.c ## OPPONENT

After that, create the proper logic for each one. Keep in mind that the "velocity" property does not manage the pixel distance, only the character's speed when moving.
Instead, you will need to check the distance using "position" property for comparisons.

didhit.c (ATTACKER)
Code:
void self = getlocalvar("self");
void opponent = getentityproperty(self, "opponent");

if(opponent)
{
    float x  = getentityproperty(self, "x");
    float ox = getentityproperty(opponent, "x");
    float dist = ox - x;

    if(dist > 0)
    {
        changeentityproperty(opponent, "direction", 0);
        changeentityproperty(opponent, "velocity", 7, 0, 0);
    }
    else if(dist < 0)
    {
        changeentityproperty(opponent, "direction", 1);
        changeentityproperty(opponent, "velocity", -7, 0, 0);
    }
}

onmovex.c (OPPONENT)

Code:
void self = getlocalvar("self");
void attacker = getentityproperty(self, "opponent");

if(attacker)
{
    float x  = getentityproperty(self, "x");
    float ax = getentityproperty(attacker, "x");
    float dist = ax - x;

    if(dist <= 7 && dist >= -7)
    {
        changeentityproperty(self, "velocity", 0, 0, 0);
    }
}
 
didhit.c (ATTACKER)
Code:
void self = getlocalvar("self");
void opponent = getentityproperty(self, "opponent");

if(opponent)
{
    float x  = getentityproperty(self, "x");
    float ox = getentityproperty(opponent, "x");
    float dist = ox - x;

    if(dist > 0)
    {
        changeentityproperty(opponent, "direction", 0);
        changeentityproperty(opponent, "velocity", 7, 0, 0);
    }
    else if(dist < 0)
    {
        changeentityproperty(opponent, "direction", 1);
        changeentityproperty(opponent, "velocity", -7, 0, 0);
    }
}

onmovex.c (OPPONENT)

Code:
void self = getlocalvar("self");
void attacker = getentityproperty(self, "opponent");

if(attacker)
{
    float x  = getentityproperty(self, "x");
    float ax = getentityproperty(attacker, "x");
    float dist = ax - x;

    if(dist <= 7 && dist >= -7)
    {
        changeentityproperty(self, "velocity", 0, 0, 0);
    }
}
@StrikerX Did it work?
I suggest using damagetaker localvar specifically in the didhit event because it's a bit more precise than opponent property, which is more abrangent.

void opponent = getlocalvar("damagetaker");

1774393371539.png
 
Back
Top Bottom