Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 WillModelForFood · Jul 14, 2011 at 01:41 AM · inputbuttonkeyboardemulator

How to emulate keyboard keys with a gui button?

Is it possible to emulate keyboard keys with a gui button? I want to move my character with a 4 gui buttons, but instead of re doing the input script, is there a way to map a gui button to simulate a keyboard key? Like if I were to press a button, then it would do what the "w" key does, and move forward.

It seems simple to do I just haven't been able to find a way to simulate a keyboard input.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by aldonaletto · Jul 14, 2011 at 03:01 AM

I've not found any way to fool Unity and make it "think" a key was pressed, but you can create a function to substitute Input.GetAxis, for instance. Add the code below to the script where you read Input.GetAxis("axis"), and just remove the dot to transform the original function calls in InputGetAxis("axis"):

 private var axisH: float = 0;
 private var axisV: float = 0;
 
 function InputGetAxis(axis: String): float {
 
     var v = Input.GetAxis(axis);
     if (Mathf.Abs(v) > 0.005) return v;
     if (axis=="Horizontal") return axisH;
     if (axis=="Vertical") return axisV;
 }
 
 function OnGUI(){
 
     axisV = axisH = 0;
     if (GUI.RepeatButton(Rect(50, 10, 40, 40), "W")) axisV = 1; 
     if (GUI.RepeatButton(Rect(50, 90, 40, 40), "S")) axisV = -1; 
     if (GUI.RepeatButton(Rect(10, 50, 40, 40), "A")) axisH = -1;
     if (GUI.RepeatButton(Rect(90, 50, 40, 40), "D")) axisH = 1;
 }

Use this InputGetAxis like the conventional Input.GetAxis, like below:

 dirH = InputGetAxis("Horizontal");
 dirV = InputGetAxis("Vertical");
 ...
 
Comment
Add comment · Show 11 · 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 WillModelForFood · Jul 14, 2011 at 03:32 AM 1
Share

Thank you very much, though I'm getting a parsing error hopefully I can get this working soon, appreciate it! Its just silly that theres not an easier way to emulate keys.

avatar image ermenda · Jul 18, 2011 at 11:59 AM 0
Share

thanks, nice code. But I aks emulate rotation of mouse in de axis x in two button

 var rotateSpeed = 20.0;

 function Update () {
 transform.Rotate(Vector3.down, Input.GetAxis("Horizontal") * rotateSpeed *   Time.deltaTime);
avatar image aldonaletto ermenda · Jul 18, 2011 at 04:48 PM 1
Share

It's the same thing: copy the code above at the beginning of your script and remove the dot from Input.GetAxis:

  transform.Rotate(Vector3.down, InputGetAxis("Horizontal") * rotateSpeed *   Time.deltaTime);
NOTE: there's a rule in Unity Answers: only use answers to answer to questions; in this case, you should have posted it as a comment in my answer (or as a new question). You may think: Why nobody told me that before?, and I would agree: there should exist some kind of omnipresent warning about this!
avatar image jesusluvsyooh · Apr 01, 2012 at 04:17 PM 0
Share

Aldo Naletto thank you so much <3

avatar image jesusluvsyooh · Apr 01, 2012 at 04:48 PM 0
Share

my god "Aldo Naletto" thank you so much, spent absolutely ages trying to get something on the lines of this working, <3

avatar image Enkendu · Oct 24, 2012 at 04:49 PM 0
Share

Ah, this was great! Thank you.

Show more comments
avatar image
0

Answer by Raatopojat · Dec 05, 2014 at 11:07 PM

Hello, so here's my script for geared car on phone. I wonder why i am not getting any reaction when i try to press the up button. Am i even close to understand this example?

It'd be more than hundred rabbits if you can find me the answer!! :)

 var enginePower=150.0;
 var up : GUITexture;
 var Magni : float;
 
 private var axisH: float = 0; 
 private var axisV: float = 0;
  
  function InputGetAxis(axis: String): float {
  
      var v = Input.GetAxis(axis);
      if (Mathf.Abs(v) > 0.005) return v;
      if (axis=="Horizontal") return axisH;
      if (axis=="Vertical") return axisV;
      
  }
         
 function Start(){
 axisV = axisH = 0;
 } 
 
 function Update () {
 
 for (var touch : Touch in Input.touches){ 
 
 if(touch.phase == TouchPhase.Stationary && up.HitTest (touch.position)){
     if (Magni >= 0){
         axisH = 1 * enginePower * Time.deltaTime * 600.0;
      }
     if (Magni > 50){
         axisH = 1 * enginePower * Time.deltaTime * 250.0;
      } 
         if (Magni > 100){
         axisH = 1 * enginePower * Time.deltaTime * 100.0;
      } 
             if (Magni > 110){
         axisH = 1 * enginePower * Time.deltaTime * 80.0;
      }
              if (Magni > 130){
         axisH= 1 * enginePower * Time.deltaTime * 30.0;
      } 
         }
 }
 
 }
 
             

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

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

12 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

Related Questions

Refactor code from getting input from keyboard to get input from UI Buttons 1 Answer

Text Prompt Problem 1 Answer

Mac OS Russian Keyboard - Input problem 0 Answers

How do you bring up the mobile keyboard without this area? 1 Answer

Problem changing the inputs 2 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