• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Position gun object relative to player angle

A

Aaron Walton

Guest
I'm trying to keep my oGun positioned at the same place on oPlayer, depending on what angle the player is at. It's a side-on game, but with a zero gravity element that means the player can walk on walls or the ceiling, so oPlayer isn't always at image_angle == 0. I have something that basically works, but only allows for four angles:
GML:
with(oPlayer)
{

    if image_angle == 0
    {
        other.x = x;
        other.y = y + 7;
    }
    if image_angle == 180
    {
        other.x = x;
        other.y = y - 7;
    }
    if image_angle == 270
    {
        other.x = x - 7;
        other.y = y;
    }
    if image_angle == 90
    {
        other.x = x + 7;
        other.y = y;
    }
}
Now, I honestly can't think of a time that the player won't be at one of these angles, but when I accidentally placed my sprite at a slightly diagonal angle and my gun suddenly stopped following him, I figured I ought to fix this just in case. I'm guessing that the lengthdir function will be the answer, but I haven't gotten it to work. Here's what I tried:

GML:
with(oPlayer)
{
    other.x = lengthdir_x(0,image_angle);
    other.y = lengthdir_y(7,image_angle);
}
But for some reason, when I use this instead of the other code, oGun's location is moved to the top left corner (I'm assuming 0,0, but haven't checked that for sure) of the room. Did I make some obvious mistake with how I coded this? Could it be interacting with some code elsewhere in the game perhaps?
 
A

Aaron Walton

Guest
Well, these ARE relative to 0,0 in the sense that they're absolute. Did you want them to be offset from the oPlayer's x and y? Because you have to tell it to do that.
Ohhhkay, I guess I don't quite understand how the function works as well as I thought I did. I thought making the statements with(oPlayer) would do that?
 

Nidoking

Member
lengthdir_x and lengthdir_y tell you how far it is from point A to point B. It's up to you to apply that to point A if point B is where you want to end up. It's like being in Chicago and asking someone for directions from Cleveland to Santa Fe. You can follow the directions they give you, but you're not starting from the same place, so why would you end up where you wanted to be? You need to add the x and y you want to start from.
 
Top