Forum rules - please read before posting.

Translating Draggable Track position to Game Camera FoV value through using Float Global Var

Hi
I have a Monocular Camera at some point the player can use it to see the downtown and zoomin or zoom out while dragging a handle on a 0-1 straightTrack.

I tried to make it but i get errors shoiwng

using UnityEngine;
using AC;

public class ZoomControl : MonoBehaviour
{
    // The name of the global variable in Adventure Creator
    public string zoomVariableName = "Zoom";

    // The minimum and maximum values for the draggable object's position
    public float minPosition = 0f;
    public float maxPosition = 1f;

    // The camera whose FoV will be affected
    public Camera gameCamera;

    // The minimum and maximum field of view values for the camera
    public float minFOV = 25f;
    public float maxFOV = 55f;

    // Update is called once per frame
    private void Update()
    {
        // Get the current position of the draggable object
        float currentPosition = Mathf.Clamp01(GetObjectPosition());

        // Update the global variable in Adventure Creator
        SetGlobalVariable(zoomVariableName, currentPosition);

        // Update the camera's FoV based on the global variable
        float newFOV = Mathf.Lerp(minFOV, maxFOV, currentPosition);
        gameCamera.fieldOfView = newFOV;
    }

    // Get  normalized position of the draggable object between minPosition and maxPosition
    private float GetObjectPosition()
    {
        float objectPosition = Mathf.InverseLerp(minPosition, maxPosition, transform.localPosition.x);
        return Mathf.Clamp01(objectPosition);
    }

    // Helper  to set the value of a global variable in AC
    private void SetGlobalVariable(string variableName, float value)
    {
        GVar globalVariable = GlobalVariables.GetVariable(variableName);

        // value based on the type of the global variable
        if (globalVariable.type == VariableType.Float)
        {
            globalVariable.floatVal = value;
        }
        else if (globalVariable.type == VariableType.String)
        {
            globalVariable.textVal = value.ToString();
        }

    }
}

I get errors Like :
error CS0122: 'GVar.floatVal' is inaccessible due to its protection level
also about GVar.TextVal the same

Comments

  • I forgot to mention:
    Unity 2020.3.30f1 with ACv1.79.1 in a 3D direct game.

  • I was Also thinking maybe i must Make an Animation for my Camera with FoV change and try using draggable position of Track to control which frame it must show ?!

  • Alternatively I just made 4 animations for MonocularCamera to have FoV changes, then used OnRelease action list of the Draggable.
    apparently it is not as juicy as the other way which was thinking about. Lack of programming knowledge :(

  • error CS0122: 'GVar.floatVal' is inaccessible due to its protection level

    Use FloatValue and TextValue instead of floatVal and textVal.

    I was Also thinking maybe i must Make an Animation for my Camera with FoV change and try using draggable position of Track to control which frame it must show ?!

    Yes, you could create a Blend Tree and use a parameter that goes from 0 -> 1 to control it. You can get how far along a Draggable is along its track with the GetPositionAlong function.

  • edited December 2023

    Thanks Chris! It solved my main problem and helps me learn alot.
    Here is the code which is working fine.(In case somebody needs such behavior)

    using UnityEngine;
    using AC;
    
    public class ZoomControl : MonoBehaviour
    {
        // The name of the global variable in Adventure Creator
        public string zoomVariableName = "Zoom";
    
        // Reference to the draggable object it is optional
        public AC.DragBase draggableObject;
    
        // Reference to the track along which the draggable object moves
        public Transform track;
    
        // Reference to the camera
        public Camera gameCamera;
    
        // The minimum and maximum Fov values for the camera
        public float minFOV = 20f;
        public float maxFOV = 60f;
    
        // Update is called once per frame
        private void Update()
        {
            if (draggableObject != null && track != null && gameCamera != null)
            {
                // Calculate the position of the draggable object along the track
                float positionOnTrack = Mathf.InverseLerp(track.position.z - track.localScale.z / 1f, track.position.z + track.localScale.z / 1f, draggableObject.transform.position.z);
    
                // Update the global variable in Adventure Creator
                SetGlobalVariable(zoomVariableName, positionOnTrack);
    
                // Update the camera's FoV based on the global variable
                float newFOV = Mathf.Lerp(minFOV, maxFOV, positionOnTrack);
                gameCamera.fieldOfView = newFOV;
            }
            else
            {
                Debug.LogError("Draggable Object, Track, or Camera not assigned to ZoomControl.");
            }
        }
    
        private void SetGlobalVariable(string variableName, float value)
        {
            GVar globalVariable = GlobalVariables.GetVariable(variableName);
    
            // Set the value based on the type of the global variable
            if (globalVariable != null)
            {
                if (globalVariable.type == VariableType.Float)
                {
                    globalVariable.FloatValue = value;
                }
                else if (globalVariable.type == VariableType.String)
                {
                    globalVariable.TextValue = value.ToString();
                }
                // Add additional checks for other variable types as needed
            }
            else
            {
                Debug.LogError("Global Variable " + variableName + " not found.");
            }
        }
    }
    

    Ah! Blend tree which i forgot due to my lack of experience was even a more neat way to do it! thanks for insights.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.