GameMaker Rotating an Object to Follow the Mouse - HELP

So I have this object that I plan to have "stick" to another object. Anyways, I want it to point in the direction of the cursor, I kind of have it down from following instructions from another post.

The only thing is that it just points in the four directions rather than staying and following the mouse cursor exactly, I basically want it to rotate as the mouse moves as a constant. I hope that makes sense.

Now I only have four-directional sprites for this, but I'll work on that later to make it look better, but I'm trying to have the mechanics down first at least. (I'm bad at art, so...)

Here's a copy of the code I have so far:
GML:
//STEP EVENT

var _player_riding = false;

with (obj_player)
    {
    if (player_transportation == move_states.RIDE) 
       { // Check if the player is in ride state
       _player_riding = true; 
       } 
    }


if _player_riding = true
{
    //mouse directional rotation
    var md = point_direction(x, y, mouse_x, mouse_y);
    if (md >= 45 && md < 135) {
      sprite_index = spr_gatty_turret_up;
    } else if (md >= 135 && md < 225) {
      sprite_index = spr_gatty_turret_left;
    } else if (md >= 225 && md < 315) {
      sprite_index = spr_gatty_turret_down;
    } else {
      sprite_index = spr_gatty_turret_right;
    }
}
I only have a step event right now... Not sure what else I need for this yet.

Thanks in advance!
 
Last edited:

Slyddar

Member
If you set the sprite of the object to spr_gatty_turret_right (as that is 0 degrees), and just set the image_angle to md directly, it will always rotate towards the mouse.
 
Alright, I think that did it, I need to tinker with it a little but it seems to have done the trick!

Now, I don't if anyone can help me with this other problem I have with this. I have the object "sticking" to another, the object on top is supposed to shoot projectiles, but when it does it moves the object on top to behind the object on the bottom and then you can't see the projectiles.

Here's the copy of the code I'm working with:
GML:
//STEP EVENT
if mouse_check_button(mb_left)
        {
        image_index +=0;
        _shoot = true;
        
        image_angle = point_direction(x, y, mouse_x, mouse_y);
        //shoot
        if _shoot == true
            {
            bullet = instance_create_depth(x, y, bbox_bottom, obj_gatty_bullet);
            bullet.speed = 5;
            bullet.direction = image_angle;
            }
        } else
            {
            _shoot = false;
            image_index = 0;
            }
 

Nidoking

Member
image_index +=0;
_shoot = true;
image_angle = point_direction(x, y, mouse_x, mouse_y);
//shoot
if _shoot == true
There are a lot of fundamental misunderstandings here that you need to clear up by learning how GML works.

Adding zero to a number has no effect. That's not even GML-specific. That's math.
Your if _shoot == true has no meaning because you just set _shoot to true. _shoot is always true, so there's no if about it.
You're using bbox_bottom of self as the depth parameter to create your instance. Is this some sort of faux-3D effect? You're converting a coordinate in the XY plane to a Z coordinate of sorts. That may be what you want, but if you don't understand the fundamentals I've already mentioned, I think you might just not understand depth either.
 
There are a lot of fundamental misunderstandings here that you need to clear up by learning how GML works.

Adding zero to a number has no effect. That's not even GML-specific. That's math.
Your if _shoot == true has no meaning because you just set _shoot to true. _shoot is always true, so there's no if about it.
You're using bbox_bottom of self as the depth parameter to create your instance. Is this some sort of faux-3D effect? You're converting a coordinate in the XY plane to a Z coordinate of sorts. That may be what you want, but if you don't understand the fundamentals I've already mentioned, I think you might just not understand depth either.
Yeah, I just started watching some videos explaining some of this so I can get a better understanding, I was tweaking some code someone was helped with, in a similar situation and trying to make it work to get a better visual for myself. I'm definitely learning a lot as I go.

I was trying to get my object to play through all the animation and the only way for it to work was when I would click was to do that I found just messing around, that's why I ended up doing image_index +=0;... I wasn't sure how to do it all but that seemed to do what I was looking for.

I was trying to get the bullet and the object on top to stay on top of the bottom object and I was messing around with different depths.

I was honestly just throwing things together how I thought it would work. I guess I should basically scrap it all and try something else, lol.

I basically just want it to rotate in the direction of the mouse and shoot the projectiles in that direction, it just got a little complicated for me when I placed it on top of another separate object.

Thanks for the help! I'm getting a better idea!
 
Last edited:

Nidoking

Member
I was trying to get the bullet and the object on top to stay on top of the bottom object and I was messing around with different depths.
Determining which instance is "on top" is what layers are good for. You can make layers for things that need to be at particular depths relative to each other and just put them in the appropriate layer and never have to worry again.
 
Determining which instance is "on top" is what layers are good for. You can make layers for things that need to be at particular depths relative to each other and just put them in the appropriate layer and never have to worry again.
Alrighty! Thanks so much for your help, that helped.

I've refined what I had and here's a copy of it:
GML:
if _player_riding = true
{
    //mouse directional rotation
    var md = point_direction(x, y, mouse_x, mouse_y);
    var flipped = (mouse_x > x) *2-1;
    var gun_x = x+20*flipped;
    var x_offset = lengthdir_x(44, md);
    var y_offset = lengthdir_y(44, md);
    
    if (md >= 45 && md < 135) {
      sprite_index = spr_gatty_turret_up;
    } else if (md >= 135 && md < 225) {
      sprite_index = spr_gatty_turret_left;
    } else if (md >= 225 && md < 315) {
      sprite_index = spr_gatty_turret_down;
    } else {
      sprite_index = spr_gatty_turret_right;
    }
    
    if mouse_check_button(mb_left)
        {
        _shoot = true;
        instance_create_layer(gun_x + x_offset, y + y_offset, "Instances_2", obj_gatty_bullet);
        } else
            {
            _shoot = false;
            image_index = 0;
            }
}
I still need to work on it... the projectiles don't really match the barrel, but I'm trying to tweak it still.
 
Top