I was following the course and I had several issues at this point.
I am new to unity, so I have no idea if they are due to changes in unity or something else.
In any case, those are the issues I have encountered:
- Selection box was offset quite far (out of screen)
- Unit spawing was no longer working when clicking the spawner (box);
The first issue was already mentioned by someone else in another post.
The solution is simply to set the anchor point to the lower left corner.
The second issue was trickier.
As far as I can guess (being a beginner at Unity), is that the UI is “eating” the input click.
I guess there are several way to fix the issue. Changing the way UnitSpawner detects click (not using IPointerClickHandler) is one.
However, I opted to change the logic of the selection in a way that makes more sense for an RTS in my opinon.
I changed:
private void StartSelectionArea()
{
startPosition = Mouse.current.position.ReadValue();
}
to only save the start position, it does not activate the selection game object.
SelectionArea checks for the area to be sensible before activating the game object (so it would be possible to control how small the minimum box selection is)
private void UpdateSelectionArea()
{
Vector2 mousePosition = Mouse.current.position.ReadValue();
float areaWidth = mousePosition.x - startPosition.x;
float areaHeight = mousePosition.y - startPosition.y;
if (!unitSelectionArea.gameObject.activeSelf && (Mathf.Abs(areaWidth) > 1f || Mathf.Abs(areaHeight) > 1f))
unitSelectionArea.gameObject.SetActive(true);
unitSelectionArea.sizeDelta = new Vector2(Mathf.Abs(areaWidth), Mathf.Abs(areaHeight));
unitSelectionArea.anchoredPosition = startPosition + new Vector2(areaWidth, areaHeight) / 2f;
}
I used 1 as limit value, but it might be “cleaner” to have it as a serialized field so that can be configured from the inspector.
Finally, I changed the check in ClearSelectionArea to check if the area image is active. I also moved the deselection of the previous unit at that point, but that is purely personal preference.
private void ClearSelectionArea()
{
// Deselect old selection (personal preference)
foreach (Unit selecedUnit in SelectedUnits)
{
selecedUnit.Deselect();
}
SelectedUnits.Clear();
if (!unitSelectionArea.gameObject.activeSelf)
{
// Single selection
// ... same code as explained in the lesson
}
else
{
// Disactivate selection image
unitSelectionArea.gameObject.SetActive(false);
// Multiple selection
// ... same as in the lesson
}
// Activate selected units
foreach (Unit selecedUnit in SelectedUnits)
{
selecedUnit.Select();
}
}
Hope this helps people with similar issues