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
0
Question by Tekksin · May 09, 2014 at 05:46 AM · inputmobiletouchaxiscontrols

Using Input.GetAxis variable for Touch Controls

Hey everybody, I'm having some trouble getting my touch controls to work right, and was wondering if anybody here has had experience with it.

I have my jump button linked to a guiTexture (as with my other controls), so it (in particular) was very easy to script for touch controls with a hitTest, but my left and right movement is a lot trickier.

The game in question is a 2D platformer. Currently the player moves by using this code:

 var move : float = Input.GetAxis ("Horizontal");
 
 if(move > 0){
      rigidbody2D.velocity.x = move * maxSpeed;
 }
 if(move < 0){
      rigidbody2D.velocity.x = move * maxSpeed;
 }

How would I translate something like that to touch controls? It is essential that I use the input manager, because the gravity, speed, and other parameters have been fine tuned to make my character control exactly how I want. I wouldn't know where to go to start hardcoding in all those values. I'm relatively novice.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by iamvishnusankar · May 09, 2014 at 05:59 AM

C# version of swipe control. Im not good at JavaScript though it will only require some declaration tweaks to make it Java

 // Update is called once per frame
     void Update () 
     {
         if(Input.GetTouch(0).phase == TouchPhase.Moved)
         {
             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
             Debug.Log(touchDeltaPosition);
             touchDeltaPosition = new Vector2(touchDeltaPosition.x,
                                              0);
             rigidbody2D.velocity = touchDeltaPosition;
         }
     
     }
Comment
Add comment · Show 2 · 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 Tekksin · May 09, 2014 at 06:27 AM 0
Share

I want the player to move right and left by tapping the left or right button, just as the player would jump by pressing the jump button. I don't want any swiping taking place whatsoever. Is the script you provided still useful? If not, how would you change it to make it work?

avatar image iamvishnusankar · May 10, 2014 at 05:28 AM 0
Share

For that you need to be familiar with Unity Input System. In unity there are 4 types of input. 1(positive/forward), 0(Idle), -1(negative/backward) and bool (To check the state of an object).

In case of motion we use (-1,0,1) inputs. In order to implement a motion system. Define a direction vector (preferably forward/right). And assign it as as translate direction of your gameObject.

As soon as someone taps the right button $$anonymous$$ultiply your direction vector using 1, When user taps left button multiply direction vector using -1. And when user takes finger from any of the button, reset them to Zero.

You could use On$$anonymous$$ouseDown() and On$$anonymous$$ouseUp()events.

 void On$$anonymous$$ouseDown()
 {
   //Logic to multiply the direction vector using -1 or 1
 }
 
 void On$$anonymous$$ouseUp()
 {
   //Logic to multiply direction vector using zero.
 }

Hope this will cover what you might be looking for. :)

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

21 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

Related Questions

PS4 Controller Y-axis is linked to L2-button? 0 Answers

Mobile - button press while moving (First Person Controls) 0 Answers

Is there a built in method that works like OnMouseDown() but for touch input? 1 Answer

Issue with Input.GetMouseButton (0) on mobile (iPhone) 2 Answers

Tie touch logic into InputManager 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