# XNA moving towards 3D point, RTS style

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
-CSng(Math.Sin(unitRotation))

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.

• Like this, but with one more dimension: gamedev.stackexchange.com/questions/23447/… – MichaelHouse Apr 15 '13 at 17:32
• 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. – MichaelHouse Apr 15 '13 at 19:11
• Sorry, figured I was clear- select a unit, right click on a point, and the unit walks there over a given period of time. – Ares513 Apr 16 '13 at 15:07

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;
Direction.Normalize();

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.

• Keep in mind that since it's 3D, these would be Vector3's (i.e. with a z value). – Richard Marskell - Drackir Apr 15 '13 at 16:51
• 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. – Ares513 Apr 16 '13 at 15:09
• 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. – Ares513 Apr 16 '13 at 15:31