4
\$\begingroup\$

First of all, if you know a better way to do what I want to achieve please don't hesitate to suggest your own method.

What I want to do is the classic area selection in RTS games, which player drags his mouse to draw a rectangle and units inside rectangle become selected.

At this point, I have two ideas:

  1. A dedicated gameObject with a trigger BoxCollider2D.
  2. Detecting overlaps with Physics2D.OverlapArea method.

In the first method, the gameObject will get resized on the fly while the user drags and by using collision control the objects get selected.

In the second method starting point and end point of the area is being recorded and script draws an OverlapArea using these two coordinates.

Which one would be more effective in terms of performance?

Thanks in advance.

\$\endgroup\$
4
\$\begingroup\$

I would definitely go with the second method. In this manner, you don't have to have anything firing in your physics engine, or have to deal with disabling/enabling colliders when you are not using them.

The second method also has the added benefit of having Point1 and Point2 parameters, which coincide with your drag/draw selection interface. You are going to start dragging at Point1 and release at Point2.

Using the second method for this mechanic is the way to go.

\$\endgroup\$
0
0
\$\begingroup\$

I would definitely look at this link: RTS style Unit selection

It is very well done. You will need to do a little bit of optimalization yourself tho.

I use it in my RTS game and it works much better than the other solutions i tried.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This answer currently leans very heavily on the link. If the URL for that article ever changes, or becomes unavailable, then users viewing your answer won't be able to glean the solution you're proposing. Please consider editing to include at least a rough summary inside your answer itself, so that it can stand on its own, and include the link for credit & further reading. \$\endgroup\$ – DMGregory Apr 29 '18 at 16:06
  • \$\begingroup\$ Will do when I get home :) fair point! \$\endgroup\$ – Lucián Blažek Apr 29 '18 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.