Now for the Banker… This is going to require a slightly different solution, because we’re simply turning on a UI when we want to open a bank (much like the Inventory itself). So we’re going to have to create a new class to add to the player.
public class BankAction : MonoBehaviour, IAction
Of course, it’s an IAction, so we’ll need a Cancel() method. Also, like the other walk to classes, we’re going to need a target. For this one, though, we’re simply going to have a Transform as the target.
public void Cancel()
This takes care of the cancelling, but only the moving to part of the cancelling. It doesn’t take into account that the windows might be open. To facilitate this, we’re going to create a static method in ShowHideUI that actually closes ALL the windows.
public static void CloseAllWindows()
foreach (ShowHideUI hideUI in FindObjectsOfType<ShowHideUI>())
Now our Cancel method can call
public void Cancel()
Now that that’s out of the way, we can create our action to initiate the BankAction. We’re going to use a callback technique to initiate our action when we’ve reached our destination.
public void StartBankAction(Transform bankTarget, System.Action callback)
if (bankTarget == null) return;
this.callback = callback;
target = bankTarget;
Next up, we need to head to OtherInventory to modify IHandleRaycast.
In the section where the Mouse is down, rather than opening the window directly, we’re going to pass along an anonymous function to the StartBankAction method.
public bool HandleRaycast(PlayerController callingController)
if (showHideUI == null) return false;
callingController.GetComponent<BankAction>().StartBankAction(transform, () =>
Finally, it’s time for our challenge!
We need an Update() method in BankAction that, if you have a target moves to the target, and when it is in range (say 3?), invokes the action.
You can invoke the action with
callback?.Invoke(); and it automatically null checks.
Since we passed a Transform as the target, you can use target.position instead of target.transform.position in the Distance calculation.