Author Topic: Character Controller Input.GetAxis("Horizontal") Question  (Read 11485 times)

badnews

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Character Controller Input.GetAxis("Horizontal") Question
« on: June 11, 2014, 02:02:37 PM »
I just bought NGUI and need some help...

Right now my 2D character is moving left and right with the left and right arrows.
I have the left and right arrows setup to do this in the input manager.

I made left and right arrow buttons and added them to my UI Root.
How do I get it to mimic the left arrow being pressed when I press the left arrow button?
Or is there a way to add the left arrow button being pressed to the input manager?

All I want to do is make the left arrow button act like when I press the left arrow key and the same for the right.
I have been stuck on this for like 6 hours now, and can't figure it out.

Any help would be greatly appreciated!
Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Character Controller Input.GetAxis("Horizontal") Question
« Reply #1 on: June 12, 2014, 02:19:23 AM »
Put a UIKeyBinding script on a button, make it listen for the appropriate button (left or right arrow). Now when you hit Play and press the key, the button will be visibly pressed.

pcutile

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 81
    • View Profile
Re: Character Controller Input.GetAxis("Horizontal") Question
« Reply #2 on: August 08, 2015, 11:28:33 AM »
Excuse me..user ask...All I want to do is make the left arrow button act like when I press the left arrow key and the same for the right.


but for me your aswer is the contrary that is i press left arrow and button result pressed..no! we nedd to press a button and it run how if i press left arrow. It is possible?
« Last Edit: August 08, 2015, 03:24:37 PM by pcutile »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Character Controller Input.GetAxis("Horizontal") Question
« Reply #3 on: August 08, 2015, 11:39:24 PM »
I don't see the difference. UIKeyBinding can be used to press the button for you when you press some key.

pcutile

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 81
    • View Profile
Re: Character Controller Input.GetAxis("Horizontal") Question
« Reply #4 on: August 09, 2015, 05:29:56 PM »
I don't want press any key i want press a button on a touch screen and read value of getaxisraw how if it come from a left or right button. how can i do this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Character Controller Input.GetAxis("Horizontal") Question
« Reply #5 on: August 14, 2015, 08:09:56 AM »
Your question doesn't make sense to me. You want to press a button on a touch screen... touch screen implies touch input. There is no "left or right" button. Left or right button implies mouse, not touch screen. Then you also ask about getaxisraw... what axis? You said touch screen... neither touch input or mouse input has anything to do with axes. I am confused...

pcutile

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 81
    • View Profile
Re: Character Controller Input.GetAxis("Horizontal") Question
« Reply #6 on: August 16, 2015, 12:48:05 PM »
yes you call a UIButton what i touch on my tabLet screen...so YOU means it not the same think.
BUt more easily i seek how can use your UiButton with getaxisraw..or if this is not possible what are the best ngui mobile joystick?
« Last Edit: August 17, 2015, 11:01:52 AM by pcutile »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Character Controller Input.GetAxis("Horizontal") Question
« Reply #7 on: August 18, 2015, 04:36:22 PM »
So you want an on-screen joystick of sorts where you touch a button and your character moves. You are approaching it from the wrong perspective. Your character currently moves as a result of GetAxis, and you think you can "fake" this by pressing a button. This is wrong -- you instead should have an abstraction here. One script takes GetAxis, and sets some global value. You then have another script that will be working with this global value to move your character, not use GetAxis.

This way your button can also set this global value when it's pressed. Don't try to set the axis that comes from Unity.
  1. public Vector2 moveAxis = Vector2.zero;
  2. public bool leftKey = false;
  3. public bool rightKey = false;
  4.  
  5. void Update ()
  6. {
  7.     moveAxis.x = Input.GetAxis("Horizontal");
  8.     moveAxis.y = Input.GetAxis("Vertical");
  9. }
  10.  
  11. void OnLeftButtonPress (bool isPressed) { leftKey = isPressed; }
  12. void OnRightButtonPress (bool isPressed) { rightKey = isPressed; }
  13.  
  14. void LateUpdate ()
  15. {
  16.     if (moveAxis.x < 0f || leftKey) MoveLeft();
  17.     if (moveAxis.y > 0f || rightKey) MoveRight();
  18. }

pcutile

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 81
    • View Profile
Re: Character Controller Input.GetAxis("Horizontal") Question
« Reply #8 on: August 21, 2015, 03:34:50 PM »
thanks arenmook