GameMaker Help with Project Player + Weapon + Mouse

Help with Project Player + Weapon + Mouse

Hello, friends, I have a hard time defining how best my game works with the Sprites of Weapons + Mouse to do the sights.

See the image below, I made my Player without the arms to put the arms together with the Weapons, not if this is the best way.

But when the mouse is in certain position the gun looks strange on the player, giving a bad impression.


I thought about limiting the angle of the weapon in relation to the player but I do not know how to program it in the game maker.

I thought about making a Sprite of My Player Along with the Weapon for each Degree Example 45,90,135 .... so on, but I do not know if this is ideal.

This is my code for controlling the rank of the weapon.
Code:
x = oPlayer.x + global.weaponArray[global.weapon,5];
y = oPlayer.y + global.weaponArray[global.weapon,6];

//Código para utilização de controle
if (oPlayer.controller == 0) // caso o controle esteja desligado
{   //teclado e mouse ativos
    image_angle = point_direction(x,y,mouse_x,mouse_y);
}
else
{   //direções do analógico direito ativos
    var controllerh = gamepad_axis_value(0,gp_axisrh);//gp_axislh R é de analógico da Direita H Horizontal
    var controllerv = gamepad_axis_value(0,gp_axisrv);
    if (abs(controllerh) > 0.2) || (abs(controllerv) > 0.2) //area morta
    {   
        controllerangle = point_direction(0,0,controllerh,controllerv);    //na posição de 0,0 recebemos os valores H e V do analógico da direita
    }
    image_angle = controllerangle;
}
Here I change the sprite sideways according to the angle.
Code:
// altera a sprite da Gun para a Esquerda
if (image_angle > 90) && (image_angle < 270)
{
    image_yscale = -1;
}
else
{
    image_yscale = 1;
}
If anyone can point me a way, or any tips.

if I can not I'll put the static gun even pointing in one direction. I did not want this if I could not.

Sorry for English, I'm using the translator.
 

YoSniper

Member
It looks like you're flipping the image_xscale value on the player, which will throw a wrench into how the image_angle of the gun is interpreted.

Try this code for the gun:
Code:
image_xscale = obj_player.image_xscale; //Replace obj_player with whatever the object index is for the Player holding the gun.
if (abs(controllerh) > 0.2) || (abs(controllerv) > 0.2) //area morta
{   
   controllerangle = point_direction(0,0,controllerh,controllerv);    //na posição de 0,0 recebemos os valores H e V do analógico da direita
}
if image_xscale > 0 {
    image_angle = controllerangle;
} else {
    image_angle = controllerangle + 180;
}
Patch your code with this trick as needed and hopefully it doesn't look so weird.
 
Hi, I do not know if I'm doing it right, see.

Code:
x = oPlayer.x + global.weaponArray[global.weapon,5];
y = oPlayer.y + global.weaponArray[global.weapon,6];

//Código para utilização de controle
if (oPlayer.controller == 0) // caso o controle estega desligado
{   //teclado e mouse ativos
    image_angle = point_direction(x,y,mouse_x,mouse_y);
}
else
{   //direções do analogico direito ativos
    var controllerh = gamepad_axis_value(0,gp_axisrh);//gp_axislh R é de analogico da Direita H Horizontal
    var controllerv = gamepad_axis_value(0,gp_axisrv);
    if (abs(controllerh) > 0.2) || (abs(controllerv) > 0.2) //area morta
    {  
        controllerangle = point_direction(0,0,controllerh,controllerv);    //na posição de 0,0 recebemos os valores H e V do analogico da direita
    }
    image_angle = controllerangle;
}

image_xscale = oPlayer.image_xscale; //Replace obj_player with whatever the object index is for the Player holding the gun.
if (abs(controllerh) > 0.2) || (abs(controllerv) > 0.2) //area morta
{  
   controllerangle = point_direction(0,0,controllerh,controllerv);    //na posição de 0,0 recebemos os valores H e V do analógico da direita
}
if image_xscale > 0 {
    image_angle = controllerangle;
} else {
    image_angle = controllerangle + 180;
}
I get this error when I get my oGun

