# How to make an object towards the mouse on one Axis - Gamemaker Studio 2

I am trying to have my "obj_playership" move towards the Cursor on a single Axis. Prefer it to be the Vertical Axis. I tried doing this is an "Execute Code Box" in a step event

"direction = mouse_x" "speed = 1 " but the movement looks weird and does not do what I am trying to do.

To better refine my question I'm trying to have my "obj_playership" move just up and down in relation to the mouse, so if I'm moving my mouse up or down the obj_playership" will follow towards the mouse either up or down.

Just check where is the mouse based on the object's position:

if (mouse_y - y) > 0 {
y+=1;
} else if(mouse_y - y) < 0 {
y-=1;
}


This same code could be written in different ways but are a little bit complex to understand XD

y += sign(mouse_y - y);


The "direction" must be an angle, that's why it looks weird like this.

• The first part is what I Was Trying to do thanks the second part is helpful too if I wanted something more instant. – sketcherskt Apr 17 '19 at 1:38
• If you think it moves slow you can use your own variable for speed. I've repaired a mistake on the second form – Juanpa Apr 17 '19 at 14:55