#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.CameraTasks { using Opsive.GraphDesigner.Runtime.Variables; using Opsive.BehaviorDesigner.Runtime.Tasks.Actions.TransformTasks; using UnityEngine; [Opsive.Shared.Utility.Category("Camera")] [Opsive.Shared.Utility.Description("Sets camera FOV with smooth transition and optional curve.")] public class SetFieldOfView : TargetGameObjectAction { [Tooltip("The target field of view.")] [SerializeField] protected SharedVariable m_TargetFOV = 60.0f; [Tooltip("The transition duration (0 = instant).")] [SerializeField] protected SharedVariable m_TransitionDuration = 0.0f; [Tooltip("The easing curve type.")] [SerializeField] protected SmoothMoveTo.EasingType m_EasingType = SmoothMoveTo.EasingType.Linear; private Camera m_ResolvedCamera; private float m_StartFOV; private float m_ElapsedTime; /// /// Initializes the target GameObject. /// protected override void InitializeTarget() { base.InitializeTarget(); m_ResolvedCamera = m_ResolvedGameObject.GetComponent(); } /// /// Called when the action starts. /// public override void OnStart() { base.OnStart(); m_ElapsedTime = 0.0f; if (m_ResolvedCamera != null) { m_StartFOV = m_ResolvedCamera.fieldOfView; } } /// /// Updates the camera FOV. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedCamera == null) { return TaskStatus.Failure; } if (m_TransitionDuration.Value > 0.0f) { m_ElapsedTime += Time.deltaTime; var progress = Mathf.Clamp01(m_ElapsedTime / m_TransitionDuration.Value); var easedProgress = ApplyEasing(progress); m_ResolvedCamera.fieldOfView = Mathf.Lerp(m_StartFOV, m_TargetFOV.Value, easedProgress); return progress >= 1.0f ? TaskStatus.Success : TaskStatus.Running; } else { m_ResolvedCamera.fieldOfView = m_TargetFOV.Value; } return TaskStatus.Success; } /// /// Applies easing to the value. /// private float ApplyEasing(float t) { switch (m_EasingType) { case SmoothMoveTo.EasingType.EaseIn: return t * t; case SmoothMoveTo.EasingType.EaseOut: return 1.0f - (1.0f - t) * (1.0f - t); case SmoothMoveTo.EasingType.EaseInOut: return t < 0.5f ? 2.0f * t * t : 1.0f - Mathf.Pow(-2.0f * t + 2.0f, 2.0f) / 2.0f; default: return t; } } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_TargetFOV = 60.0f; m_TransitionDuration = 0.0f; m_EasingType = SmoothMoveTo.EasingType.Linear; } } } #endif