Selecting multiple units doesn't work for me

I’m running into issues where the units are in the selection area, but the units aren’t being selected. I literally have to select the entire screen.

I’ve gone over other topics where users had issues like the selection box being off, which I battled with too.

Things I’ve tried:
Set the canvas on which the Graphics RayCaster is on to be the same settings as the canvas in which the DragBox_Image is in.

Moved the DragBox_Image into the canvas that the Graphics Raycaster is on

Played with the Canvas Scaler settings since as a general rule I always set my target canvas size to 1920x1080 which made the DragBox_Image to be offset again.

Deleted Both canvases and the DragBox_Image and redid everything to be exactly the same as in the video.

Reviewed my code and made sure its the same as in the video and in the GitLab files.

Edit: I just realized after testing that the multi-selection does not work for the client. I’m not sure what I’m doing wrong but it is starting to feel like this project is falling apart.

Screenshots:



Hi there,
I would stick with the canvas and DragBox setup as is done in the video with the UnitHandler as the parent.
image
Canvas


Drag Box

Make sure the Rect Transform on both have the same settings.

If this is just a client only issue, it is more likely not an issue with the Drag Box. Please check if Units are being added to the SelectedUnits list in the UnitSelectionHandler. If they are not being added there, the issue is with in that class, or with the cavnas setup, most likely. If they are being added there, you could have an issue with the highlight or movement or something else.

Hi there, thank you so much for taking the time to reply.

At the time of sharing the screenshots was after many many attempts to get it to work. I’ve gone and reverted all of my changes back to what it was in the video and still I had an issue with the selection not working as intended.

I’ve posted a recording of the behaviour I’m experiencing on Discord. I’m not sure if you are on Discord but if you are and you don’t mind feel free to @Dekiru#5051 and I’ll show you a video of the behaviour.

But to quickly explain:

The selection box is being created at the point of my mouse, but when trying to select a unit nothing happens. If I create a smaller selection box offset to the far left then it actually selects the units. I can confirm that the selection works because I can see my units in the list of selected units.

If I select the entire screen then all of my units are selected.

I checked out your video. It looks like it’s offset to the right? Does that seem right? Is it consistently offset the same amount? Perhaps there is a negative sign in the wrong spot or something. You you share a picture of your UnitSelectingHandler in the inspector?
I would recommend using gizmos to visualize the raycasting behaviour.
If you’d like you can also upload your project here:
https://gdev.tv/projectupload
I can take a look at and see if I can spot what is causing the offset.

Hi there, I’ve got my project on a GitHub repo and I’ve been committing at the end of each video, I just checked out the commit for the video “Selecting Multiple Units” and confirmed that my settings are identical to what you provided in the screenshot.

If you prefer I can share my repo link here for you to check out, unless I have to upload my project using the link above you mentioned.

Here’s the repo: https://github.com/ZeroCool5254/RTS-Multiplayer-Course/tree/dev

Hi there,
For starters, I see your canvas scaler is set to scale not to constant pixel size. Then you seem to be doing some math to adjust for the scale.
If I replace your selection code with the code from the course, and change the dragbox canvas to constant pixel size, then the unit selection works correctly.

Did you check out the latest commit or the one from the video. I’ve gone through the video again and I still can get it to work. I’ll go through everything again and follow your advice from the screenshots and hope for the best.

Thank you for taking the time to help me :slight_smile:

I copied in the code from my project, but it looks the same as GameDev.tv’s latest commit for lecture 9.
Your code I pulled from the git project had a localScale value you were using to adjust for scale that I think was the cause of the problem.

private void UpdateSelectionArea()
    {
        Vector2 mousePosition = Mouse.current.position.ReadValue();

        float areaWidth = mousePosition.x - _startPosition.x;
        float areaHeight = mousePosition.y - _startPosition.y;
        
        **Vector2 localScale = canvasScaler.transform.localScale;**
        unitSelectionArea.sizeDelta = new Vector2(Mathf.Abs(areaWidth), Mathf.Abs(areaHeight)) / localScale;
        unitSelectionArea.anchoredPosition = (_startPosition + new Vector2(areaWidth / 2, areaHeight / 2)) / localScale;
    }

After the initial issue with the selection offset I started playing with settings and trying to fix it myself. I looked online for tutorials on unit selection and came across a Zenva video that’s nearly identical to the lecture from the course. In the comments of the video people were also having problems with the selection box being offset.

Someone made a suggestion using the canvas scaler and that didn’t work either. I’m going to attach a few screenshots of my current project setup as it was from the lecture "Selecting Multiple Units. As well as the code from the UpdateSelectionArea() method.



Note that in the screenshots you sent me, the canvas has a width of 1920 and height of 1080. I’m unable to get that width or height due to the fact that I have a monitor with a resolution of 1366x768 and is incapable of a resolution of 192x1080. For the spirit of testing things I’ll attach my TV to my PC which is capable of a resolution of 1920x1080 and see if the problem persists.

The resolution should have no effect since we are using a constant pixel size. By updating the code to that of the course, and changing the canvas scale to constant pixel size I was able to get it working in your project.
So I believe the issue resides somewhere in your UnitSelectionHandler code.

Going through your code again there are a few other differences from the course code.

  1. The start method is currently a coroutine
  2. Your minimum size check on line 75 is:
    Math.Abs(unitSelectionArea.sizeDelta.magnitude) < 0.5f
  3. There is a typo on line 101 where you compare screenPosition.y to the min.x instead of min.y

I am sooo sorry, the whole issue the whole time was in fact the typo on line 101 where I should’ve used min.y instead of min.x

I would like to add that the difference between Math.Abs(unitSelectionArea.sizeDelta.magnitude) < 0.5f and unitSelectionArea.sizeDelta.magnitude == 0 is that Jetbrains Rider complains about “Equality comparison of floating point numbers. Possible loss of precision while rounding values” and suggests I change it to what I have. and the < 0.5f is a tolerance value. That has no effect on the selection that was off.

I am sorry that I took you through an entire rabbit hole of my project not being the same as the video. I know it is actually best to have the code and project set up identical to the instructor’s code, but I can’t help myself sometimes with the Rider sugestions.

Again thank you so much for your help :slight_smile:

1 Like

No worries, making changes to the code is good practice, you shouldn’t be afraid to do that. Floating point comparisons are a real problem!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms