There can be several ways. I can suggest one, although it is a little laborious, but it should work well:
You can use a "morph" style skill that doesn't have a timer between unit transformations.
As an example of the Gargoyle, as if it were to be a passive use skill (removing time for Casting, no Cooldown or spending for Mana).
You can change the "Art-Button Position X/Y" to hide the Skill visibility by shifting to a hidden slot.
In each of the two units you make the changes, one would be with distance attributes and with the Archery model and another unit would be Melee, with the model using sword.
Then creating a trigger system that activates the use of the ability alternately according to the presence of enemy units by "X" away from your unit. Activation of the transformation would be the requirement if there are enemy units nearby, activating the Melee unit. If there are no enemy units nearby, it would activate the transformation to the type Range unit.