• 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
1
Question by Dainiusss · May 30, 2015 at 06:44 PM · uibuttoninputtouchaxis

How to replace input axis keyboard button with touch button?

Hello everyone! I am trying to make my game work on android phone. I need to switch from my keyboard input to screen touch input. I am using Input.GetAxis() usually, when the input is my keyboard arrows. I want to replace that keyboard button(s) of axis with particular button(s) of my ui elements, so that when I touch my screen, it will be the same as I would push the keyboard button. I want to do that, because axis settings has lots of settings like 'sensitivity' or 'gravity' and etc. and so I would not need to make that input system by myself, only replace the input button. So how can I do that? Thanks.

Comment
Add comment · Show 1
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 torombolo89 · Dec 06, 2015 at 03:19 PM 0
Share

i know this is old... but Holy Baby Jesus with a Golden Diaper... Thank you!!!

2 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by Saad_Khan · May 31, 2015 at 12:14 PM

Simple solution:

You can try using the following code , for replicating the push of a keyboard button or mouse button..

 float xPos = Input.GetAxis("Mouse X");
 
 float yPos = Input.GetAxis("Mouse Y");
  
      if (Input.touchCount > 0)
      {
          xPos = Input.touches[0].deltaPosition.x;
          yPos = Input.touches[0].deltaPosition.y;
      }

Heres info on how mobile input works:

http://docs.unity3d.com/ScriptReference/Input.GetTouch.html

Recommend solution

Although, I would recommend that if you are using the latest Unity UI .

1) Create a button , and add an event trigger component

2) Add two new events ( on the event trigger component) from the options select "Pointerdown" and "PointerUp". These events will be triggered when you push this button on the UI and release it , respectively.

3) Hook corresponding functions like "OnPointerDown" etc that you'd like to call, into these triggers

4) Use something like the following script to make it work

 void Update () {
 
         if (_isMove == true && (transform.position.x) <= 4.5f) {
                     transform.Translate (Vector3.left * 0.01f);
                             transform.Translate (Vector3.left * 0.01f);
                 }
     }
 
      public void OnPointerDown()
     {
         _isMove = true;
 
                 
     }
     public void OnPointerUp()
     {
         _isMove = false;
     }



screen-shot-2015-05-31-at-21513-am.png (47.8 kB)
Comment
Add comment · Show 4 · 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 Dainiusss · May 31, 2015 at 05:38 PM 1
Share

Thank you! Your answer gave me a good clue of understanding touch input principles.

avatar image surajsirohi1008 · Jul 20, 2016 at 08:30 AM 0
Share

Its just make the game object move in anti-clockwise way,....

avatar image ScriptLab · Mar 11, 2017 at 10:03 AM 1
Share

Thank you so much! The clearest explanation i've found!

avatar image EVART · Dec 12, 2018 at 03:58 PM 0
Share

Please, you deserve a Grammy. You have given me life. Thank you so much!

avatar image
0

Answer by unity_9d5-VgjizaCUeQ · Dec 28, 2017 at 10:15 AM

How do I fix the error on _isMove? @Saad_Khan

 using System;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 
     public class ButtonHandler : MonoBehaviour
     {
 
 
 
         void Update()
         {
 
             if (_isMove == true && (transform.position.x) <= 4.5f)
             {
                 transform.Translate(Vector3.left * 0.01f);
                 transform.Translate(Vector3.left * 0.01f);
             }
         }
 
         public void OnPointerDown()
         {
             _isMove = true;
 
 
         }
         public void OnPointerUp()
         {
             _isMove = false;
         }
     }
 

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 bulleergottsominihelvete · Dec 19, 2018 at 05:29 PM 0
Share

using System; using UnityEngine; using UnityEngine.UI; using UnityEngine.Scene$$anonymous$$anagement;

  public class ButtonHandler : $$anonymous$$onoBehaviour
  {
         bool _is$$anonymous$$ove; //You forgot to add a bool:)
 
 
      void Update()
      {
 
          if (_is$$anonymous$$ove == true && (transform.position.x) <= 4.5f)
          {
              transform.Translate(Vector3.left * 0.01f);
              transform.Translate(Vector3.left * 0.01f);
          }
      }
 
      public void OnPointerDown()
      {
          _is$$anonymous$$ove = true;
 
 
      }
      public void OnPointerUp()
      {
          _is$$anonymous$$ove = false;
      }
  }
 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

29 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

Related Questions

Use Canvas Buttons as Input Axis 1 Answer

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

Problems with Inputs 1 Answer

Detect Input type by checking if ANY key on keyboardd/joypad is pressed 1 Answer

What order do UI elements capture input? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges