I wanted to share some insight on something I came across in my implementation of this project that I thought may be useful in the current lecture on implementing a Generic TakeAction method. I used slightly different controls in my project for this course. I use the left mouse button for selecting units and actions, and the right mouse button for completing actions. I did this with an RTS game style in mind, similar to AoE. So anyway, by using the different controls, I don’t have to test for a unit already being selected in order to complete the spin action when I click on the unit itself that the valid grid position is behind. However, if I left-click the selected unit after selecting an action, it resets to the default action. Testing for the selected unit prevents that. So, I still used the test for clicking on an already selected unit. but for a different reason.
Yup you can prevent it from re-selecting and going back to the Move action.
With the Right click, how are you intending to handle multiple actions? It works in an RTS because the logic will choose what Right click means, (on top of an enemy = attack; on open ground = move), but in this game there’s a multitude of actions you can take so I’m not sure I see what you’re trying to do.
Unless you mean you’re doing literally everything the same, selecting the action from the UI, except you then use Right click to use the active action instead of Left click? If so yup that works just fine.
For this course, yes, I still have the action UI buttons to select with left-click, and then it just activates the selected action when I right-click. I still have to right-click on a valid grid position for it to do anything.
Prior to adding the UI buttons, I actually had the spin move on the O key. Then, I decided to remove the extra control when we added the UI buttons.
Here’s a snippet of code from my UnitActionSystem script:
private void Update()
{
if (isBusy)
{
return;
}
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
HandleSelectedAction();
}
private void HandleSelectedAction()
{
if (Input.GetMouseButtonDown(0))
{
SelectUnit();
}
if (Input.GetMouseButtonDown(1))
{
GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition());
if (!selectedAction.IsValidActionGridPosition(mouseGridPosition))
{
return;
}
if (selectedUnit.TrySpendActionPoints(selectedAction.GetActionPointsCost()))
{
SetBusy();
selectedAction.TakeAction(mouseGridPosition, ClearBusy);
OnActionStarted?.Invoke(this, EventArgs.Empty);
}
}
}
private void SelectUnit()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out RaycastHit rayCastHit, float.MaxValue, unitLayerMask))
{
if (rayCastHit.transform.TryGetComponent<Unit>(out Unit unit))
{
if (unit == selectedUnit)
{
return;
}
SetSelectedUnit(unit);
}
}
}