- Home /
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
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.
@$$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
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.
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,
okay my bad just passed it this movingLeft(1, 0.1f); ins$$anonymous$$d and it runs perfectly thanks again @callen
Your answer
![](https://koobas.hobune.stream/wayback/20220612065327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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