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 Jul 22, 2014 at 01:06 PM by RedDevil for the following reason:

I figured it out by myself after a few hours

avatar image
0
Question by RedDevil · Jul 22, 2014 at 08:22 AM · inputtouchcontrollertouchscreentouchphase

Unity 2D Touch Controll via on screen button for Mobile

Hi.I have implemented touch controll over my game from diferent scripts I found on the web to be able to jump and move left/right via buttons on screens(im using GUI Textures i created).The problem is the script I ended up with is not very responsive and it just seems wierd. It all worked perfect on the computer when I was using Input.GetAxis horizonal but when I try via code it just does not have the same feel to it.Curently my problem is with Left/Right movement. Here is my code:

   void FixedUpdate()
 {
     grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
     if(move < 0 && facingRight)
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
     if(move > 0 && !facingRight)
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
     // 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 right arrow?
             if (RIGHT.HitTest(t.position, Camera.main))
             {
                 move += 0.99f;
                 rigidbody2D.velocity = new Vector2(move * MaxSpeed,rigidbody2D.velocity.y);
                 anim.SetFloat("Speed",Mathf.Abs(move));
                 if(move > MaxSpeed)
                 {
                     move = MaxSpeed;
                 }
             }
             // Are we touching the left arrow?
             if (LEFT.HitTest(t.position, Camera.main))
             {
                 move -= 0.99f;
                 rigidbody2D.velocity = new Vector2(move * MaxSpeed,rigidbody2D.velocity.y);
                 anim.SetFloat("Speed",Mathf.Abs(move));
                 if(move > MaxSpeed)
                 {
                     move = MaxSpeed;
                 }
             }
             // Are we touching the jump button?
             if (UP.HitTest(t.position, Camera.main) && grounded)
             {
                     anim.SetBool("Ground",false);
                     anim.SetBool("Jumping",true);
                     rigidbody2D.AddForce(new Vector2(0,JumpForce));
             }
             if (DOWN.HitTest(t.position, Camera.main))
             {
                 anim.SetBool("Crouching",true);
             }

         }
         
         // Did the touch end?
         if (t.phase == TouchPhase.Ended)
         {
             anim.SetBool("Crouching",false);
             anim.SetBool("Jumping",false);
             anim.SetFloat("Speed",0);
             move = 0;
             // Stop all movement
             rigidbody2D.velocity = Vector2.zero;
         }
     }
     if(grounded && Input.GetKeyDown(KeyCode.Z))
     {
         move -= 10;
         rigidbody2D.velocity = Vector2.right * -5;
         anim.SetFloat("Speed",Mathf.Abs(-5));
     }
     if(grounded && Input.GetKeyUp(KeyCode.Z))
     {
         move = 0;
         anim.SetFloat("Speed",0);
     }
     if(grounded && Input.GetKeyDown(KeyCode.X))
     {
         move = 10;
         rigidbody2D.velocity = Vector2.right * 5;
         anim.SetFloat("Speed",Mathf.Abs(5));

     }
     if(grounded && Input.GetKeyUp(KeyCode.X))
     {
         move = 0;
         anim.SetFloat("Speed",0);
     }
     if(grounded && Input.GetKeyDown(KeyCode.Space))
     {
         anim.SetBool("Ground",false);
         anim.SetBool("Jumping",true);
         rigidbody2D.AddForce(new Vector2(0,JumpForce));
     }
     if(grounded && Input.GetKeyUp(KeyCode.Space))
     {
         anim.SetBool("Jumping",false);
     }
     if(grounded && Input.GetKeyDown(KeyCode.C))
     {
         anim.SetBool("Crouching",true);
     }
     if(grounded && Input.GetKeyUp(KeyCode.C))
     {
         anim.SetBool("Crouching",false);
     }
 }

I am using Z and X for left and right so I dont have to keep building versions for iPad to test.Hope you can help me.Thank you

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

0 Replies

  • Sort: 

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

Touch inputs Angry-bird style 0 Answers

made a football game and my touches works fine on my laptop but on my device only the upper region works 0 Answers

Touch Input madness 2 Answers

Unity Touch Help! 0 Answers

Unity Getting Input from Horizontal axis while using buttons 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