Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by glennuke · Nov 20, 2019 at 12:36 PM · buttonkeypresssimulate

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Marioooo · Mar 31, 2021 at 02:41 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to simulate a keypress/input in code? 5 Answers

Disable Sprite when keypress (Working Script, just need disble) 0 Answers

Can I Simulate the "Submit" Event with Gamepad Buttons? 0 Answers

Blocking one button's function when other pressed 1 Answer

My Button triggers when I press big Enter Key 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges