using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.UI; public class Menu : MonoBehaviour { public GameObject MenuRotator; public GameObject Floor1Panel; public GameObject Floor2Panel; public Button Floor1Button; public Button Floor2Button; public Camera Camera; public Image Playericon; public InputActionReference openMenuAction; private Canvas canvas; public Vector2 rotatedPlayerPos = new Vector2(); public float worldToMapAngle = 130.0f; public Vector2 VR_P1 = new Vector2(-77.4f, 10.1f); // - 71.2f (alumine punkt) public Vector2 VR_P2 = new Vector2(95.7f, -82.8f); public Vector2 IMG_P1 = new Vector2(-387.2f, 193.1f); // public Vector2 IMG_P2 = new Vector2(389.7f, -221.4f); // private void Awake() { openMenuAction.action.Enable(); openMenuAction.action.performed += ToggleMenu; InputSystem.onDeviceChange += OnDeviceChange; canvas = GetComponent(); canvas.enabled = false; } private void ToggleMenu(InputAction.CallbackContext context) { // Match the Y rotation of the MenuRotator to the Camera Vector3 cameraRotation = Camera.transform.eulerAngles; Vector3 menuRotatorRotation = MenuRotator.transform.eulerAngles; // Update only the Y rotation, keep X and Z unchanged menuRotatorRotation.y = cameraRotation.y; MenuRotator.transform.eulerAngles = menuRotatorRotation; // Set the menu rotator position to that of the camera MenuRotator.transform.position = Camera.transform.position; canvas.enabled = !canvas.enabled; } // Start is called before the first frame update void Start() { Floor1Button.onClick.AddListener(DisplayFloor1); Floor2Button.onClick.AddListener(DisplayFloor2); DisplayFloor2(); } private void DisplayFloor1() { //Debug.Log("Dispaling floor 1"); Floor1Panel.gameObject.SetActive(true); Floor2Panel.gameObject.SetActive(false); } private void DisplayFloor2() { //Debug.Log("Dispaling floor 2"); Floor1Panel.gameObject.SetActive(false); Floor2Panel.gameObject.SetActive(true); } private void OnDeviceChange(InputDevice device, InputDeviceChange change) // To avoid bugs with controllers disconnecting. { switch (change) { case InputDeviceChange.Disconnected: openMenuAction.action.Disable(); openMenuAction.action.performed -= ToggleMenu; break; case InputDeviceChange.Reconnected: openMenuAction.action.Enable(); openMenuAction.action.performed += ToggleMenu; break; } } // Update is called once per frame void Update() { if (transform.position.y < -50) // If the player has fallen off the platform. { if (!canvas.enabled) // If has menu not activated. { ToggleMenu(new InputAction.CallbackContext()); // Activate menu. } } float worldToMapAngleRad = worldToMapAngle * Mathf.Deg2Rad; //Vector2 VR_Player = new Vector2(transform.position.x, transform.position.z); Vector2 VR_Player = new Vector2( Mathf.Cos(worldToMapAngleRad) * transform.position.x - Mathf.Sin(worldToMapAngleRad) * transform.position.z, Mathf.Sin(worldToMapAngleRad) * transform.position.x + Mathf.Cos(worldToMapAngleRad) * transform.position.z ); VR_Player = VR_Player * new Vector2(1.0f, 1.0f); rotatedPlayerPos = VR_Player; //Debug.Log(VR_Player); //Vector2 VR_P1 = new Vector2(-10, 102); //Vector2 VR_P2 = new Vector2(-123, -19); /*Vector2 VR_P1 = new Vector2(-102.0f, 73.0f); Vector2 VR_P2 = new Vector2(71.1f, 76.6f); Vector2 IMG_P1 = new Vector2(-364.0f, -180.0f); Vector2 IMG_P2 = new Vector2(394.0f, -225.0f);*/ Vector2 VR_L = VR_P2 - VR_P1; Vector2 IMG_L = IMG_P2 - IMG_P1; Vector2 IMG_Player = (VR_Player - VR_P1) / VR_L * IMG_L + IMG_P1; Playericon.GetComponent().anchoredPosition = IMG_Player; /* float VR_X = transform.position.x; float VR_Y = transform.position.y; float VR_X1 = -10; float VR_X2 = -123; float IMG_X1 = -364; float IMG_X2 = 394; float Lvr = VR_X2 - VR_X1;//(-138 - -10) float Limg = IMG_X2 - IMG_X1; IMG_X = (VR_X - VR_X1) / Lvr * Limg + IMG_X1; */ //IMG_Y = (VR_Y - VR_Y1) / Lvr * Limg + IMG_Y1; //Playericon.transform.position.Set(IMG_X,0,0); //Playericon.GetComponent().anchoredPosition = new Vector2(IMG_X, IMG_Y); //Debug.Log(IMG_X + " vs " + Playericon.GetComponent().anchoredPosition.x); } private void OnDestroy() { openMenuAction.action.Disable(); openMenuAction.action.performed -= ToggleMenu; InputSystem.onDeviceChange -= OnDeviceChange; Floor1Button.onClick.RemoveListener(DisplayFloor1); Floor2Button.onClick.RemoveListener(DisplayFloor2); } }