local variable controllerh(100001, -2147483648) not set before reading it.
at gml_Object_oGun_Step_1 (line 23) - if (abs(controllerh) > 0.2) || (abs(controllerv) > 0.2) //area morta
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_oGun_Step_1 (line 23)

PS: This Gun sprite problem happens with the Mouse position or with the control analog.
 
Last edited:

YoSniper

Member
I think the error happens because you declared controllerv as a var inside that "else" block, and variables declared as var are deleted once they leave the appropriate scope.

Try this:
Code:
x = oPlayer.x + global.weaponArray[global.weapon,5];
y = oPlayer.y + global.weaponArray[global.weapon,6];

//Código para utilização de controle
if (oPlayer.controller == 0) // caso o controle estega desligado
{   //teclado e mouse ativos
   image_angle = point_direction(x,y,mouse_x,mouse_y);
}
else
{   //direções do analogico direito ativos
   var controllerh = gamepad_axis_value(0,gp_axisrh);//gp_axislh R é de analogico da Direita H Horizontal
   var controllerv = gamepad_axis_value(0,gp_axisrv);
   if (abs(controllerh) > 0.2) || (abs(controllerv) > 0.2) //area morta
   { 
       controllerangle = point_direction(0,0,controllerh,controllerv);    //na posição de 0,0 recebemos os valores H e V do analogico da direita
   }
   image_xscale = oPlayer.image_xscale;
   if image_xscale > 0 {
      image_angle = controllerangle;
   } else {
      image_angle = controllerangle + 180;
   }
}
 
It worked,.

but the result is still strange from the weapon on the character.

I still do not know what to do, I'll do more tests.
 
so it seems a bit strange to the eyes, I thought of something like this image below. the angle of the weapon limited to that range.

 

YoSniper

Member
Oh okay. Well then that condition can be tacked onto the end of the code.

Here's a fix to your code with an arbitrary angle limitation for either facing right or left.

Code:
x = oPlayer.x + global.weaponArray[global.weapon,5];
y = oPlayer.y + global.weaponArray[global.weapon,6];

var MAX_DEVIATION_FROM_HORIZONTAL = 20; //20 degrees

//Código para utilização de controle
if (oPlayer.controller == 0) // caso o controle estega desligado
{   //teclado e mouse ativos
   image_angle = point_direction(x,y,mouse_x,mouse_y);
}
else
{   //direções do analogico direito ativos
   var controllerh = gamepad_axis_value(0,gp_axisrh);//gp_axislh R é de analogico da Direita H Horizontal
   var controllerv = gamepad_axis_value(0,gp_axisrv);
   if (abs(controllerh) > 0.2) || (abs(controllerv) > 0.2) //area morta
   {
      controllerangle = point_direction(0,0,controllerh,controllerv);    //na posição de 0,0 recebemos os valores H e V do analogico da direita
   }
   image_xscale = oPlayer.image_xscale;
   if image_xscale > 0 {
     image_angle = controllerangle;
     if image_angle < 180 and image_angle > MAX_DEVIATION_FROM_HORIZONTAL {
        image_angle = MAX_DEVIATION_FROM_HORIZONTAL;
     } else if image_angle > 180 and image_angle < 360 - MAX_DEVIATION_FROM_HORIZONTAL {
        image_angle = 360 - MAX_DEVIATION_FROM_HORIZONTAL;
     }
   } else {
     image_angle = controllerangle + 180;
     if image_angle < 180 - MAX_DEVIATION_FROM_HORIZONTAL {
        image_angle = 180 - MAX_DEVIATION_FROM_HORIZONTAL;
     } else if image_angle > 180 + MAX_DEVIATION_FROM_HORIZONTAL {
        image_angle = 180 + MAX_DEVIATION_FROM_HORIZONTAL;
     }
   }
}
 
that's just it.

Thank you very much, my friend.

I'm just checking because the sprite is not going to the same side of the player.
 
I made a change, I'm testing just for the angles caught from the mouse.

to the Right is working perfectly.

However to the left the result is not expected.


Look how my code is.

Code:
var MAX_DEVIATION_FROM_HORIZONTAL = 10; //20 degrees
var TEMPANGLE = point_direction(x,y,mouse_x,mouse_y);

