public void Awake() { input_manager = new InputManager((short)player_index); selections = FindObjectsOfType <GUIButtonSelectShipBehaviour>(); Selection = selections[0]; PlayerJoined = (player_index == 0); }
public void Update() { // Toggle join status. if (input_manager.Join) { PlayerJoined = !PlayerJoined; } // Confirm the selection. if (input_manager.Confirm) { Confirmed = !Confirmed; } // Move the selection around. Vector2 movement = new Vector2(input_manager.Horizontal, input_manager.Vertical); // Direction of the input. if (movement.sqrMagnitude <= 0.2f) { enable_movement = true; } else if (enable_movement) { enable_movement = false; movement.Normalize(); var target_selections = selections // Sort the other GUI elements the cursor can jump to by distance. .Where(s => s != this.selection) .OrderBy(s => (s.transform.position - this.selection.transform.position).sqrMagnitude) .ToArray(); foreach (var target_selection in target_selections) // Find the best GUI element to jump to by direction (near elements are favored). { Vector2 direction = target_selection.transform.position - this.selection.transform.position; if (Vector2.Dot(direction.normalized, movement) >= 0.8f) { Selection = target_selection; break; } } } }