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 martipello · Jan 24, 2016 at 08:37 PM · uimovementgetaxisgetkey

how to move player with getAxis and UI buttons

pretty self explanatory title but lets expand a little so i now have a few UI buttons left right and jump but my players left and right motion is controlled through Input.GetAxis("Horizontal") so im wondering if i can split this somehow into left and right this is the block

 moveHorizontal = Input.GetAxis("Horizontal");
             Vector3 movement = new Vector3(moveHorizontal, 0, 0);
             rb.AddForce(movement * speed);

so i need to check if moveHorizontal is < or > 0 and then move left or right but i dont know how you can move left or right using getAxis or if i can swap it to something like getKey and still keep the same speed and such, heres how im passing the code to my UI button, someone from here helped me with this,

 public void Move(string mDir)
 {
     if (mDir == "left")
     {

         moveHorizontal = Input.GetAxis("Horizontal");
         Vector3 movement = new Vector3(moveHorizontal, 0, 0);
         rb.AddForce(movement * speed);
         animator.SetFloat("rollLeft", 0);
         animator.SetFloat("rollRight", 2);
         jumpingDirection = false;
         if (moveHorizontal < 0)
         {
            
         }

     }
     else if (mDir == "right")
     {
        
         moveHorizontal = Input.GetAxis("Horizontal");
         Vector3 movement = new Vector3(moveHorizontal, 0, 0);
         rb.AddForce(movement * speed);
         animator.SetFloat("rollRight", 0);
         animator.SetFloat("rollLeft", 2);
         jumpingDirection = true;
         if (moveHorizontal > 0)
         {
            
         }


     }
     else if (mDir == "jump")
     {
         doMovement = true;
         isGrounded = false;
         rb.AddForce(0, jumpSpeed, 0);
         source.PlayOneShot(jumpNoise, 1f);

         if (jumpingDirection == true)
         {
             animator.SetFloat("jumpLeft", 0);
             animator.SetFloat("jumpRight", 2);
         }
         else if (jumpingDirection == false)
         {
             animator.SetFloat("jumpRight", 0);
             animator.SetFloat("jumpLeft", 2);
         }

     }

so obviously as it stands left and right do nothing but change the animation and that works but theres obviously no movement but jump works fine any and all feedback welcome

Comment
Add comment · Show 4
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 ShadyProductions · Jan 24, 2016 at 09:58 PM 0
Share

Why do you want to use your horizontal axis, you aren't pressing your keys when pressing the ui button are you? Just do it like this.

 if (mDir == "left")
      {
          Vector3 movement = new Vector3(-1, 0, 0); //i think left is -1, otherwise it is 1
          rb.AddForce(movement * speed); //might change this with transform.Translate maybe?
          animator.SetFloat("rollLeft", 0);
          animator.SetFloat("rollRight", 2);
          jumpingDirection = false;
      }
avatar image martipello · Jan 24, 2016 at 10:44 PM 0
Share

haha hi :-) thank you for your reply i changed it to

  transform.Translate(Vector3.left * Time.deltaTime * speed);

and i get a juttery step forwards but its not firing continuously, its in pointerdown, so should i be editing what you wrote to take a bool value so that i can toggle it on and off

avatar image ShadyProductions martipello · Jan 24, 2016 at 10:54 PM 0
Share

I'm sure there are onButtonDown methods that make it fire the code while the button is being clicked look it up on the wiki :)

avatar image martipello · Jan 24, 2016 at 11:50 PM 0
Share

I will do thank you I have something that's semi working now

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vishwa001 · Jun 29, 2017 at 06:37 AM

i am also looking for same solution. to GetAxis Horizontal by UI button in unity. But still no solution found.

Comment
Add comment · 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
0

Answer by Kishotta · Jun 29, 2017 at 07:21 AM

It seems like you already have something that works, but here's another answer anyway.

You can achieve something similar to Input.GetAxis ("whatever") with:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class UIButtonInputAxis : MonoBehaviour {
     private float value;
     public float Value { // Readonly for security
         get {
             return value;
         }
     }
     public Button PositiveButton;
     public Button NegativeButton;
     public void UpdateAxisValue (int v) {
         value += v;
     }
 }

Attach the UIButtonInputAxis to an empty GameObject in your scene (You'll need one of these per axis you want to set up).

You'll need two buttons, and each one should have an "Event Trigger" component with both "Pointer Down" and "Pointer Up" events added.

On the "Positive" button's "Pointer Down" event, assign the UIButtonInputAxis object, and call the UpdateAxisValue function with whatever positive value you want (1, most likely). On the "Pointer Up" event, do the same thing, but with -1.

Do the exact same thing on the "Negative" button's events, but with the value's swapped (-1 on "Pointer Down", +1 on "Pointer Up").

When this is set up, it can be used as such:

 using UnityEngine;
 using UnityEngine.UI;
 public class UIButtonInputAxisExample : MonoBehaviour {
     public UIButtonInputAxis inputAxis;
     public Slider example; // Slider with range set from -1 to 1
     void Update () {
         example.value = inputAxis.Value;
     }
 }

Just hook up the UIButtonInputAxis in the inspector, and you're good to go.

Comment
Add comment · 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

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

46 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

Related Questions

Maintaining Run on direction change (double tap using GetAxis) 0 Answers

UI object move? 2 Answers

Looking for a clean bug-free solution to my issue with collisions on a moving UI object with other UI objects 1 Answer

Triple Slider control - UI slider value control 1 Answer

clicking on button while moving on andriod 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