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();
}