- Home /
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
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;
}
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
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 :)
I will do thank you I have something that's semi working now
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.
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.
Your answer
Follow this Question
Related Questions
Maintaining Run on direction change (double tap using GetAxis) 0 Answers
UI object move? 2 Answers