UI down arrow button

Hello,

I usually try to figure things out on my own but I’m stumped. I have a left, right and down arrow in my UI touch controls. The left and right buttons are working great. I cannot for the life of me figure how to code my down arrow to represent what essentially is the (“s”), (“↓”) on the keyboard. Any ideas on how to code this would be greatly appreciated.

Thanks for your time!

Could you share what you have tried? I’m not sure what you mean by “represent what is ‘S’ or ‘Down arrow’…”

I think you mean you want it to do the same thing on mobile, that pressing S or down arrow does on the editor? Right?

Right, I would like my UI down button to do the same thing as S or down arrow. Here is just one example. Thanks

   if (moveDown && Input.GetAxisRaw("Vertical") >= 0f)
        {
            myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, -1f, 0f);
        }

Well, the way I always handle having controls work on both mobile and in the editor (or standalone win/mac) is to do something where I look for input in the editor over an update (checking if a key is down, or an axis) but when using the UI to do things on mobile, you need to check instead for events of the button, like OnPointerDown and OnPointerUp to keep track of the user tapping and holding the UI button down. (read more on that here) and then have it do the exact same things while that input is held down.

Here is my code below. I’m using an Event Trigger > Pointer Down along with Bools.

Player Script
      
if (moveright)
        {
            transform.localScale = new Vector3(1, 1, 1);
            myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);
            myAnim.SetFloat("Speed", Mathf.Abs(myRigidbody.velocity.x));
        }
        if (moveleft)
        {
            transform.localScale = new Vector3(-1, 1, 1);
            myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);
            myAnim.SetFloat("Speed", Mathf.Abs(myRigidbody.velocity.x));
        }
        if (moveJump && (isGrounded))
        {
            myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
            jumpSound.Play();
        }
        if (moveDown)
        {
           
        }
Control Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchControls : MonoBehaviour
{
    private PlayerController thePlayer;

    // Use this for initialization
    void Start ()
    {
        thePlayer = FindObjectOfType<PlayerController>();
    }
    public void LeftArrow()
    {
        thePlayer.moveright = false;
        thePlayer.moveleft = true;
    }
    public void RightArrow()
    {
        thePlayer.moveright = true;
        thePlayer.moveleft = false;
    }
    public void DownArrow()
    {       
        thePlayer.moveDown = true;       
        thePlayer.moveUp = false;                         
    }
    public void jumpButton()
    {
        thePlayer.moveJump = true;       
    }
   
    public void ReleaseLeftArrow()
    {
        thePlayer.moveleft = false;
    }
    public void ReleaseRightArrow()
    {
        thePlayer.moveright = false;
    }
   // public void ReleaseDownArrow()
   // {
   //     thePlayer.moveDown = false;
   // }
    public void ReleaseUpArrow()
    {
        thePlayer.moveDown = false;
    }
    public void ReleaseJumpButton()
    {
        thePlayer.moveJump = false;             
    }
}

So what exactly is going wrong with the code you think? Is the down arrow functionality just not working? How far through the code does it seem to get? Is moveDown being set true when the UI button is held down? Is it being set false when it is let go? Does it work normally when your doing this with the WASD keys?

Yes my moveDown bool is set to true when holding down on the UI button and set to false when let go.

Sorry if there are gaps in explaining what I’m trying to do but basically I wrote a script to disable the 2D collider on any platform so I can press down and go through it and once through the platform it re-enable the 2D collider. Now this works using the S and down arrow perfectly on my keyboard but I can’t seem to figure out how to use my UI down button to accomplish this task.

  • if (moveDown)
  • {
  • “Do something”
  • }

Wheres the code where it worked on PC (well, on keyboard)? I mean like a snippet of the part that does what you need it to do, when you press S or down arrow. Sorry to make you work for it haha, but I just am trying to figure out exactly where things are going wrong!