RTS style selecting box and map scrolling

Hey there!

I’m currently working with RTS-style selecting box, which is easy to implement since there are a lot of topics about this feature. What the problem is that I couldn’t find a solution for selecting range of units while scrolling the map in any direction.

Here’s an attached image that shows the problem:

As you can see when I’m dragging the box to the bottom and scrolling down the map, box startPoint is not updated. What do I want to do is to move the startPoint vector respectively to the scroll speed.

I’ve tried couple of methods like Raycasting, ScreenToWorldPoint etc.

This code selects startPoint of selection box and saves the hit point with the terrain (which is not necessary, this was just another try out).

if (Input.GetMouseButtonDown(0)) {
            startPoint = Input.mousePosition;

            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(startPoint);

            if (Physics.Raycast(ray, out hit, 1000)) {
                if ("MapArea" == hit.collider.tag) {
                    startClickWorldPoint = hit.point;
                }
            }
        }

…and here’s an example of the code, when the map is scrolled and the base condition here is true when selectionbox has been marked. This is an example of another try out since I cannot find any good solution.

if (...) {
                startClickWorldPoint = Vector3.Lerp(
                    startClickWorldPoint,
                    startClickWorldPoint + new Vector3(
                        xMoveValue,
                        0,
                        zMoveValue
                    ),
                    screenScrollSmoothFactor
                );

                startPoint = Camera.main.WorldToScreenPoint(new Vector3(
                    startClickWorldPoint.x,
                    0.1f,
                    startClickWorldPoint.z
                ));
            }

Once I’ve tried to move the selectionbox (startPoint) with the size of xMoveValue (float variable, moves the camera to left/right) and zMoveValue (float, top/down).
Just to notice - lerp is not necessary here.

I’ve also attached the whole camera script.

So the question is how to make it work properly? How should I update the startPoint or maybe there is something wrong with my code.

Thanks in advance!
AmBeam.

1903566–122800–CameraOperator.cs (5.15 KB)

You need to always keep a reference to the start point in world space. Once you’ve started dragging this will never change. You should never be updating start point based on screen space.

Basically - all of your comparisons need to be in world space and you should only convert to screen space when you’re about to draw your selection bounds texture.

The other option is to lock camera movement when a player is drag selecting. That’s what we do :slight_smile:

1 Like

Ok, I’ve finally figured it out! Thank you so much for Your advice.
I’ve recalculated the screen point right before drawing the box, also “startPoint” is now a world point instead of screen point. This needn’t have any updates in scrolling method :). Whoa!

If anyone got similar problem, I’ve attached an updated working code (got other bugs, but the topic’s feature works good).

1903666–122806–CameraOperator.cs (4.59 KB)