Coding Enemies to Jump Situationally

C

Chillbot17

Guest
I recently tried to code an enemy to jump over walls and gaps and had success in a way. They do jump, but constantly in a pogo style. What I'm aiming for is having the enemies detect when there is an obstacle or absence of floor where they're going and jump then as opposed to constant jumping. Occasionally they'll revert to my desired state, but after one jump they'll go back to pogo.

Here is my code for the script and the variables I set, hopefully this can help with troubleshooting:

Script:
vspeedplayer_ +=gravity_;
if place_meeting(x+hspeedplayer_, y, obj_wall)
{ { if (enemyjump_limit_ = 1){
vspeedplayer_ = enemyjump; enemyjump_limit_ = 0 } else { vspeedplayer_ +=gravity_; } }
enemyjump_limit_ = 1;
hspeedplayer_ = 0;
}
x +=hspeedplayer_;

if place_meeting(x, y + vspeedplayer_, obj_wall) {vspeedplayer_ = 0; enemyjump_limit_ = 1} else if !place_meeting(x +- 2,y,obj_wall) {
{ if (enemyjump_limit_ = 1){
vspeedplayer_ = enemyjump; enemyjump_limit_ = 0 } else { vspeedplayer_ +=gravity_; } } }
y += vspeedplayer_;

Variables:
hspeedplayer_ = 0;
vspeedplayer_ = 0;
speed_ = 8;
hspeed_push = 0;
vspeed_push = 0;
gravity_ = 1;
hp = 3;
enemyjump = -15;
enemyjump_limit_ = 1;
 
D

drowned

Guest
you need to put your code in code blocks, like "[ code ] <some code> [/ code ]" without the spaces in the tags. Is this script being called in the step event of your enemies?

Also, what's with the double braces in your if-statements, and why are you checking x+hspeedplayer_ to determine whether the enemy should jump? Wouldn't you use the enemy's speed?
 
S

seorin

Guest
I did something really similar to this recently, so I'll just paste the code I used. I'm pretty new at this so it's probably not the best way to do it, but it worked flawlessly. It shouldn't be hard to adapt it for gaps as well as obstacles.
Code:
if (h_spd == 0) //against wall
            {
                if (is_on_floor)
                {
                    if (!place_meeting(x + (walk_spd * sign(dir)), bbox_bottom - jump_height, oWall)) //could jump over
                    {
                        v_spd = jump_spd;
                    }
                    else { dir *= -1; } //can't jump, turn around
                }
            }
That code is in the step event of the enemy. The variables should be pretty self-explanatory, I think.
 
C

Chillbot17

Guest
you need to put your code in code blocks, like "[ code ] <some code> [/ code ]" without the spaces in the tags. Is this script being called in the step event of your enemies?

Also, what's with the double braces in your if-statements, and why are you checking x+hspeedplayer_ to determine whether the enemy should jump? Wouldn't you use the enemy's speed?
The if statements might be excessive, I didn't know. The hspeedplayer is just a universal speed for now. But if it helps the situation I could rename the variable to enemyspeed
 
D

drowned

Guest
The if statements might be excessive, I didn't know. The hspeedplayer is just a universal speed for now. But if it helps the situation I could rename the variable to enemyspeed
Haha it's your code, you will need to determine if it helps the situation. I know that if I personally went back to work on something that wasn't fresh in my mind, and I saw "speedplayer" in my enemy's step event, I would be lost for a bit. I would say it's a good habit to get into, and if you ever find yourself working on a team, your team members will say the same thing somewhat more forcefully.
 
C

Chillbot17

Guest
Also yes, the script is being used in the step event to apply gravity and jumping.
 
C

Chillbot17

Guest
I found the solution to my problem with the code. The checking for if there's something next to the object that will prompt the jump should be a part of the step event, with a normal gravity movement script as an else statement. This prevents the pogo issue I had, and allows them to determine the time to jump over obstacles
 
Top