Switching targets while in TargetingState?

How would you go about switching targets with either the gamepad’s RS (right stick directional flick), or something like the keyboard+mouse’s Shift+WASD or Alt+WASD?

Well, the first part is setting up the buttons in the InputManager… I think you can handle that… you should wind up with two events

public event OnTargetLeft;
public event OnTargetRight;

That’s the easy part.
The hard part is selecting the target to the left or right of the current target…

This is untested and off the cuff…

    public void SelectTargetToTheLeft()
        if (!CurrentTarget)

        var position = transform.position;
        var forward = transform.forward;
        float closestDistance = 1000;
        Target candidate = null;
        float angle = Vector3.SignedAngle(forward, CurrentTarget.transform.position - position,
        foreach (var target in targets.Where(t=>Vector3.SignedAngle(forward, t.transform.position, Vector3.up)<angle))
            if (Vector3.SqrMagnitude(target.transform.position - position) < closestDistance)
                closestDistance = Vector3.SqrMagnitude(target.transform.position - position);
                candidate = target;
        if (candidate != null)
            CurrentTarget = candidate;

And for the Right, you’ll want to test for a > angle.

Of course, this means we now have three distinct select methods… so there’s probably some heavy DRY refactoring to be done.

Privacy & Terms