if (oPlayer.controller == 0)
{
   image_xscale = oPlayer.image_xscale;

   if image_xscale > 0 {
     image_angle = TEMPANGLE;
     if image_angle < 180 and image_angle > MAX_DEVIATION_FROM_HORIZONTAL {
        image_angle = MAX_DEVIATION_FROM_HORIZONTAL;
     } else if image_angle > 180 and image_angle < 360 - MAX_DEVIATION_FROM_HORIZONTAL {
        image_angle = 360 - MAX_DEVIATION_FROM_HORIZONTAL;
     }
   } else {
     image_angle = TEMPANGLE + 180;
     if image_angle < 180 - MAX_DEVIATION_FROM_HORIZONTAL {
        image_angle = 180 - MAX_DEVIATION_FROM_HORIZONTAL;
     } else if image_angle > 180 + MAX_DEVIATION_FROM_HORIZONTAL {
        image_angle = 180 + MAX_DEVIATION_FROM_HORIZONTAL;
     }
   }
}
 

YoSniper

Member
I may have miscalculated how the gun points when image_xscale = -1

Let's say the direction to the mouse is 170 degrees.

image_xscale is -1, so the gun flips horizontally. Then the resultant sprite is rotated counter-clockwise 170 + 180 = 350 degrees, which is the same as rotating it clockwise 10 degrees.

So far, the gun should be pointing in the right direction.

I realize now that the extra conditions I provided are throwing the sprite off.

Replace your code with this, and hopefully it should be fixed. Sorry for the mix-up:
Code:
var MAX_DEVIATION_FROM_HORIZONTAL = 10; //20 degrees
var TEMPANGLE = point_direction(x,y,mouse_x,mouse_y);

if (oPlayer.controller == 0)
{
   image_xscale = oPlayer.image_xscale;

   if image_xscale > 0 {
    image_angle = TEMPANGLE;
   } else {
    image_angle = TEMPANGLE + 180;
    if image_angle >= 360 {
        image_angle -= 360;
    }
   }
   if image_angle < 180 and image_angle > MAX_DEVIATION_FROM_HORIZONTAL {
      image_angle = MAX_DEVIATION_FROM_HORIZONTAL;
   } else if image_angle > 180 and image_angle < 360 - MAX_DEVIATION_FROM_HORIZONTAL {
      image_angle = 360 - MAX_DEVIATION_FROM_HORIZONTAL;
   }
}
 
friends this code below, does the sprite limitation of my Weapon, but the direction of the bullet is not respecting this same limitation.

Weapon Limit angle
Code:
        var MAX_DEVIATION_FROM_HORIZONTAL = 10; //20 degrees
        var TEMPANGLE = point_direction(x,y,mouse_x,mouse_y);

           image_xscale = oPlayer.image_xscale;

           if image_xscale > 0 {
            image_angle = TEMPANGLE;
           } else {
            image_angle = TEMPANGLE + 180;
            if image_angle >= 360 {
                image_angle -= 360;
            }
           }
           if image_angle < 180 and image_angle > MAX_DEVIATION_FROM_HORIZONTAL {
              image_angle = MAX_DEVIATION_FROM_HORIZONTAL;
           } else if image_angle > 180 and image_angle < 360 - MAX_DEVIATION_FROM_HORIZONTAL {
              image_angle = 360 - MAX_DEVIATION_FROM_HORIZONTAL;
           }

create a bullets
Code:
    if (ammo[weapon] != 0)
    {
        with (instance_create_layer(x+lengthdir_x(length,direction),y+lengthdir_y(length,direction),"Projectiles",projectile))
        {
direction = other.image_angle + random_range(-1.5,1.5);
                    image_angle = direction;
            speed = other.bulletspeed;
            damage = other.damage;
                      
        }
        ammo[weapon] -= 1;
    }


with my code without limit of angle, works perfectly the creation of the bullet and the direction.

Code:
        direction = point_direction(x,y,mouse_x,mouse_y);
        if (direction > 90) && (direction < 270) image_yscale = -1; else image_yscale = 1;
  
        image_angle = direction;

the bullet always leaves in the right direction.
 
Last edited:
Top