Cycling through units with TAB

I finished the course and was wondering if it is possible to cycle through the player units using TAB? I tried figuring it out but couldn’t get anything to work. I thought putting the friendly units in an array and cycling through them would work but I couldn’t get it functioning. I would appreciate any help. :smiley:

This method could be added to UnitActionSystem to cycle the units:

    public void CycleSelectedUnit()
    {
        if (TurnSystem.Instance.IsPlayerTurn())
        {
            int index = UnitManager.Instance.GetFriendlyUnitList().IndexOf(GetSelectedUnit());
            index++;
            if (index > UnitManager.Instance.GetFriendlyUnitList().Count)
            {
                index = 0;
            }
            SetSelectedUnit(UnitManager.Instance.GetFriendlyUnitList()[index]);
            
        }
    }

Mine looks pretty much the same.

I only wanted to select units with AP.

private void TrySwitchToNextAvailableUnit()
{
    List<Unit> unitsWithAP = UnitManager.Instance.GetFriendlyUnitsWithAPList();
    int unitIndex = 100;
    if (selectedUnit != null)
    {
        unitIndex = unitsWithAP.IndexOf(selectedUnit);
    }
    if (unitIndex + 1 < unitsWithAP.Count && selectedUnit != null)
    {
        SetSelectedUnit(unitsWithAP[unitIndex + 1]);
    }
    else
    {

        SetSelectedUnit(unitsWithAP[0]);
    }
    SetCancelFlag(); //ignore this, as it makes sure it closes my attack dialogue if i switch units
}

Easy fix: First, you’ll need to add using System.Linq; to your usings clauses.

    public void CycleSelectedUnit()
    {
        if (TurnSystem.Instance.IsPlayerTurn())
        {
            var unitList = UnitManager.Instance.GetFriendlyUnitList().Where(u => u.GetActionPoints() > 0).ToList();
            if (unitList.Count == 0) return;
            int index = unitList.IndexOf(GetSelectedUnit());
            index++;
            if (index > unitList.Count)
            {
                index = 0;
            }
            SetSelectedUnit(unitList[index]);
            
        }
    }

I like that better then what I did.

private void Unit_OnAnyActionPointsChanged(object sender, EventArgs e)
{
    UpdateFriendlyUnitsWithAPList();
}

private void UpdateFriendlyUnitsWithAPList()
{
    friendlyUnitsWithAPList.Clear();
    foreach (Unit unit in friendlyUnitList)
    {
        if (unit.GetActionPoints() > 0) friendlyUnitsWithAPList.Add(unit);
    }
}

and just constantly stored and rebuilt the list

Thank you so much you guys! :smiley: I added the ability to cycle through the units and have the camera jump to each one.

One thing i noted, was it was reel jarring if the unit was already on screen.

So i added this to the camera controller.

if the unit is already in the camara frame, don’t jump to it.

private void UnitActionSystem_OnSelectedUnitChange(object sender, EventArgs e)
{


    Vector3 positionToCheck = UnitActionSystem.Instance.GetSelectedUnit().transform.position;
    Vector3 viewportPosition = Camera.main.WorldToViewportPoint(positionToCheck);

    if (viewportPosition.x >= 0 && viewportPosition.x <= 1 && viewportPosition.y >= 0 && viewportPosition.y <= 1)
    {
        //Unit is already on screen, no need for an abrasive switch
        return;
    }
    else
    {
        //Unit is not on screen, move to position
        transform.position = positionToCheck;
    }
    
}

Privacy & Terms