On Screen Button for Jumping

Hello, I am on progress to making mobile 2D platformer. I am using unity button as control interface. The problem is when I want to make jump button pressed when only start pressed (no jumping as long as player hold it). How to do that?

Hi @RxGian

Well this is an UI question not 2D question.

You should definitely check Unity UI basics tutorials.

EventSystem has several interfaces to handle various events:
https://docs.unity3d.com/Packages/com.unity.ugui@1.0/api/UnityEngine.EventSystems.IPointerDownHandler.html

by change I also want toa ask about that event systems, I am using this virtual joysticks: Joystick Pack | Input Management | Unity Asset Store

Previously I make jump button with unity buttons and I found it’s not match for control input since event activated when button has released. I found inside of that assets virtual joystick using pointer event and I need information how to use it. That assets only activate joystick inside of panel area and I can’t find when that pointer event reads panel data…

@RxGian

“I found inside of that assets virtual joystick using pointer event”

How to use Pointer events is explained in many Unity’s official tutorials, just go to Learn section and watch some tutorials. Also, manual / API reference has several examples.

https://docs.unity3d.com/Manual/EventSystem.html

“That assets only activate joystick inside of panel area and I can’t find when that pointer event reads panel data…”

Edit:
Pointer events work so that you attach the script with implemented listener methods (PointerDown, Drag…) to some UI element. When you click/drag that specific UI element, the method gets called. UI events don’t react to just any clicks on screen.

Well, the event system mostly sends listener something based on specific event. Imagine when you play 2D games like King of fighter or marvel vs capcom, player should input specific steps to perform special move (like dragon punch input or down to forward 90 degree). Or like super mario bros when player must throw fire balls, they should press rapidly (unless turbo button) Unity has Input.onkeypressed but it will not work on touch input.

I’m sorry, actually reading the eventSystem is still unclear and ambiguous

1 Like

@RxGian

And because of that, it is best to read the docs and watch the tutorials :slight_smile:

Uh yeah, i will try and post another question after finished

I’d try to nest your Inputs, which in your case would be button presses. You could use IPointerDownHandler to set a variable true when pressed. Then inside of Update() check to see if that variable is true. If true, then check to see if another button is pressed, which sets another variable true.

The only thing about coding this way for the IPointerHandler is that each button would need it’s own script. You’d use static bool variables to pass onto your movement/attack script. I could demonstrate a code example if it’s unclear what I am saying. Let me know :slight_smile:

Yes please, I need a simple code for example. Is IPointerDownHandler only “called” or “active” once just like Input.GetButtonPressed method?

Okay, let’s say you have a movement script. You want to attach a script like this to each of your directional buttons:

public class LeftButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{

// You would only need moveLeft for this script.
// You put the other variables on the according script
// Not on this one. (You can if you want, but if for some
// reason you need to disable the button, this script may
// also get disabled and cause a problem.
//You will want one script for each button. Put the vars
// on each one by name to make it easier.
public static bool moveLeft;
public static bool moveRight;
public static bool moveUp;
public static bool moveDown;
    public void OnPointerEnter(PointerEventData pointerEventData)
    {
    moveLeft = true;
    }

    public void OnPointerExit(PointerEventData pointerEventData)
    {
     moveLeft = false;
    }

Now on your movement script, you’d do something like this:

public class Movement : MonoBehavior
{
private void Update()
{
if(LeftButton.moveLeft == true)
{
MovePlayerLeft();
}
else if(RightButton.moveRight == true)
{
MovePlayerRight();
}
else if(UpButton.moveUp == true)
{
MovePlayerUp();
}
else if(DownButton.moveDown == true)
{
MovePlayerDown();
}
}

MovePlayerLeft()
{
// Code to move left
}

MovePlayerRight()
{
// Your code to move player right
}

MovePlayerUp()
{
// Your code to move up
}

MovePlayerDown()
{
// Your code to move down
}
}

Like I said, you simply need to create the scripts for each button and then set the static variable true/false depending on the IPhandler event.

Edit: Since you’re really asking about the jump functionality, I’ll demonstrate that:

public class JumpButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public static bool canJump;

private void Start()
{
canJump = true; // Maybe you dont need or want to do this?
}

    public void OnPointerEnter(PointerEventData pointerEventData)
    {
     canJump = false;
    }

    public void OnPointerExit(PointerEventData pointerEventData)
    {
     canJump = true;
    }
}

Now all you need to do is add a PlayerJump function to the movement script and make the conditions that if JumpButton.canJump == true then jump. Until the player releases the button, the variable will remain canJump = false.

2 Likes

I see, basically I just need to call OnPointerEnter and OnPointerExit.

Maybe that’s enough clear to answer this thread, I will continue in a new thread for future issue. Thank you all

1 Like

I don’t remember if you will need to import a namespace for EventSystems or not but if the IPhandler doesn’t work, try adding

using UnityEngine.EventSystems;

Good luck to ya, amigo!

1 Like