Forum rules - please read before posting.

Auto Switch Camera + Marker + Navigation Mesh into single action

Very often in my game I switch Camera and Navigation Mesh and Marker position for my player, i.e. 3 actions.
So I created a script that would merge all these into one.

#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections.Generic;
using UnityEngine;

namespace AC
{
    [System.Serializable]
    public class ActionSceneEntrySetup : Action
    {
        // --- Camera ---
        public _Camera newCamera;
        public int newCameraConstantID = 0;
        public int newCameraParameterID = -1;
        public bool instantCameraSwitch = true;
        public float cameraTransitionTime = 0f;

        // --- Marker / teleport ---
        public Marker teleportMarker;
        public int markerConstantID = 0;
        public int markerParameterID = -1;
        public bool copyRotation = true;

        // --- NavMesh ---
        public NavigationMesh newNavMesh;
        public int navMeshConstantID = 0;
        public int navMeshParameterID = -1;

        public override ActionCategory Category { get { return ActionCategory.Custom; } }
        public override string Title { get { return "Scene entry setup (Camera + Teleport + NavMesh)"; } }
        public override string Description
        {
            get
            {
                return "Switches the active GameCamera, teleports the Player to a Marker (optionally copying " +
                       "its rotation), and sets the scene's active NavigationMesh in one step. Intended to run " +
                       "inside the destination scene's OnStart cutscene, right after a Scene: Change action.";
            }
        }

        public override void AssignValues(List<ActionParameter> parameters)
        {
            newCamera = AssignFile<_Camera>(parameters, newCameraParameterID, newCameraConstantID, newCamera);
            teleportMarker = AssignFile<Marker>(parameters, markerParameterID, markerConstantID, teleportMarker);
            newNavMesh = AssignFile<NavigationMesh>(parameters, navMeshParameterID, navMeshConstantID, newNavMesh);
        }

        public override float Run()
        {
            // 1) Switch camera
            if (newCamera != null && KickStarter.mainCamera != null)
            {
                KickStarter.mainCamera.SetGameCamera(newCamera);
                if (!instantCameraSwitch && cameraTransitionTime > 0f)
                {
//                    KickStarter.mainCamera.Crossfade(cameraTransitionTime);
                }
            }

            // 2) Teleport player to marker (+ optional rotation copy)
            if (teleportMarker != null && KickStarter.player != null)
            {
                Player player = KickStarter.player;

                // Stop any pathfinding / movement before warping
                player.Halt();
                player.Teleport(teleportMarker.transform.position);

                if (copyRotation)
                {
                    player.SetRotation(teleportMarker.transform.rotation);
                }
            }

            // 3) Set active NavMesh for the scene
            if (newNavMesh != null && KickStarter.sceneSettings != null)
            {
                if (KickStarter.sceneSettings.navMesh != null && KickStarter.sceneSettings.navMesh != newNavMesh)
                {
                    KickStarter.sceneSettings.navMesh.TurnOff();
                }
                KickStarter.sceneSettings.navMesh = newNavMesh;
                newNavMesh.TurnOn();
            }

            return 0f;
        }

#if UNITY_EDITOR

        public override void ShowGUI(List<ActionParameter> parameters)
        {
            EditorGUILayout.LabelField("Camera", EditorStyles.boldLabel);
            newCameraParameterID = Action.ChooseParameterGUI("New camera:", parameters, newCameraParameterID, ParameterType.GameObject);
            if (newCameraParameterID < 0)
            {
                newCamera = (_Camera)EditorGUILayout.ObjectField("New camera:", newCamera, typeof(_Camera), true);
                newCameraConstantID = FieldToID<_Camera>(newCamera, newCameraConstantID);
                newCamera = IDToField<_Camera>(newCamera, newCameraConstantID, false);
            }
            instantCameraSwitch = EditorGUILayout.Toggle("Instant switch?", instantCameraSwitch);
            if (!instantCameraSwitch)
            {
                cameraTransitionTime = EditorGUILayout.FloatField("Transition time (s):", cameraTransitionTime);
            }

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Teleport", EditorStyles.boldLabel);
            markerParameterID = Action.ChooseParameterGUI("Marker to teleport to:", parameters, markerParameterID, ParameterType.GameObject);
            if (markerParameterID < 0)
            {
                teleportMarker = (Marker)EditorGUILayout.ObjectField("Marker to teleport to:", teleportMarker, typeof(Marker), true);
                markerConstantID = FieldToID<Marker>(teleportMarker, markerConstantID);
                teleportMarker = IDToField<Marker>(teleportMarker, markerConstantID, false);
            }
            copyRotation = EditorGUILayout.Toggle("Copy marker rotation?", copyRotation);

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("NavMesh", EditorStyles.boldLabel);
            navMeshParameterID = Action.ChooseParameterGUI("New NavMesh:", parameters, navMeshParameterID, ParameterType.GameObject);
            if (navMeshParameterID < 0)
            {
                newNavMesh = (NavigationMesh)EditorGUILayout.ObjectField("New NavMesh:", newNavMesh, typeof(NavigationMesh), true);
                navMeshConstantID = FieldToID<NavigationMesh>(newNavMesh, navMeshConstantID);
                newNavMesh = IDToField<NavigationMesh>(newNavMesh, navMeshConstantID, false);
            }
        }

        public override string SetLabel()
        {
            if (newCamera != null && teleportMarker != null)
            {
                return newCamera.name + " / " + teleportMarker.name;
            }
            if (teleportMarker != null) return teleportMarker.name;
            if (newCamera != null) return newCamera.name;
            return string.Empty;
        }

        public override void AssignConstantIDs(bool saveScriptsToo, bool fromAssetFile)
        {
            AssignConstantID<_Camera>(newCamera, newCameraConstantID, newCameraParameterID);
            AssignConstantID<Marker>(teleportMarker, markerConstantID, markerParameterID);
            AssignConstantID<NavigationMesh>(newNavMesh, navMeshConstantID, navMeshParameterID);
        }

#endif

#if UNITY_EDITOR
        [UnityEditor.MenuItem("Adventure Creator/Actions/New: Scene entry setup")]
        public static void CreateNew_MenuHint() { }
#endif
    }
}
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.