I have a texture drawn on screen. Whenever I click on the viewport it remembers its destination and starts moving towards it, until it is there. This is the code I am using therefore:
//Check if destination was reached
if ((currentDestination - position).Length() <= movementSpeed)
{
// Stop moving
position = currentDestination;
playerState = PlayerState.Idle;
reachedDestination = true;
}
// If destination is not reached
if(!reachedDestination)
{
Vector2 direction = (currentDestination - position); // gives the direction needed to travel from position to target
angle = (float)(Math.Atan2(direction.Y, direction.X) + MathHelper.PiOver2);
direction.Normalize(); // we need it as a unit vector
position += direction * movementSpeed;
// The Y coordinate stays the same in this case
position.Y = 455;
}
It does work, but the closer the texture is to the destination, the slower it gets. Why is that?
movementSpeed
isn't changing? If it's being changed elsewhere, becoming smaller, that would cause the speed of the texture to be affected. \$\endgroup\$Vector2.Normalize
wasn't working (that's an XNA provided method, not user). Regardless, definitely worth debugging and making sure a unit vector is being set. \$\endgroup\$