[SOLVED] Limit Z movement

O Ilusionista

Captain 100K
Hi guys. I want to limit how far a character (and only that one, which is a weapon from another character) can walk in Z axis.
He would either no move in z axis at all or just move few pixels.

What would be the best solution for that? The right area to put a script for that would be "onmovezscript", right?
I can think in how I make it stop to move by checking the his Z position on the stage and setting his Z velocity to 0, but how to make it able to move again?

Or it would be better to adapt the "keymove" scripted function to take care of the z position?
Code:
void keymove(float V)
{// Move hero if direction button is pressed
      void self = getlocalvar("self");
      int iPIndex = getentityproperty(self,"playerindex"); //Get player index
	float xdir = 0;
	float zdir = 0;

      if (playerkeys(iPIndex, 0, "moveleft")){// Left is pressed?
	  xdir = -V;
	} else if(playerkeys(iPIndex, 0, "moveright")){// Right is pressed?
	  xdir = V;
      }

	if(playerkeys(iPIndex, 0, "moveup")){// Up is pressed?
	  zdir = -V/2;
	} else if(playerkeys(iPIndex, 0, "movedown")){// Down is pressed?
	  zdir = V/2;
      }

	changeentityproperty(self, "velocity", xdir, zdir);
}
 
Both would work, but I'd probably use onmovez. Setting velocity is only persistent until something else changes it again - like the player attempting to move, so you don't need to worry about it freezing them in place.

DC
 
I agree with Damon Caskey, onmovez works best for this purpose.

Here's an example to limit z movement with onmovez:
Code:
void main()
{ // Limits z movement to certain z coords
    void self = getlocalvar("self");
    int z = getentityproperty(self,"z"); //Get character's z coordinate

    if(z >= 250){ // Reached z limit
      changeentityproperty(self, "position", NULL(), 250);
      changeentityproperty(self, "velocity", NULL(), 0);
    }
}

This script limits movement z to z = 250
 
Thanks everyone.

@WhiteDragon: I haven't tested your function because I am trying to be economic with scripts

@BB: Thanks, but to get it work, I've modified it a bit:

I saved this code as limitz.c then called it

onmovezscript          data/scripts/limitz.c

Code:
void main()
{ // Limits z movement to certain z coords
    void self = getlocalvar("self");
    int z = getentityproperty(self,"z"); //Get character's z coordinate

    if(z != 200){ // Reached z limit
      changeentityproperty(self, "position", NULL(), 200);
      changeentityproperty(self, "velocity", NULL(), 0);
    }
}

 
O Ilusionista said:

Here's a slightly refined version for you. This will only stop Z movement instead of all movement. That way players can still move along the limit when they walk diagonally.

Code:
void main()
{ // Limits z movement to certain z coords
    void self = getlocalvar("self");
    int z = getentityproperty(self,"z"); //Get character's z coordinate
    
    float vel_x;
    float vel_y;
    float vel_z;

    if(z != 200)
    { 
        vel_x = getentityproperty(self, "xdir");
        vel_y = getentityproperty(self, "dropv");
        vel_z = 0.0;
    
      // Reached z limit
      changeentityproperty(self, "position", NULL(), 200);
      changeentityproperty(self, "velocity", vel_x, vel_z, vel_y);
    }
}

DC
 
Back
Top Bottom