# [C#] Right click move help

This topic is 2694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello there,

I'm trying to develop some simple RTS functions and the one I'm trying to do right now is the right-click move physic.
The thing with my code is it makes my unit move right on the first right click wherever I click on the screen. It starts to
go wrong on the second click and beyond... A problem I probably figured out but I can't seem to see why it's happening
is that the unit is increasing in speed each time I right-click.

Here's my code:

 // The unit is already selected via left-click. if (_input.Mouse.RightPressed == true) { if (_testunit._selected == true) { _vectorDest = new Vector(_input.Mouse.Position.X, _input.Mouse.Position.Y, 0); _xDiff = _vectorDest.X - _testunit._sprite._positionX; _yDiff = _vectorDest.Y - _testunit._sprite._positionY; } } if (_xDiff > 0 && _yDiff > 0) { if (_testunit._sprite._positionX < _vectorDest.X && _testunit._sprite._positionY < _vectorDest.Y) { _testunit.Move(_vectorDest * elapsedTime); } } if (_xDiff < 0 && _yDiff < 0) { if (_testunit._sprite._positionX > _vectorDest.X && _testunit._sprite._positionY > _vectorDest.Y) { _testunit.Move(_vectorDest * elapsedTime); } } if (_xDiff > 0 && _yDiff < 0) { if (_testunit._sprite._positionX < _vectorDest.X && _testunit._sprite._positionY > _vectorDest.Y) { _testunit.Move(_vectorDest * elapsedTime); } } if (_xDiff < 0 && _yDiff > 0) { if (_testunit._sprite._positionX > _vectorDest.X && _testunit._sprite._positionY < _vectorDest.Y) { _testunit.Move(_vectorDest * elapsedTime); } } 

I know this is probably bad but I just have so much difficulty following why it's doing that.
I also think that the _xDiff and _yDiff are off because sometimes the position of the sprite of the unit
is 100.0875... instead of exact 100 (X or Y). So maybe I can give it a range of the X destination + 0.99 so it takes the
decimals in account...

I'm quite lost and It would be very kind if someone could enlighten me...

Thanks!

PS: The engine I'm using is the one I made with the help of a book. It was going very well with the 2 shooters I made so I think it is great.
The book in question is "C# Game Programming: For Serious Game Creation".

##### Share on other sites
Ok to try to easy this a bit i'll give you the new code I made. With it, I've found that the problem is when I change the coordinates of the Vector it still takes in account as if the origin is 0,0 so the unit still move from 0,0 to the new Vector.

This is the code now :

 private void UpdateInput(double elapsedTime) { // simply selecting the unit. if (_input.Mouse.LeftPressed == true) { if (_testunit.GetBoundingBox().IntersectsWith(new RectangleF(_input.Mouse.Position.X, _input.Mouse.Position.Y, 1, 1))) { _testunit._selected = true; _testunit._sprite.SetColor(new Engine.Color(0, 1, 0, 1)); } } // setting the vector coordinates on each right click. if (_input.Mouse.RightPressed == true) { if (_testunit._selected == true) { _vectorDest.X = _input.Mouse.Position.X; _vectorDest.Y = _input.Mouse.Position.Y; } } // what I use here is something that must eventually come close to 0 because it's like a // measurement between the destination and the current position of the unit. _xDiff = _vectorDest.X - _testunit._sprite._positionX; _yDiff = _vectorDest.Y - _testunit._sprite._positionY; if ((_xDiff > 0.5 || _xDiff < 0) && (_yDiff > 0.5 || _yDiff < 0)) { _testunit.Move(_vectorDest * elapsedTime); } else { _testunit._sprite.SetPosition(Math.Round(_testunit._sprite.GetPosition().X, 0), Math.Round(_testunit._sprite.GetPosition().Y, 0)); } } 

Keep in mind that the main problem I have is that the unit always move from the origin when it shouldn't. So that makes him stop at an offset location from where it's supposed to be.

If someone is willing to help, I can provide pictures to show you what I'm talking about. I think it's quite a simple problem but I just can't get it since I'm new to game programming.

Thanks!

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
18
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632583
• Total Posts
3007212

×