Following a straight line

Hi all, I’ve been tinkering with this for a while and can’t seem to find an answer. What I want is for the player character to move when the right mouse buttons is held down, and to move towards where the buttons is pressed. I’ve gotten a simple version done, but not one where the character goes directly to where the mouse was clicked.

The player has an x, y, dx, dy, goingX and goingY variables. at each frame im adding dx to x and dy to y. So it’s all about getting dx and dy to the right values.

I hope that made sense. Anyone care to offer some help?

Find the vector from the current position to the target position, normalise it, then scale it based on your target speed:

float dx = goingX - x;
float dy = goingY - y;
float length = Math.sqrt(dxdx + dydy);
dx /= length;
dy /= length;
dx *= speed;
dy *= speed;

Hey, works like a charm! Thanks for that. :slight_smile: