Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 munggol97 · Dec 25, 2018 at 02:20 PM · movementbuttonmobilecontrollercrossplatform

How to transpose key press events to UI Buttons

Hi, I hope someone really helps me with my problem. I'm having an on screen buttons that controls the movement of the character, the current controls that I have is A,D and Space that controls the movement left, right and Jump of my character now I want to control my character with on screen buttons, but how do I do that with my current code? Thanks A lot in Advance to anyone replying on my question!

 void Update () {
         
         Movement();
         {
             float move = Input.GetAxis("Horizontal");
             anim.SetFloat("speed", move);
 
            isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
     
 
         } 
     }
 
     void Movement()
     {
         if (Input.GetKey(KeyCode.D))
         {
             transform.Translate(Vector2.right * 3f * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if (Input.GetKey(KeyCode.A))
         {
             transform.Translate(-Vector2.right * 3f * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
 
         if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
         {
             isJumping = true;
             jumpTimeCounter = jumpTime;
             rb.velocity = Vector2.up * jumpForce;
             rb.freezeRotation = true;
             anim.SetBool("IsJumping", false);
         }
 
         if (Input.GetKey(KeyCode.Space) && isJumping == true)
         {
             if (jumpTimeCounter > 0)
             {
                 rb.velocity = Vector2.up * jumpForce;
                 rb.freezeRotation = true;
                 jumpTimeCounter -= Time.deltaTime;
                 anim.SetBool("IsJumping", true);
             }
             else
             {
                 isJumping = false;
             }
 
 
         }
         if (Input.GetKeyUp(KeyCode.Space))
         {
             isJumping = false;
             anim.SetBool("IsJumping", false);
         }
 
     }


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 ignacevau · Dec 25, 2018 at 03:03 PM

Since you are using GetKey() GetKeyDown() and GetKeyUp() you could add an Event Trigger component to your buttons. To implement the GetKeyDown() and GetKeyUp() for your 'Space' button, you could write this:

     public void OnSpaceDown()
     {
         if(isGrounded == true)
         {
             isJumping = true;
             jumpTimeCounter = jumpTime;
             rb.velocity = Vector2.up * jumpForce;
             rb.freezeRotation = true;
             anim.SetBool("IsJumping", false);
         }
     }
 
     public void OnSpaceUp()
     {
         isJumping = false;
         anim.SetBool("IsJumping", false);
     }

Add these methods to the Event Trigger, for the events Pointer Down and Pointer Up


To implement the GetKey() you could do write:

     bool RightPressed;
     bool LeftPressed;
     bool SpacePressed;
     public void OnScreenButtonChange(string dir)
     {
         switch (dir)
         {
             case "D":
                 RightPressed = !RightPressed;
                 break;
             case "A":
                 LeftPressed = !LeftPressed;
                 break;
             case "Space":
                 SpacePressed = !SpacePressed;
                 break;
         }
     }



Add the method OnScreenButtonChange with the respective string parameters (`"D"`, "A" and "Space") to the Pointer Down and Pointer Up events for each button. Now you can simply replace Input.GetKey(KeyCode.Space) with SpacePressed in your Movement method:


     void Movement()
     {
         if (RightPressed)
         {
             transform.Translate(Vector2.right * 3f * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if (LeftPressed)
         {
             transform.Translate(-Vector2.right * 3f * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if (SpacePressed)
         {
             if(isJumping == true)
             {
                 isJumping = true;
                 jumpTimeCounter = jumpTime;
                 rb.velocity = Vector2.up * jumpForce;
                 rb.freezeRotation = true;
                 anim.SetBool("IsJumping", false);
             }
         }
     }

Also, Merry Christmas!

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 munggol97 · Dec 25, 2018 at 03:58 PM 0
Share

The code looks so good. The left and right button are working it moves the characters left and right but the walk animation are not playing as the character walks, and also the Idle animation and Jump animation are having a conflict they are playing at the same time? Thank you for helping me out, $$anonymous$$erry Christmas!

avatar image munggol97 · Dec 25, 2018 at 05:08 PM 0
Share

$$anonymous$$aybe there will be also changes on the Update method?

avatar image ignacevau · Dec 25, 2018 at 05:26 PM 0
Share

That's because I just showed you the method to do it, I didn't write the full script. Here is the full script:


     void Update()
     {
         $$anonymous$$ovement();
         float move = Input.GetAxis("Horizontal");
         anim.SetFloat("speed", move);
 
         isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
     }
 
     void $$anonymous$$ovement()
     {
         if (RightPressed)
         {
             transform.Translate(Vector2.right * 3f * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if (LeftPressed)
         {
             transform.Translate(-Vector2.right * 3f * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if (SpacePressed && isJumping == true)
         {
             if (jumpTimeCounter > 0)
             {
                 rb.velocity = Vector2.up * jumpForce;
                 rb.freezeRotation = true;
                 jumpTimeCounter -= Time.deltaTime;
                 anim.SetBool("IsJumping", true);
             }
             else
             {
                 isJumping = false;
             }
 
 
         }
     }
 
     public void OnSpaceDown()
     {
         if (isGrounded == true)
         {
             isJumping = true;
             jumpTimeCounter = jumpTime;
             rb.velocity = Vector2.up * jumpForce;
             rb.freezeRotation = true;
             anim.SetBool("IsJumping", false);
         }
     }
 
     public void OnSpaceUp()
     {
         isJumping = false;
         anim.SetBool("IsJumping", false);
     }
 
     bool RightPressed;
     bool LeftPressed;
     bool SpacePressed;
     public void OnScreenButtonChange(string dir)
     {
         switch (dir)
         {
             case "D":
                 RightPressed = !RightPressed;
                 break;
             case "A":
                 LeftPressed = !LeftPressed;
                 break;
             case "Space":
                 SpacePressed = !SpacePressed;
                 break;
         }
     }

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

199 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 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 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 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 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 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 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

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

My character has stopped moving 0 Answers

Movement Object 0 Answers

Button and Touchpad work together 1 Answer

How to know if finger is on joystick if it is at horizontal & vertical zero 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