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 · Feb 23, 2016 at 05:02 PM · uimovementinput.getaxis

moving player with ui buttons

ive set up my methods to work for a keyboard so for instance this calls my moving left method

 if (Input.GetKey(KeyCode.LeftArrow))
     {
         movingLeft();
         animationClips();
     }

i want to call the same method from a ui button i use a bool when true it can move when false it cant and set these to my button in in the pointer up and pointer down to get continual movement something like this

 public void MoveMeLeft()
 {
     moveLeft = true;
 }

 public void StopMeLeft()
 {
     moveLeft = false;
 }

and then check them and move accordingly like this

      if (moveLeft && !moveRight)
     {
         movingLeft();
         animationClips();
     }

     if (moveRight && !moveLeft)
     {
         movingRight();
         animationClips();
     }

but this doesnt seem to work, if i add a debug.log("player should be moving left") in my moving left method it prints to the screen but no movement happens despite it being the same method im calling for the keyboard and it works fine, can any body tell me whats going on here?

heres my movingLeft method,

 public void movingLeft()
 {
     moveHorizontal = Input.GetAxis("Horizontal");
     Debug.Log("i should be moving left"); //this displays in the console when pressing keyboard or pressing the ui button
     Vector3 movement = new Vector3(moveHorizontal, 0, 0);
     playerRigidbody.AddForce(movement * speed);
     if (moveHorizontal < 0)
     {
         jumpDirection = false;
         moveDirection = false;
     }
 }

any and all suggestions welcome, many thanks

Comment
Add comment · Show 2
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 Magius96 · Feb 23, 2016 at 05:35 PM 0
Share

Input.GetAxis("Horizontal") is only returning a value when you are using the keyboard. But you seem to be using its values even when using the UI buttons.

Think about this ins$$anonymous$$d. Create a Vector3 at class level. When you recieve input from the keyboard or the UI, modify the Vector3 values appropriately. Then in your Update method, use the Vector3 values to perform the actual movement and set what animations need to be done.

avatar image martipello · Feb 23, 2016 at 07:39 PM 0
Share

@$$anonymous$$agius96 O$$anonymous$$ that makes sense I've done some research and found the same conclusion, but input.getaxis seems to go from 0-1 in a second or 2 if I change this to snap straight to 1 I'll have stop and go rather than a kind of acceleration, could you expand on how I could change this to kind of accelerate? Would I be looking at adding a co routine to speed up over time or is that overkill? Thank you for you reply

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by callen · Feb 23, 2016 at 09:07 PM

Try changing your function like this. Change Speed Secs describes, starting at the moment you start pressing the left button, how much time it will take to change movement.x from 0 to -1 (in the GetAxis version, you're using analog values so change speed is less relevant)

 Vector2 movement = Vector2.zero;
 
 public void movingLeft(float moveInput, float changeSpeedSecs)
  {
      movement.x = Mathf.max(-1, movement.x - moveInput * (Time.deltaTime / changeSpeedSecs));
      playerRigidbody.AddForce(movement * speed);
      if (moveInput < 0)
      {
          jumpDirection = false;
          moveDirection = false;
      }
  }
 
 //Example usage for GetAxis, with fast change time since InputManager applies this to axes already:
 movingLeft(Input.GetAxis("Horizontal"), 0.01f);
 
 //Example usage for buttons, using slower change time to prevent abrupt start/stop
 movingLeft(-1, 0.5f);

I'm not totally sure how your code is structured (why moveLeft/Right instead of a single moveHorizontal method) but this example should work for you. To make the "movingRight" version, you'd just have to flip a couple negatives to positives.

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 martipello · Feb 23, 2016 at 10:27 PM 0
Share

for what ive asked this definitely works thank you very much its a really nice piece of code that is also quite flexible, however there are a couple of odd glitches ive just been playing with it if you move right or left let the player come to a stop and try moving in the other direction the player will step once more in the opposite direction, (you dont have to let the player stop its just more noticable) again im not trying to pick holes in it its just for others reference and again thank you very much,

avatar image martipello · Feb 24, 2016 at 12:39 AM 0
Share

okay my bad just passed it this movingLeft(1, 0.1f); ins$$anonymous$$d and it runs perfectly thanks again @callen

avatar image callen · Feb 24, 2016 at 02:17 PM 0
Share

I'm glad you got it working!

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

53 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

Related Questions

Input.GetAxis not being called every called every frame 1 Answer

Click Goes Everywhere 0 Answers

Movement in Scroll View 0 Answers

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

Look Where You're Going 3 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