Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

2D RTS unit selection not working

I am making a 2d RTS style game in which you have the ability to select units in game with a click and drag selection box. When ever a unit is inside of this selection box and the left mouse button is let go the unit will be added to a "selected" list and their animation will be changed from the idle animation to the selected idle animation. After the selection box is draw again all the units in the selected list will be removed and their animations set back to the idle position, then all the units inside the new selection box will be added to the list and set to the selected idle animation. The problem is that if any of these units in the new selected box had been previously selected they will just switch to the idle animation and stay there instead going to the selected animation. I tested this without animations and it worked, it would stay as the selected sprite when selected again instead of switching to the idle sprite and staying there. I am pretty new at C# so this could very well be a very simple problem to fix.

Thanks

Here is the code:

 ControllableUnits ControlUnitSelected;
 


 public List<ControllableUnits> selectedControllableUnits;
 
 private Vector3 mousePosition;

 public void Awake()
 {
     selectedControllableUnits = new List<ControllableUnits>();
     selectionAreaTransform.gameObject.SetActive(false);
     ControlUnitSelected = this.gameObject.GetComponent<ControllableUnits>();
 }
 public void Update()
 {
     //Transforming Selection Area
     if (Input.GetMouseButton(0))
     {
         Vector3 currentMousePosition = UnitControlClass.GetMouseWorldPosition();
         Vector3 lowerLeft = new Vector3(
             Mathf.Min(mousePosition.x, currentMousePosition.x),
             Mathf.Min(mousePosition.y, currentMousePosition.y)
             );
         Vector3 upperRight = new Vector3(
             Mathf.Max(mousePosition.x, currentMousePosition.x),
             Mathf.Max(mousePosition.y, currentMousePosition.y)
             );
         selectionAreaTransform.position = lowerLeft;
         selectionAreaTransform.localScale = upperRight - lowerLeft;
     }
     
     if (Input.GetMouseButtonDown(0))
     {
         mousePosition = UnitControlClass.GetMouseWorldPosition();
        
         selectionAreaTransform.gameObject.SetActive(true);
     }
     //Creating the selection process
     if (Input.GetMouseButtonUp(0))
     {



         Collider2D[] collider2DArray = Physics2D.OverlapAreaAll(mousePosition, UnitControlClass.GetMouseWorldPosition());
         foreach (ControllableUnits control in selectedControllableUnits)
         {

             //if(ControlUnitSelected != null)


             
             control.animPeasant.Play("Peasant_Idle");
             control.isSelected = false;
             
             
         }
         selectedControllableUnits.Clear();



         foreach (Collider2D collider2D in collider2DArray)
         {
             ControllableUnits controllableUnit = collider2D.GetComponent<ControllableUnits>();
             if (controllableUnit != null)
             {





                 controllableUnit.animPeasant.Play("Peasant_Idle_Selected");
                 controllableUnit.isSelected = true;
                 selectedControllableUnits.Add(controllableUnit);




             }
         }
         selectionAreaTransform.gameObject.SetActive(false);
     }
    
     
     
     

 }


Sign In or Register to comment.