Knockback Top down

E

Emberex

Guest
I am creating a top down, but the player and enemies sprites only face left and right in a cartoonish way. I am trying to create knockback and have tried many different ways, it seems as though it should be simple and I believe it is, I just can't figure it out. This one code here stumps me, I thought it would knock me left or right depending on where the zombie is to the player, but the player is only knocked left, right is that?
GML:
if(place_meeting(x,y,oZombie)){
        if(instance_nearest(x,y,oZombie) > x){
            x -= 8;   
        }
        if(instance_nearest(x,y,oZombie) < x){
            x += 8;   
        }
    }
 

TheouAegis

Member
Code:
if(instance_nearest(x,y,oZombie) > x)
You are missing something inside that conditional. What do you think it is? If you don't know, try opening the manual and read the entry on "instance_nearest".
 

curato

Member
yeah I always like to declare a local variable inst and assign the instance_nearest to it then call the inst.x to do the comparison to avoid just that kind of mistake visually. It has the added benefit of only calling it once though you could avoid that with and else instead of another if as well.
 
E

Emberex

Guest
Code:
if(instance_nearest(x,y,oZombie) > x)
You are missing something inside that conditional. What do you think it is? If you don't know, try opening the manual and read the entry on "instance_nearest".
Okay, I read the code and figured maybe you mean the x and y, so I made a variable that holds nearest enemy, and used it. The problem is the same. The player is pushed left, even if I am to the right of the zombie, it pushes me through them and to the left still.
GML:
///Step Event

zNear = instance_nearest(x,y,oZombie);


    if(place_meeting(x,y,oZombie)){
        if(instance_nearest(zNear.x,zNear.y,oZombie) > x){
            x -= 8;   
        }
        if(instance_nearest(zNear.x,zNear.y,oZombie) < x){
            x += 8;   
        }
    }
 

Nidoking

Member
Also note that because there is no else, if zNear.x is within 8 of self.x, then it will perform both movements and return to where it started.

But if you're already checking place_meeting, why not just check instance_place against noone and then you don't need instance_nearest at all?
 
E

Emberex

Guest
yeah I always like to declare a local variable inst and assign the instance_nearest to it then call the inst.x to do the comparison to avoid just that kind of mistake visually. It has the added benefit of only calling it once though you could avoid that with and else instead of another if as well.
Thank
Nope. instance_nearest() returns an instance id.

Why are you comparing an id to x?
Ohhhh, I'm checking twice because I've already checked it. With the variable. lol Thanks
 
E

Emberex

Guest
I finally got it to work thanks to you all, I will probably need to tweak it a bit in the future but this is what I needed for now! Thank you!
GML:
//Zombie Collision & Knock-Back
    if(place_meeting(x,y,oZombie)){
        if(zNear.x > self.x){
            x -= 16;   
        }
        else {
            x += 16;   
        }
    }
        if(place_meeting(x,y,oZombie)){
        if(zNear.y > self.y){
            y -= 16;   
        }
        else {
            y += 16;   
        }
 
Top