- Home /
Press Ui Button To Simulate Keypress
Im working on a game i have it on pc and android but on android i need to press a ui button to simulate key A and D Im new to unity so can someone tell me how i can do it Thanks
Answer by Casiell · Nov 20, 2019 at 12:46 PM
You should almost never do it this way. It would be easier to help if you could share your code, so I could use it as an example, but let's try anyway.
Move everything you do when detecting that keypress to a single method. Then call that method from a button. And voi'la, everything should work
Answer by Marioooo · Nov 20, 2019 at 08:48 PM
you could simply use a button in UI and a DoSomething() function that executes everything that key does... call the function with the button and that's it!
Answer by DanuVIP · Mar 31, 2021 at 11:38 AM
hey i have the same problems i wished to find an answer in the web but probably i couldnt find anything until now my problem is as i said the same my script is scripted to use horizontal input now i want the application work fine on the mobile and i need to find a way to simulate a keypress by clicking a ui button
here is my code:
public GameObject GameOverScreen;
public float moveSpeed = 600f;
public Text highscore;
public Text score;
public float highscoreVal;
public float currentscore;
public float counter;
float movement = 0f;
public bool isDead;
void Start()
{
Time.timeScale = 1f;
highscoreVal = PlayerPrefs.GetFloat("highscore");
isDead = false;
}
void Update()
{
movement = Input.GetAxisRaw("Horizontal");
currentscore = counter;
counter += Time.deltaTime;
score.text = counter.ToString();
highscore.text = highscoreVal.ToString();
if(isDead == true)
{
GameOverScreen.SetActive(true);
}else if(isDead == false)
{
GameOverScreen.SetActive(false);
}
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
}
private void OnTriggerEnter2D(Collider2D collision)
{
isDead = true;
if(currentscore > highscoreVal)
{
highscoreVal = currentscore;
PlayerPrefs.SetFloat("highscore", highscoreVal);
}
Time.timeScale = 0f;
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
i am not fresh new to unity, i am working with Unity round about 2 years but not professional. hopefull someone have a nice solution to me (finding a path to simulate keypress or finding a path to dispense on the Horizontal axis) have a nice day! and thanks in advance
you are listening to a input and converting it into a variable... so do this: create a left button, add an event and attach a script to control it... if theres only one player, then just via find gameobject, create a reference to the player script inside the button controller script... now add two events inside the button, one for press and one for release... on press create a function that Subtracts 1 to the movement and on release Add 1... that's it... repeat for right button and switch values... don't forget to make the movement variable public