- Home /
How to disable character movement while editing InputField?
Sorry for the poor translation.
I want to prevent character movement while editing inputfield
At first, I blocked keyboard input by giving a bool value as IsActive Function , but another problem occurred, so this method cannot be used.
As shown in the picture, the character also moves during inputfiled input.
How to I solve it?
Thank you.
I focus on the input box when the user presses 'tab'. Now in the built in game player this works and I can type into the input box fine and it does not affect the player. However when I build the game despite being focused on the input box the first person controller moves around and jumps when I type into the input box. ePayitonline
Answer by asafsitner · Mar 16 at 01:07 PM
If you're using the UGUI system, you can use the EventSystem to find the currently selected UI object, like so: var selectedObject = FindObjectOfType<EventSystem>().currentSelectedGameObject;
Then if selectedObject
is not null, you can check that it has an InputField component with GetComponent<InputField>()
.
You could even write an extension method like this:
public static class EventSystemExtensions
{
public static bool IsTypeFocused<T>(this EventSystem self)
{
return self.currentSelectedGameObject.GetComponent<T>() != null;
}
}
and then do the check with if(FindObjectOfType<EventSystem>().IsTypeFocused<InputField>()) { /* code here */ }
I'm using UGUI System And, I've tried SetSelectedGameObject and ActivateInputField but both are isFocused Function gives false value
I checked that isFocused Function became true after a while because SetSelectedGameObject was not applied immediately, but the character can move even when isFocus is true.
I've setup a simple scene to test this out (and edited my answer's code to fix my type comparison mistake (currentSelectedGameObject always returns the gameObject):
----Canvas
--------InputField
--------Button
----EventSystem
----Camera
Then using a simple test script, I'm able to discern whether or not the input field is focused: using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;
public class UIFOcusTester : MonoBehaviour
{
public EventSystem events;
// Start is called before the first frame update
void Start()
{
events = FindObjectOfType<EventSystem>();
}
private void OnGUI()
{
if (events.currentSelectedGameObject)
{
InputField field = events.currentSelectedGameObject.GetComponent<InputField>();
if (field)
{
GUILayout.Box(field.name + " > " + field.isFocused);
}
}
}
}
Yeah, I checked that it is isfocused, but the character is still moving while editing InputField
Just to make sure - now that you can tell whether an InputField has focus or not, you're sure to use it to block character movement, right? Like
if(events.currentSelectedGameObject.GetComponent<InputField>().isFocused) { return; }
else { /* character movement etc. here */ }
right?
Regarding your isFocused
delay - if you never call ActivateInputField
, does isFocused
ever turn true
? I was assuming all this time that you're clicking the input field, but that was a baseless presumption on my end.
Can you confirm how you're using the input field?
I understand that when using an inputfield it tells the character to stop moving.
However, I have already tried before and found it difficult.
If I set the character's speed to 0 immediately, the animation will be unnatural.
If I block the Move function, the animation of walking in place is executed without changing the speed.
Also I'm using GetAxis for character movement, I couldn't find a way to just block keyboard input.
So I was trying to figure out how to apply keyboard input only to UI when Inputfield is in use.
Also, I'm trying to use inputfield as a chatiing during the game, so inputfield is activated when I press enter instead of clicking to use the inputfield.
I'm sorry I couldn't explain in advance, and thank you for trying to help.
For reference, my character's behavior is like this:
Unfortunately the image is broken, but I can try to imagine what you're trying to do.
In that case, you'll want to have a method on your character script that gracefully stops the animation, and call that method when the user presses 'Enter' to start the chat. Exactly how to do that is entirely up to your specifications, but for example you can use Mathf.Lerp to gradually bring the character's speed to 0.
You can then subscribe to the chat input field's onEndEdit
event, and resume control of the character. Always remember to unsubscribe from events before disabling/destroying the object, or you'll get null reference error. Something like:
private void OnEnable()
{
/*
* other code here
*/
// subscribe to input field event, assuming the reference to it is already set earlier or in the inspector
chatInputField.onEndEdit.AddListener(ResumeCharacterControl);
}
private void OnDisable()
{
/*
* other code here
*/
// it's important to unsubscribe from events or we'll get errors
chatInputField.onEndEdit.RemoveListener(ResumeCharacterControl);
}
private void ResumeCharacterControl(string inputValue)
{
/*
* code here
*/
}