- Home /
Why does the keyboard control what button is selected?
My problem is detailed in the picture attached.
Note: I do have the horizontal and vertical inputs already manually assigned to camera movement and nothing else via script.
Answer by thelonelypanda · Jul 20, 2020 at 03:23 AM
--FINAL EDIT-- Again IDK why I cant make a new answer so I'm editing this one. Ignore everything else in this answer, except what is in this section... All I needed to do was import UnityEngine.EventSystems and insert the line EventSystem.current.SetSelectedGameObject(null);
(where it makes sense to place it). That's it, seriously.
So I thought of a solution...when none of the panels attached to those buttons are displayed, I have to manually deselect the toolbar. I don't know how to do that yet or if it's even possible, but when I get around to fixing that I will come back here and post the answer and mark it correct. I will fix this issue one way or another, mark my words and date it July 19th, 2020.
--Edit-- IDK why I cant make a new answer so I'm editing this one.
I edited StandaloneInputModule.cs by copying that script to a new script since that one is uneditable. Anyways, I just commented out shouldactivate |= input.getbuttondown(m_submitbutton); shouldactivate |= input.getbuttondown(m_cancelbutton); shouldactivate |= !mathf.approximately(input.getaxisraw(m_horizontalaxis), 0.0f); shouldactivate |= !mathf.approximately(input.getaxisraw(m_verticalaxis), 0.0f);
inside of public override bool ShouldActivateModule()
then I commented out if (eventSystem.sendNavigationEvents) { if (!usedEvent) usedEvent |= SendMoveEventToSelectedObject(); if (!usedEvent) SendSubmitEventToSelectedObject(); }
inside of public override void Process()
THEN I commented out private Vector2 GetRawMoveVector() { Vector2 move = Vector2.zero; move.x = input.GetAxisRaw(m_HorizontalAxis); move.y = input.GetAxisRaw(m_VerticalAxis); if (input.GetButtonDown(m_HorizontalAxis)) { if (move.x < 0) move.x = -1f; if (move.x > 0) move.x = 1f; } if (input.GetButtonDown(m_VerticalAxis)) { if (move.y < 0) move.y = -1f; if (move.y > 0) move.y = 1f; } return move; }
and LASTLY I commented out protected bool SendMoveEventToSelectedObject() { float time = Time.unscaledTime; Vector2 movement = GetRawMoveVector(); if (Mathf.Approximately(movement.x, 0f) && Mathf.Approximately(movement.y, 0f)) { m_ConsecutiveMoveCount = 0; return false; } bool similarDir = (Vector2.Dot(movement, m_LastMoveVector) > 0); // If direction didn't change at least 90 degrees, wait for delay before allowing consequtive event. if (similarDir && m_ConsecutiveMoveCount == 1) { if (time <= m_PrevActionTime + m_RepeatDelay) return false; } // If direction changed at least 90 degree, or we already had the delay, repeat at repeat rate. else { if (time <= m_PrevActionTime + 1f / m_InputActionsPerSecond) return false; } var axisEventData = GetAxisEventData(movement.x, movement.y, 0.6f); if (axisEventData.moveDir != MoveDirection.None) { ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, axisEventData, ExecuteEvents.moveHandler); if (!similarDir) m_ConsecutiveMoveCount = 0; m_ConsecutiveMoveCount++; m_PrevActionTime = time; m_LastMoveVector = movement; } else { m_ConsecutiveMoveCount = 0; } return axisEventData.used; }
I put that script on the "Event System" gameobject and disable the original version of the script that was uneditable.
All after all that I got no error messages and my problem was gone :)
Answer by tuinal · Jul 20, 2020 at 04:31 AM
When you add a Canvas, unity by default adds an EventSystem, with a StandAloneEventSystem component that controls the default UI input bindings.
This by default has the horizontal axis and vertical axis for UI navigation mapped to the InputManager's horizontal and vertical.
Quick fix, just select the Event System child of the Canvas, and disable the module.
That didn't work, it just disabled all functionality for the whole canvas.
even though it wasn't fully what i needed to do, i want to give you credit for leading me in the right direction