How I implemented the action camera manager

I thought the switch statement for the CameraManager would get really unruly (I plan on an action camera for almost every action).

Here is how I refactored it so that the Action implementation move the camera, but the camera manager still shows or hides the action camera.

I’m curious on your thoughts on the refactor, and if I was missing any pitfalls.

BaseAction.cs

public virtual bool TrySetActionCamera(GameObject actionCameraGameObject)
{        
        return false;
}

ShootAction.cs

public override bool TrySetActionCamera(GameObject actionCameraGameObject)
    {
        Unit shooterUnit = GetUnit();
        Unit targetUnit = GetTargetUnit();

        Vector3 shootDir = (targetUnit.GetWorldPosition() - shooterUnit.GetWorldPosition()).normalized;

        Vector3 cameraCharacterHeight = Vector3.up * 1.7f;
        float shoulderOffsetAmount = 0.5f;
        Vector3 shoulderOffset = Quaternion.Euler(0, 90, 0) * shootDir * shoulderOffsetAmount;
        Vector3 actionCameraPosition = shooterUnit.GetWorldPosition() +
            cameraCharacterHeight +
            shoulderOffset +
            (shootDir * -1);

        actionCameraGameObject.transform.position = actionCameraPosition;
        actionCameraGameObject.transform.LookAt(targetUnit.GetWorldPosition() + cameraCharacterHeight);

        return true;
    }

CameraManager.cs

public void BaseAction_OnAnyActionStarted(object sender, EventArgs args)
    {
        if (sender is BaseAction baseAction)
        {
            if (baseAction.TrySetActionCamera(actionVirtualCameraGameObject))
            {
                ShowActionCamera();
            }
        }
    }
    public void BaseAction_OnAnyActionCompleted(object sender, EventArgs args)
    {
        HideActionCamera();        
    }
1 Like

That’s an interesting approach, leaving the set up to the action itself.

The main question is on all the action cameras that you are planning, do they all share that same VirtualCamera setup? If so then yup that works.
But if you want different action cameras to have different settings, then you’re still going to need to handle all those action camera objects and select which one the current action wants. Perhaps also leave that logic up to the Action itself and just pass in a List of all Action Cameras and let the Action select which one.

So yup that works perfectly, just depends on how much more complex you want the action camera system to be.

1 Like

Privacy & Terms