From f9c66a68443c4f26e4047e91752bf496bd044b15 Mon Sep 17 00:00:00 2001 From: mhby1g21 <mhby1g21@soton.ac.uk> Date: Sun, 29 Dec 2024 22:01:51 +0000 Subject: [PATCH] added very scuffed and unoptimised checks to disable noVR player movement in menu and vice versa --- AVVR/Assets/_Scripts/PlayerController.cs | 33 +++++++++++-- AVVR/Assets/_Scripts/noVRMenu.cs | 61 ++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/AVVR/Assets/_Scripts/PlayerController.cs b/AVVR/Assets/_Scripts/PlayerController.cs index bd619bf..68343fd 100644 --- a/AVVR/Assets/_Scripts/PlayerController.cs +++ b/AVVR/Assets/_Scripts/PlayerController.cs @@ -14,19 +14,24 @@ public class PlayerController : MonoBehaviour private Camera playerCamera; private float verticalRotation = 0f; private Vector3 velocity; + private bool isEnabled = true; void Start() { controller = GetComponent<CharacterController>(); playerCamera = GetComponentInChildren<Camera>(); - // Lock cursor - Cursor.lockState = CursorLockMode.Locked; - Cursor.visible = false; + // Initial cursor state + UpdateCursorState(); } void Update() { + if (!isEnabled) + { + return; + } + // WASD Movement float moveX = Input.GetAxis("Horizontal"); float moveZ = Input.GetAxis("Vertical"); @@ -58,4 +63,24 @@ public class PlayerController : MonoBehaviour velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); } -} \ No newline at end of file + private void UpdateCursorState() + { + if (enabled) + { + Cursor.lockState = CursorLockMode.Locked; + Cursor.visible = false; + } + } + + private void OnEnable() + { + isEnabled = true; + UpdateCursorState(); + } + + private void OnDisable() + { + isEnabled = false; + } +} + diff --git a/AVVR/Assets/_Scripts/noVRMenu.cs b/AVVR/Assets/_Scripts/noVRMenu.cs index 16a1914..d3202c4 100644 --- a/AVVR/Assets/_Scripts/noVRMenu.cs +++ b/AVVR/Assets/_Scripts/noVRMenu.cs @@ -8,6 +8,7 @@ public class NonVRMenu : MonoBehaviour { private bool isMenuVisible = true; private Canvas menuCanvas; + private PlayerController playerController; private AudioClip musicClip; private AudioClip singingClip; @@ -45,6 +46,15 @@ public class NonVRMenu : MonoBehaviour menuCanvas = GetComponentInChildren<Canvas>(); } + // Find the PlayerController in the scene + playerController = FindObjectOfType<PlayerController>(); + if (playerController == null) + { + Debug.LogWarning("NonVRMenu: PlayerController not found in scene"); + } + // Set initial state + SetMenuAndPlayerState(isMenuVisible); + // Ensure EventSystem exists if (FindObjectOfType<UnityEngine.EventSystems.EventSystem>() == null) { @@ -74,16 +84,28 @@ public class NonVRMenu : MonoBehaviour private void Update() { + if (!isMenuVisible) + { + // Toggle menu visibility with Tab key + if (Input.GetKeyDown(toggleMenuKey)) + { + ToggleMenuVisibility(); + } + return; + } // Toggle menu visibility with Tab key if (Input.GetKeyDown(toggleMenuKey)) { ToggleMenuVisibility(); } - // Toggle audio playback with Space key - if (Input.GetKeyDown(toggleAudioKey)) + // Only handle other inputs if menu is visible + if (isMenuVisible) { - ToggleAudioPlayback(); + if (Input.GetKeyDown(toggleAudioKey)) + { + ToggleAudioPlayback(); + } } } @@ -110,7 +132,8 @@ public class NonVRMenu : MonoBehaviour private void ToggleMenuVisibility() { isMenuVisible = !isMenuVisible; - + SetMenuAndPlayerState(isMenuVisible); + if (menuCanvas != null) { menuCanvas.enabled = isMenuVisible; @@ -133,6 +156,36 @@ public class NonVRMenu : MonoBehaviour Cursor.lockState = isMenuVisible ? CursorLockMode.None : CursorLockMode.Locked; } + private void SetMenuAndPlayerState(bool menuVisible) + { + // Handle menu visibility + if (menuCanvas != null) + { + menuCanvas.enabled = menuVisible; + } + else + { + foreach (var canvas in GetComponentsInChildren<Canvas>(true)) + { + canvas.enabled = menuVisible; + } + foreach (var renderer in GetComponentsInChildren<Renderer>(true)) + { + renderer.enabled = menuVisible; + } + } + + // Handle cursor state + Cursor.visible = menuVisible; + Cursor.lockState = menuVisible ? CursorLockMode.None : CursorLockMode.Locked; + + // Handle player controller state + if (playerController != null) + { + playerController.enabled = !menuVisible; + } + } + private void ValidateComponents() { if (volumeSlider == null) Debug.LogError("NonVRMenu: Volume Slider not assigned"); -- GitLab