Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Dainiusss · May 30, 2015 at 06:44 PM · uiinputbuttontouchaxis

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.

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

EventSystem not detecting all UI elements 1 Answer

Rotating 2D Sprite on 1 Axis Using 2 Input Axes 0 Answers

OVRInput keys are not working 0 Answers

GetKey/GetButton not working if 2 Adjacent keys are being pressed at the same time 1 Answer


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