Hi, I am trying to control my character using an onscreen touch joystick. I have written this code with OnPointerDown, OnPointerUp and OnDrag and it works perfectly. The issue I am having is that this does not really replicate a real controller in the sense that if you keep the joystick pressed in a direction on a controller the movement in that direction continues. In my case if you keep the joystick pressed in a direction than it does not generate another event for movement unless i make a change in the direction axis. Is there an event or something i do not know about to do this? I have looked over the unity documentation and google searches but did not find anything usefull on what I am trying to achieve here. Note that this will be an optimised mobile game so I do not want to use fixed update or just update. Any help is apreciated. Thank you!
public void OnDrag(PointerEventData ped)
public void OnPointerDown(PointerEventData ped)
public void OnPointerUp(PointerEventData ped)
I had not added more code as it would not be relevant to my current problem.
I am moving the character about a unit in the direction of the joystick on each dragging move.
The issue i am facing is that OnDrag really works only on drag and i was wondering if there is some way to make it fire events like let's say every 0.5 s while the joystick button is being hold down. At first when writing this code I had asumed that the OnPointerDown does just that ... while it is being hold down would fire events but that is really not the case.
How can we help if you don't post the relevant code?
issue updated with relevant code
There's no actual functionality in that code...
Answer by RedDevil
Feb 08 at 04:28 PM
Since i could not really find a good way to do this with PointerEventData i just changed my code so that i can achieve what i was after.
The way I solved the problem is by changing on PointerDown to call a coroutine of a new method called movement where i just do a yield wait for seconds and do the movement on a loop and then i changed OnPointerUp to stop that coroutine when the joystick is not pressed.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
65 People are following this question.
Pointer Down event on a button still registering a hold even when I drag pointer away??
EventHandler from external DLL not firing in MonoBehaviour C# script
How to make an android joystick like Arcane Legends
Bug moving a character with onscreen Joystick prefab
Triggering events for overlaying UI Elements.