How do I change the direction of a sprite with the cursor?

Hi, I am new in gdevelop and I created a sprite that move forward when you press W and move backwards when you press S. Now, I want to change his direction with the mouse pointer. Example: I keep W pressed and it move forward, then I move the cursor on the left of the prite and it change direction moving left (keeping the W pressed). I have no idea how to do that. Ask if I wasn’t being clear

Do you just want 4 way movement (left/right/up/down), or in the direction of the mouse?

I want that pressing W, the object start to following the mouse. I have only 2 buttons: W = forward and S= backwards. Pressing one of them the object start to move in the direction of the mouse

This is what i did for the object but it moves strangely

Can you describe what you mean by moves strangely?

And why is there an always in the condition. I thought you want the movement when the w key is pressed?

i try to make a video

!

it move without touching the buttons (the cursor is on his right wing, bottom right). If i press W or S he do strange movements like he moves right and left. Also if I don’t move the mouse or i don’t touch the buttons he start trembe.
The site doesn’t allow me to upload the video.
The “always” is there becouse if i put W it moves only up

The problem is with the first two events, the one that deal with the w and s key being pressed. All they are doing is forcing Nave to move up and down the the screen, not at the angle it’s rotated.

Replace the action of the w key pressed event with the move Nave action in the last event into the actions of the first event (drag and drop it, because you only want it in the first event).

Add the same move Nave action to the s key pressed event, but make the force negative.

I believe this is all you’ll need to do to get your object following the mouse when w is pressed.

I did this but the object moves sideways, on his right wing like this:
the white point is the mouse pointer

Yes, that’s because 0 degrees on an object is to the right, not straight up. It’s a tad confusing.

The best and simplest way to fix this is to rotate your initial image (the one saved on your drive) in an image editor, so it points to the right.

The alternative is to subtract 90 from any of the object’s angles. This can get messy, because you have to remember to do this calculation every time you use the object’s angle or rotation. I’d recommend the first way.

1 Like

thank you so much. I’m gonna try to do the first way. :smiley:

1 Like