I looked around to see if this question had already been asked- maybe it has, maybe it hasn't, I don't know.

I'm trying to make a 3D RTS game. Right now we're trying to get the basics on the table. When you select a unit (I have that working) and right click on a point, the unit should move towards that point at a set speed. We can't figure out what's going wrong, but the unit never seems to move towards the clicked point, and sometimes, not at all. We used MSDN's

but that didn't seem to work. (We converted to degrees) How do you get an appropriate, consistent velocity to set to the unit so that it moves towards the ordered position?

Thanks much.

EDIT: Clarification: If I can get it to move towards a point, I can take it from there. We've got a pathing system in the works. I just want it to go where you click right now.

  • \$\begingroup\$ Like this, but with one more dimension: gamedev.stackexchange.com/questions/23447/… \$\endgroup\$
    – House
    Commented Apr 15, 2013 at 17:32
  • \$\begingroup\$ What is "RTS Style" movement? Are you asking about path finding or just straight to the point movement? Are you using steering? You need to add more details. \$\endgroup\$
    – House
    Commented Apr 15, 2013 at 19:11
  • \$\begingroup\$ Sorry, figured I was clear- select a unit, right click on a point, and the unit walks there over a given period of time. \$\endgroup\$
    – Ares513
    Commented Apr 16, 2013 at 15:07

2 Answers 2


Assuming you don't have any pathfinding constraints, what you need to do is calculate the offset from your unit's position (let's say x) to your target's position (let's say y). Then, the velocity of your unit will be y-x, normalized times the speed of your unit. You can then move your unit's position towards the target location. In pseudocode:

void update(Unit u, float dt) {

    Vector velocity = u.target - u.pos;
    float distance = velocity.magnitude();
    float dx = u.speed * dt;

    if(distance < dx) {
        u.pos = u.target;
    else {
        velocity /= distance;
        u.pos += velocity * dx;

If you are using vectors you can do something like that

Vector UnitPosition;   // position of your unit
Vector ClickPosition;  // position of your click

Vector Direction = ClickPosition - UnitPosition; 

Vector UnitSpeed = Direction * speed; // speed is your unit's speed

// Now you update position
Unit.Position.x += UnitSpeed.x * dt;
Unit.Position.y += UnitSpeed.y * dt;

This should be it. Let me know if it works / if you don't understand anything.

  • 2
    \$\begingroup\$ Keep in mind that since it's 3D, these would be Vector3's (i.e. with a z value). \$\endgroup\$ Commented Apr 15, 2013 at 16:51
  • \$\begingroup\$ How do I get my click position into a vector3 that's accurate? I've done something exactly like this for 2D but I can't seem to figure it out in 3 dimensions. \$\endgroup\$
    – Ares513
    Commented Apr 16, 2013 at 15:09
  • \$\begingroup\$ I've figured out the mouse problem. Now the problem is whenever I right click on a point, it seems to reverse the sign of the vector. It's negative on one click and positive on another, even though the mouse position is the same. \$\endgroup\$
    – Ares513
    Commented Apr 16, 2013 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .