Looks like I had to also handle the "OnCameraChangePerspectives" or I wouldn't be able to return to state.
I originally did what you asked without the "OnCameraChangePerspectives" but I kept being stuck in FP mode. I added "OnCameraChangePerspectives" after and tracked the bool there. It works but I think there is a more elegant solution here?
using Opsive.Shared.Events;
using Opsive.Shared.Utility;
using Opsive.UltimateCharacterController.Camera;
using UnityEngine;
public class ZoomPerspective : MonoBehaviour
{
private CameraController _cc;
private string _lastViewType;
private string _zoomType = "Opsive.UltimateCharacterController.FirstPersonController.Camera.ViewTypes.Combat";
private bool _isFirstPerson;
private void Awake()
{
EventHandler.RegisterEvent<bool, bool>(gameObject, "OnAimAbilityStart", OnAimAbilityStart);
EventHandler.RegisterEvent<bool>(gameObject, "OnCameraChangePerspectives", OnCameraChangePerspectives);
}
void Start()
{
_cc = Camera.main.GetComponent<CameraController>();
_isFirstPerson = false;
}
private void OnCameraChangePerspectives(bool firstPerson)
{
//We only want to do this if allowed
if (!_cc.CanChangePerspectives) return;
//Track state of first person before zoom started.
_isFirstPerson = firstPerson;
}
private void OnAimAbilityStart(bool aiming, bool viaUser)
{
//Only want to use this if invoked by the player
if (!viaUser || _cc == null) return;
//Turn this feature off to avoid switching while in aim state.
_cc.CanChangePerspectives = false;
//If we are aiming...
if (aiming)
{
//Store the current view type so we can restore it later
_lastViewType = _cc.ActiveViewType.GetType().Name;
//Switch to first person combat
ChangeViewType(_zoomType);
}
else
{
//Don't do this if we haven't set a last view type
if (_lastViewType == null)
return;
//Allow perspective change
_cc.CanChangePerspectives = true;
//Restore the last perspective prior to zoom
ChangeViewType(_lastViewType);
//change perspective
_cc.SetPerspective(_isFirstPerson);
//Clear this now that we've used it
_lastViewType = null;
}
}
private void ChangeViewType(string viewType)
{
//Do this crazy thing
_cc.SetViewType(TypeUtility.GetType(viewType), true);
}
}