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 Leroterr · May 26, 2014 at 08:48 AM · androidmovementiostouchaxis

Replace Input.GetAxis with Touch

Is there a way of converting this:

         moveHorizontal = Input.GetAxis ("Horizontal");
         moveVertical = Input.GetAxis ("Vertical");
 
         transform.Translate (Vector3.right * moveHorizontal * speed * Time.deltaTime);
         transform.Translate (Vector3.forward * moveVertical * speed * Time.deltaTime);

...so that it would work for touch?

I made buttons on my game where if I touch the up arrow key using my Android, it would go forward as if I pressed the up arrow key on my keyboard.

My touch code is currently this one, but it just detects the touch, it doesn't really move anything.

 if (Input.touchCount > 0) 
         {
             Touch theTouch = Input.GetTouch (0);
             
             Ray ray = Camera.main.ScreenPointToRay(theTouch.position);
             
             if(Physics.Raycast(ray, out hit) && hit.transform.tag == "UpButtonTag")
             {
                 if(Input.touchCount == 1)
                 {
                     if (theTouch.phase == TouchPhase.Stationary || theTouch.phase == TouchPhase.Moved) 
                     {
                         Debug.Log ("The Up Button is pressed!");
                     }
                 }
             }
         }

I have many more of these for the Left Button, Right Button, Down Button, etc, but I still don't know how to make it move my player like from the code above.

Thanks! I know someone already asked this question here, but its answers didn't work for me, and the asker didn't also chose a correct answer.

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
2
Best Answer

Answer by fafase · May 26, 2014 at 09:08 AM

 public sealed class InputSystem:MonoBehaviour 
 {
 public Rect[] axisVert;
 public Rect[] axisHor;
     private Rect[]r_axisVert;
     private Rect[]r_axisHor;
 public bool snap = true;
 private static bool b_snap;
 private static float xMov = 0;
 private static float yMov = 0;
 private static float ratio = 0.5f;
 void Start(){
     b_snap = snap;
             r_axisVert = axisVert;
             r_axisHor = axisHor;
 }

     public static float GetAxis(Axis axis)
     {
     Touch[] myTouches = Input.touches;
     for(int i = 0; i < myTouches.Length; i++){
         if(axis == Axis.Vert){
             for(int j = 0; j < r_axisVert.Length; j++){
                 if(r_axisVert[j].Contains(myTouches[i].position)){
                     float value = 1 * 2 - 1;
                     if(b_snap)
                         return value;
                     else{
                         yMov = Mathf.MoveTowards(yMov, value, ratio * Time.deltaTime);
                         return yMov;
                     }
                 } 
             }
             yMov = Mathf.MoveTowards(yMov, 0, ratio * Time.deltaTime);
             return yMov;
         }
         // Same for horizontal axis
 }
 }
 public enum Axis{
     Hor, Vert
 }

This will return -1, 0 or 1. If you need the smooth system Unity provide you would need to add a variable to store the previous value and lerp to the -1 , 1 or 0 when you release.

You need to add the component and provide the rect values in the inspector. The first rect is for the left/down rect, the second is for right/up. This could be modified to use a different approach like marking some objects as axis buttons and finding them on start but for now this is simple.

Comment
Add comment · Show 12 · 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 fafase · May 26, 2014 at 09:38 AM 1
Share

You use it with :

 float hor = InputSystem.GetAxis(Axis.Hor);
avatar image fafase · May 26, 2014 at 09:40 AM 1
Share

Ok hold on a second I am trying it now, I have forgotten a couple of details.

avatar image fafase · May 26, 2014 at 09:55 AM 1
Share

Ok, I reviewed, now it should also include the lerping. You define it with snap. You can make it faster changing the value of ratio.

It works for me with normal input, but I owuld guess that should do it the same with GetTouch

avatar image fafase · May 26, 2014 at 12:11 PM 1
Share

I added the missing [i], hopefully that will work now.

avatar image fafase · May 26, 2014 at 05:07 PM 1
Share

Ok, I fixed this. Static method can only use static members so the rects needed to be turned into static ones. $$anonymous$$aybe this is not the best way in the end and you should maybe turn it all into a non-static method. Up to you.

Show more comments
avatar image
1

Answer by Andres-Fernandez · May 26, 2014 at 09:11 AM

You can get the Rect from the button and check if it contains the position of the touch. If it does, set the logic for the movement (some booleans or whatever) instead of the Debug.Log.

Also, I've read that GUITextures get the OnMouseDown event.

Comment
Add comment · Show 3 · 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 Leroterr · May 26, 2014 at 09:28 AM 0
Share

Thanks, how do I make it move like the Input System Unity provides? Where it goes smoothly when I release the button. transform.translate doesn't do that, and I'm not sure how to use Lerp in this situation.

avatar image Andres-Fernandez · May 26, 2014 at 09:36 AM 1
Share

That's another question. The part that makes it more smooth is the Time.deltaTime. But if you look for a way to do it cooler you should check for SmoothDamp for the smooth translation, and google for movement inertia for the release (there are tons of scripts)

avatar image Leroterr · May 26, 2014 at 09:46 AM 1
Share

Alright thanks, I'll go take a look at SmoothDamp.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Move horizontally on touch 0 Answers

getPressure for touch pressure on Android or iOS 3 Answers

Touch joystick tutorials ? 0 Answers

desktop,ios and android script differences?? 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