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 /
This question was closed Sep 27, 2015 at 01:12 PM by RedDevil for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RedDevil · Jul 18, 2014 at 10:57 AM · inputtouchtouchscreen

Unity Getting Input from Horizontal axis while using buttons

I have a good code for my game to move left/right/up/down.I then made on scree controls using some pictures with touch input.The problem im facing is that i cannot move left/right using the buttons.The character is stading in place and i dont know how to solve this please help.

   // Check to see if the screen is being touched
     if (Input.touchCount > 0)
     {
         // Get the touch info
         Touch t = Input.GetTouch(0);
         
         // Did the touch action just begin?
         if (t.phase == TouchPhase.Began)
         {
             // Are we touching the left arrow?
             if (LEFT.HitTest(t.position, Camera.main))
             {
                 float move = Input.GetAxis("Horizontal");
                 rigidbody2D.velocity = new Vector2(move * MaxSpeed,rigidbody2D.velocity.y);
                 anim.SetFloat("Speed",Mathf.Abs(move));
                 facingRight = false;
             }

             // Are we touching the right arrow?
             if (RIGHT.HitTest(t.position, Camera.main))
             {
                 float move = Input.GetAxis("Horizontal");
                 rigidbody2D.velocity = new Vector2(move * MaxSpeed,rigidbody2D.velocity.y);
                 anim.SetFloat("Speed",Mathf.Abs(move));
                 facingRight = true;
             }
             
             // Are we touching the jump button?
             if (UP.HitTest(t.position, Camera.main))
             {
                 anim.SetBool("Ground",false);
                 anim.SetTrigger("Jumping");
                 rigidbody2D.AddForce(new Vector2(0,JumpForce));
             }
             if (DOWN.HitTest(t.position, Camera.main))
             {
                 anim.SetBool("Crouching",true);
             }
             else anim.SetBool("Crouching",false);

         }
         
         // Did the touch end?
         if (t.phase == TouchPhase.Ended)
         {
             // Stop all movement
             rigidbody2D.velocity = Vector2.zero;
         }
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

  • Sort: 
avatar image
1
Best Answer

Answer by Tehnique · Jul 18, 2014 at 11:05 AM

I't not working because you use "Input.GetAxis("Horizontal");". That's only for Keyboards/Joysticks.

You alredy have code to detect if the user is touching your move icons, just set "move" as -1 for left and 1 for right, and remove the GetAxis code.

Comment
Add comment · Show 8 · 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 RedDevil · Jul 18, 2014 at 11:09 AM 0
Share

i have tried this and the character is still standing in place while the animation is starting to run

avatar image Tehnique · Jul 18, 2014 at 11:15 AM 0
Share

Are you setting $$anonymous$$axSpeed?

avatar image RedDevil · Jul 18, 2014 at 11:20 AM 0
Share

yes it is decleared public float $$anonymous$$axSpeed = 1f; i also tried values under 1 and same result

avatar image Tehnique · Jul 18, 2014 at 11:27 AM 0
Share

Try it with $$anonymous$$axSpeed = 10f, or somthing bigger than 1.

avatar image RedDevil · Jul 18, 2014 at 11:36 AM 0
Share

all the checkig gave me the idea and it seems like the problem was i was checking for both keyboard input and touch input with the same variables and that caused the problem.now its all solved ty for trying to help

Show more comments

Follow this Question

Answers Answers and Comments

22 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

Related Questions

,How Do get Touch input to work with the Player Input Component 1 Answer

How to add touch input for mobile devices using unity? 2 Answers

InputSystem simulated touchscreen stops working from v1.1.0 preview 1 0 Answers

Touch Input madness 2 Answers

Touch inputs Angry-bird style 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